is-kit 1.6.4 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/dist/index.d.mts +150 -143
- package/dist/index.d.ts +150 -143
- package/dist/index.js +166 -159
- package/dist/index.mjs +165 -159
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -75,6 +75,7 @@ __export(index_exports, {
|
|
|
75
75
|
isZero: () => isZero,
|
|
76
76
|
mapOf: () => mapOf,
|
|
77
77
|
narrowKeyTo: () => narrowKeyTo,
|
|
78
|
+
nonEmptyArrayOf: () => nonEmptyArrayOf,
|
|
78
79
|
nonNull: () => nonNull,
|
|
79
80
|
not: () => not,
|
|
80
81
|
nullable: () => nullable,
|
|
@@ -96,14 +97,6 @@ __export(index_exports, {
|
|
|
96
97
|
});
|
|
97
98
|
module.exports = __toCommonJS(index_exports);
|
|
98
99
|
|
|
99
|
-
// src/core/define.ts
|
|
100
|
-
function define(fn) {
|
|
101
|
-
return (value) => !!fn(value);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// src/core/predicate.ts
|
|
105
|
-
var predicateToRefine = (fn) => (value) => fn(value);
|
|
106
|
-
|
|
107
100
|
// src/core/assert.ts
|
|
108
101
|
var DEFAULT_ASSERT_MESSAGE = "Assertion failed";
|
|
109
102
|
function assert(fn, value, message) {
|
|
@@ -111,94 +104,9 @@ function assert(fn, value, message) {
|
|
|
111
104
|
throw new Error(message ?? DEFAULT_ASSERT_MESSAGE);
|
|
112
105
|
}
|
|
113
106
|
|
|
114
|
-
// src/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (!predicate(value)) return false;
|
|
118
|
-
}
|
|
119
|
-
return true;
|
|
120
|
-
};
|
|
121
|
-
var everyTupleValue = (values, predicates) => {
|
|
122
|
-
if (values.length !== predicates.length) return false;
|
|
123
|
-
for (const [index, predicate] of predicates.entries()) {
|
|
124
|
-
if (!predicate(values[index])) return false;
|
|
125
|
-
}
|
|
126
|
-
return true;
|
|
127
|
-
};
|
|
128
|
-
var everySetValue = (values, predicate) => {
|
|
129
|
-
for (const value of values) {
|
|
130
|
-
if (!predicate(value)) return false;
|
|
131
|
-
}
|
|
132
|
-
return true;
|
|
133
|
-
};
|
|
134
|
-
var everyMapEntry = (values, keyPredicate, valuePredicate) => {
|
|
135
|
-
for (const [key, value] of values) {
|
|
136
|
-
if (!keyPredicate(key) || !valuePredicate(value)) return false;
|
|
137
|
-
}
|
|
138
|
-
return true;
|
|
139
|
-
};
|
|
140
|
-
var everyOwnEnumerableEntry = (values, keyPredicate, valuePredicate) => {
|
|
141
|
-
for (const key of Object.keys(values)) {
|
|
142
|
-
if (!keyPredicate(key) || !valuePredicate(values[key])) return false;
|
|
143
|
-
}
|
|
144
|
-
return true;
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
// src/utils/to-boolean-predicates.ts
|
|
148
|
-
var toBooleanPredicates = (predicates) => predicates;
|
|
149
|
-
|
|
150
|
-
// src/core/logic.ts
|
|
151
|
-
function and(precondition, condition) {
|
|
152
|
-
return define((input) => {
|
|
153
|
-
if (precondition(input)) {
|
|
154
|
-
return condition(input);
|
|
155
|
-
}
|
|
156
|
-
return false;
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
function andAll(precondition, ...steps) {
|
|
160
|
-
return define((input) => {
|
|
161
|
-
if (!precondition(input)) return false;
|
|
162
|
-
return applyRefinements(input, steps);
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
function or(...guards) {
|
|
166
|
-
const predicates = toBooleanPredicates(guards);
|
|
167
|
-
return define(
|
|
168
|
-
(input) => predicates.some((guard) => guard(input))
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
function guardIn() {
|
|
172
|
-
return (guard) => (input) => guard(input);
|
|
173
|
-
}
|
|
174
|
-
function not(fn) {
|
|
175
|
-
return (input) => !fn(input);
|
|
176
|
-
}
|
|
177
|
-
function applyRefinements(value, steps) {
|
|
178
|
-
if (!steps || steps.length === 0) return true;
|
|
179
|
-
for (const step of steps) {
|
|
180
|
-
if (!step(value)) return false;
|
|
181
|
-
}
|
|
182
|
-
return true;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// src/core/nullish.ts
|
|
186
|
-
var allowWhen = (acceptedValue, predicate) => (value) => acceptedValue(value) || predicate(value);
|
|
187
|
-
var requireWhen = (rejectedValue, predicate) => (value) => !rejectedValue(value) && predicate(value);
|
|
188
|
-
function nullable(fn) {
|
|
189
|
-
return allowWhen((value) => value === null, fn);
|
|
190
|
-
}
|
|
191
|
-
function nonNull(fn) {
|
|
192
|
-
return requireWhen((value) => value === null, fn);
|
|
193
|
-
}
|
|
194
|
-
function nullish(fn) {
|
|
195
|
-
return allowWhen((value) => value == null, fn);
|
|
196
|
-
}
|
|
197
|
-
function optional(fn) {
|
|
198
|
-
return allowWhen((value) => value === void 0, fn);
|
|
199
|
-
}
|
|
200
|
-
function required(fn) {
|
|
201
|
-
return requireWhen((value) => value === void 0, fn);
|
|
107
|
+
// src/core/define.ts
|
|
108
|
+
function define(fn) {
|
|
109
|
+
return (value) => !!fn(value);
|
|
202
110
|
}
|
|
203
111
|
|
|
204
112
|
// src/utils/object-tags.ts
|
|
@@ -296,6 +204,117 @@ function equalsKey(key, target) {
|
|
|
296
204
|
return (input) => hasMatchingKey(input);
|
|
297
205
|
}
|
|
298
206
|
|
|
207
|
+
// src/core/key.ts
|
|
208
|
+
var hasKey = (key) => define(
|
|
209
|
+
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
210
|
+
);
|
|
211
|
+
var hasKeys = (...keys) => {
|
|
212
|
+
if (keys.length === 0) {
|
|
213
|
+
return define(() => false);
|
|
214
|
+
}
|
|
215
|
+
return define(
|
|
216
|
+
(input) => isObject(input) && keys.every((key) => Object.hasOwn(input, key))
|
|
217
|
+
);
|
|
218
|
+
};
|
|
219
|
+
function narrowKeyTo(guard, key) {
|
|
220
|
+
return (target) => {
|
|
221
|
+
const keyEquals = equalsKey(key, target);
|
|
222
|
+
return define(
|
|
223
|
+
(input) => guard(input) && keyEquals(input)
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/utils/guard-collections.ts
|
|
229
|
+
var everyArrayValue = (values, predicate) => {
|
|
230
|
+
for (const value of values) {
|
|
231
|
+
if (!predicate(value)) return false;
|
|
232
|
+
}
|
|
233
|
+
return true;
|
|
234
|
+
};
|
|
235
|
+
var everyTupleValue = (values, predicates) => {
|
|
236
|
+
if (values.length !== predicates.length) return false;
|
|
237
|
+
for (const [index, predicate] of predicates.entries()) {
|
|
238
|
+
if (!predicate(values[index])) return false;
|
|
239
|
+
}
|
|
240
|
+
return true;
|
|
241
|
+
};
|
|
242
|
+
var everySetValue = (values, predicate) => {
|
|
243
|
+
for (const value of values) {
|
|
244
|
+
if (!predicate(value)) return false;
|
|
245
|
+
}
|
|
246
|
+
return true;
|
|
247
|
+
};
|
|
248
|
+
var everyMapEntry = (values, keyPredicate, valuePredicate) => {
|
|
249
|
+
for (const [key, value] of values) {
|
|
250
|
+
if (!keyPredicate(key) || !valuePredicate(value)) return false;
|
|
251
|
+
}
|
|
252
|
+
return true;
|
|
253
|
+
};
|
|
254
|
+
var everyOwnEnumerableEntry = (values, keyPredicate, valuePredicate) => {
|
|
255
|
+
for (const key of Object.keys(values)) {
|
|
256
|
+
if (!keyPredicate(key) || !valuePredicate(values[key])) return false;
|
|
257
|
+
}
|
|
258
|
+
return true;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// src/utils/to-boolean-predicates.ts
|
|
262
|
+
var toBooleanPredicates = (predicates) => predicates;
|
|
263
|
+
|
|
264
|
+
// src/core/logic.ts
|
|
265
|
+
function and(precondition, condition) {
|
|
266
|
+
return define((input) => {
|
|
267
|
+
if (precondition(input)) {
|
|
268
|
+
return condition(input);
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
function andAll(precondition, ...steps) {
|
|
274
|
+
return define((input) => {
|
|
275
|
+
if (!precondition(input)) return false;
|
|
276
|
+
return applyRefinements(input, steps);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function or(...guards) {
|
|
280
|
+
const predicates = toBooleanPredicates(guards);
|
|
281
|
+
return define(
|
|
282
|
+
(input) => predicates.some((guard) => guard(input))
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
function guardIn() {
|
|
286
|
+
return (guard) => (input) => guard(input);
|
|
287
|
+
}
|
|
288
|
+
function not(fn) {
|
|
289
|
+
return (input) => !fn(input);
|
|
290
|
+
}
|
|
291
|
+
function applyRefinements(value, steps) {
|
|
292
|
+
if (!steps || steps.length === 0) return true;
|
|
293
|
+
for (const step of steps) {
|
|
294
|
+
if (!step(value)) return false;
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/core/nullish.ts
|
|
300
|
+
var allowWhen = (acceptedValue, predicate) => (value) => acceptedValue(value) || predicate(value);
|
|
301
|
+
var requireWhen = (rejectedValue, predicate) => (value) => !rejectedValue(value) && predicate(value);
|
|
302
|
+
function nullable(fn) {
|
|
303
|
+
return allowWhen((value) => value === null, fn);
|
|
304
|
+
}
|
|
305
|
+
function nonNull(fn) {
|
|
306
|
+
return requireWhen((value) => value === null, fn);
|
|
307
|
+
}
|
|
308
|
+
function nullish(fn) {
|
|
309
|
+
return allowWhen((value) => value == null, fn);
|
|
310
|
+
}
|
|
311
|
+
function optional(fn) {
|
|
312
|
+
return allowWhen((value) => value === void 0, fn);
|
|
313
|
+
}
|
|
314
|
+
function required(fn) {
|
|
315
|
+
return requireWhen((value) => value === void 0, fn);
|
|
316
|
+
}
|
|
317
|
+
|
|
299
318
|
// src/core/parse.ts
|
|
300
319
|
function safeParse(fn, value) {
|
|
301
320
|
const valid = fn(value);
|
|
@@ -305,6 +324,9 @@ function safeParseWith(fn) {
|
|
|
305
324
|
return (value) => safeParse(fn, value);
|
|
306
325
|
}
|
|
307
326
|
|
|
327
|
+
// src/core/predicate.ts
|
|
328
|
+
var predicateToRefine = (fn) => (value) => fn(value);
|
|
329
|
+
|
|
308
330
|
// src/core/primitive.ts
|
|
309
331
|
var isString = define((value) => typeof value === "string");
|
|
310
332
|
var isNumberPrimitive = define(
|
|
@@ -352,11 +374,9 @@ function arrayOf(elementGuard) {
|
|
|
352
374
|
(input) => Array.isArray(input) && everyArrayValue(input, elementGuard)
|
|
353
375
|
);
|
|
354
376
|
}
|
|
355
|
-
|
|
356
|
-
// src/core/combinators/set.ts
|
|
357
|
-
function setOf(valueGuard) {
|
|
377
|
+
function nonEmptyArrayOf(elementGuard) {
|
|
358
378
|
return define(
|
|
359
|
-
(input) =>
|
|
379
|
+
(input) => Array.isArray(input) && input.length > 0 && everyArrayValue(input, elementGuard)
|
|
360
380
|
);
|
|
361
381
|
}
|
|
362
382
|
|
|
@@ -367,18 +387,46 @@ function mapOf(keyGuard, valueGuard) {
|
|
|
367
387
|
);
|
|
368
388
|
}
|
|
369
389
|
|
|
370
|
-
// src/core/combinators/tuple.ts
|
|
371
|
-
function tupleOf(...guards) {
|
|
372
|
-
return define(
|
|
373
|
-
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
390
|
// src/core/combinators/one-of.ts
|
|
378
391
|
function oneOf(...guards) {
|
|
379
392
|
return or(...guards);
|
|
380
393
|
}
|
|
381
394
|
|
|
395
|
+
// src/core/combinators/one-of-values.ts
|
|
396
|
+
var ONE_OF_VALUES_LINEAR_SCAN_MAX = 8;
|
|
397
|
+
var isSingleArrayArg = define(
|
|
398
|
+
(value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
|
|
399
|
+
);
|
|
400
|
+
var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
|
|
401
|
+
var createLinearPredicate = (items) => {
|
|
402
|
+
return define((input) => {
|
|
403
|
+
return items.some((value) => Object.is(value, input));
|
|
404
|
+
});
|
|
405
|
+
};
|
|
406
|
+
var createSetPredicate = (items) => {
|
|
407
|
+
let hasPositiveZero = false;
|
|
408
|
+
let hasNegativeZero = false;
|
|
409
|
+
const valueSet = /* @__PURE__ */ new Set();
|
|
410
|
+
for (const literalValue of items) {
|
|
411
|
+
if (isZero(literalValue)) {
|
|
412
|
+
if (Object.is(literalValue, -0)) hasNegativeZero = true;
|
|
413
|
+
else hasPositiveZero = true;
|
|
414
|
+
} else {
|
|
415
|
+
valueSet.add(literalValue);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return define((input) => {
|
|
419
|
+
if (isZero(input)) {
|
|
420
|
+
return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
|
|
421
|
+
}
|
|
422
|
+
return valueSet.has(input);
|
|
423
|
+
});
|
|
424
|
+
};
|
|
425
|
+
function oneOfValues(...args) {
|
|
426
|
+
const items = normalizeValues(args);
|
|
427
|
+
return items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX ? createLinearPredicate(items) : createSetPredicate(items);
|
|
428
|
+
}
|
|
429
|
+
|
|
382
430
|
// src/core/combinators/record.ts
|
|
383
431
|
function recordOf(keyFunction, valueFunction) {
|
|
384
432
|
return define(
|
|
@@ -386,6 +434,13 @@ function recordOf(keyFunction, valueFunction) {
|
|
|
386
434
|
);
|
|
387
435
|
}
|
|
388
436
|
|
|
437
|
+
// src/core/combinators/set.ts
|
|
438
|
+
function setOf(valueGuard) {
|
|
439
|
+
return define(
|
|
440
|
+
(input) => isSet(input) && everySetValue(input, valueGuard)
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
|
|
389
444
|
// src/core/combinators/struct.ts
|
|
390
445
|
var isOptionalSchemaField = define(
|
|
391
446
|
// WHY: Optional fields are represented as a small tagged object so `struct`
|
|
@@ -429,60 +484,11 @@ function struct(schema, options) {
|
|
|
429
484
|
});
|
|
430
485
|
}
|
|
431
486
|
|
|
432
|
-
// src/core/combinators/
|
|
433
|
-
|
|
434
|
-
var isSingleArrayArg = define(
|
|
435
|
-
(value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
|
|
436
|
-
);
|
|
437
|
-
var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
|
|
438
|
-
var createLinearPredicate = (items) => {
|
|
439
|
-
return define((input) => {
|
|
440
|
-
return items.some((value) => Object.is(value, input));
|
|
441
|
-
});
|
|
442
|
-
};
|
|
443
|
-
var createSetPredicate = (items) => {
|
|
444
|
-
let hasPositiveZero = false;
|
|
445
|
-
let hasNegativeZero = false;
|
|
446
|
-
const valueSet = /* @__PURE__ */ new Set();
|
|
447
|
-
for (const literalValue of items) {
|
|
448
|
-
if (isZero(literalValue)) {
|
|
449
|
-
if (Object.is(literalValue, -0)) hasNegativeZero = true;
|
|
450
|
-
else hasPositiveZero = true;
|
|
451
|
-
} else {
|
|
452
|
-
valueSet.add(literalValue);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
return define((input) => {
|
|
456
|
-
if (isZero(input)) {
|
|
457
|
-
return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
|
|
458
|
-
}
|
|
459
|
-
return valueSet.has(input);
|
|
460
|
-
});
|
|
461
|
-
};
|
|
462
|
-
function oneOfValues(...args) {
|
|
463
|
-
const items = normalizeValues(args);
|
|
464
|
-
return items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX ? createLinearPredicate(items) : createSetPredicate(items);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
// src/core/key.ts
|
|
468
|
-
var hasKey = (key) => define(
|
|
469
|
-
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
470
|
-
);
|
|
471
|
-
var hasKeys = (...keys) => {
|
|
472
|
-
if (keys.length === 0) {
|
|
473
|
-
return define(() => false);
|
|
474
|
-
}
|
|
487
|
+
// src/core/combinators/tuple.ts
|
|
488
|
+
function tupleOf(...guards) {
|
|
475
489
|
return define(
|
|
476
|
-
(input) =>
|
|
490
|
+
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
477
491
|
);
|
|
478
|
-
};
|
|
479
|
-
function narrowKeyTo(guard, key) {
|
|
480
|
-
return (target) => {
|
|
481
|
-
const keyEquals = equalsKey(key, target);
|
|
482
|
-
return define(
|
|
483
|
-
(input) => guard(input) && keyEquals(input)
|
|
484
|
-
);
|
|
485
|
-
};
|
|
486
492
|
}
|
|
487
493
|
// Annotate the CommonJS export names for ESM import in node:
|
|
488
494
|
0 && (module.exports = {
|
|
@@ -541,6 +547,7 @@ function narrowKeyTo(guard, key) {
|
|
|
541
547
|
isZero,
|
|
542
548
|
mapOf,
|
|
543
549
|
narrowKeyTo,
|
|
550
|
+
nonEmptyArrayOf,
|
|
544
551
|
nonNull,
|
|
545
552
|
not,
|
|
546
553
|
nullable,
|