is-kit 1.6.3 → 1.6.4
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/dist/index.d.mts +44 -1
- package/dist/index.d.ts +44 -1
- package/dist/index.js +71 -50
- package/dist/index.mjs +66 -50
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -555,6 +555,49 @@ declare const hasKeys: <const KS extends readonly [PropertyKey, ...PropertyKey[]
|
|
|
555
555
|
*/
|
|
556
556
|
declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <const T extends A[K]>(target: T) => Predicate<A & Record<K, T>>;
|
|
557
557
|
|
|
558
|
+
/**
|
|
559
|
+
* Checks whether every array element satisfies the provided predicate.
|
|
560
|
+
*
|
|
561
|
+
* @param values Array values to validate.
|
|
562
|
+
* @param predicate Predicate applied to each element.
|
|
563
|
+
* @returns Whether all elements satisfy `predicate`.
|
|
564
|
+
*/
|
|
565
|
+
declare const everyArrayValue: (values: readonly unknown[], predicate: (value: unknown) => boolean) => boolean;
|
|
566
|
+
/**
|
|
567
|
+
* Checks whether a tuple matches the provided element predicates in order.
|
|
568
|
+
*
|
|
569
|
+
* @param values Tuple candidate values.
|
|
570
|
+
* @param predicates Predicates aligned to each tuple index.
|
|
571
|
+
* @returns Whether lengths match and each index passes.
|
|
572
|
+
*/
|
|
573
|
+
declare const everyTupleValue: (values: readonly unknown[], predicates: readonly ((value: unknown) => boolean)[]) => boolean;
|
|
574
|
+
/**
|
|
575
|
+
* Checks whether every set value satisfies the provided predicate.
|
|
576
|
+
*
|
|
577
|
+
* @param values Set values to validate.
|
|
578
|
+
* @param predicate Predicate applied to each value.
|
|
579
|
+
* @returns Whether all set values satisfy `predicate`.
|
|
580
|
+
*/
|
|
581
|
+
declare const everySetValue: (values: ReadonlySet<unknown>, predicate: (value: unknown) => boolean) => boolean;
|
|
582
|
+
/**
|
|
583
|
+
* Checks whether every map entry satisfies the provided key and value predicates.
|
|
584
|
+
*
|
|
585
|
+
* @param values Map values to validate.
|
|
586
|
+
* @param keyPredicate Predicate applied to each key.
|
|
587
|
+
* @param valuePredicate Predicate applied to each value.
|
|
588
|
+
* @returns Whether all keys and values satisfy their predicates.
|
|
589
|
+
*/
|
|
590
|
+
declare const everyMapEntry: (values: ReadonlyMap<unknown, unknown>, keyPredicate: (key: unknown) => boolean, valuePredicate: (value: unknown) => boolean) => boolean;
|
|
591
|
+
/**
|
|
592
|
+
* Checks whether every own enumerable string key/value pair satisfies the provided predicates.
|
|
593
|
+
*
|
|
594
|
+
* @param values Plain object values to validate.
|
|
595
|
+
* @param keyPredicate Predicate applied to each enumerable own string key.
|
|
596
|
+
* @param valuePredicate Predicate applied to each corresponding value.
|
|
597
|
+
* @returns Whether all enumerable entries satisfy their predicates.
|
|
598
|
+
*/
|
|
599
|
+
declare const everyOwnEnumerableEntry: (values: Record<string, unknown>, keyPredicate: (key: string) => boolean, valuePredicate: (value: unknown) => boolean) => boolean;
|
|
600
|
+
|
|
558
601
|
/**
|
|
559
602
|
* Converts predicates to plain boolean predicates for iteration helpers.
|
|
560
603
|
*
|
|
@@ -563,4 +606,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
563
606
|
*/
|
|
564
607
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
565
608
|
|
|
566
|
-
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OptionalSchemaField, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, type SchemaField, and, andAll, arrayOf, assert, define, equals, equalsBy, equalsKey, guardIn, hasKey, hasKeys, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, mapOf, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf };
|
|
609
|
+
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OptionalSchemaField, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, type SchemaField, and, andAll, arrayOf, assert, define, equals, equalsBy, equalsKey, everyArrayValue, everyMapEntry, everyOwnEnumerableEntry, everySetValue, everyTupleValue, guardIn, hasKey, hasKeys, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, mapOf, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf };
|
package/dist/index.d.ts
CHANGED
|
@@ -555,6 +555,49 @@ declare const hasKeys: <const KS extends readonly [PropertyKey, ...PropertyKey[]
|
|
|
555
555
|
*/
|
|
556
556
|
declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <const T extends A[K]>(target: T) => Predicate<A & Record<K, T>>;
|
|
557
557
|
|
|
558
|
+
/**
|
|
559
|
+
* Checks whether every array element satisfies the provided predicate.
|
|
560
|
+
*
|
|
561
|
+
* @param values Array values to validate.
|
|
562
|
+
* @param predicate Predicate applied to each element.
|
|
563
|
+
* @returns Whether all elements satisfy `predicate`.
|
|
564
|
+
*/
|
|
565
|
+
declare const everyArrayValue: (values: readonly unknown[], predicate: (value: unknown) => boolean) => boolean;
|
|
566
|
+
/**
|
|
567
|
+
* Checks whether a tuple matches the provided element predicates in order.
|
|
568
|
+
*
|
|
569
|
+
* @param values Tuple candidate values.
|
|
570
|
+
* @param predicates Predicates aligned to each tuple index.
|
|
571
|
+
* @returns Whether lengths match and each index passes.
|
|
572
|
+
*/
|
|
573
|
+
declare const everyTupleValue: (values: readonly unknown[], predicates: readonly ((value: unknown) => boolean)[]) => boolean;
|
|
574
|
+
/**
|
|
575
|
+
* Checks whether every set value satisfies the provided predicate.
|
|
576
|
+
*
|
|
577
|
+
* @param values Set values to validate.
|
|
578
|
+
* @param predicate Predicate applied to each value.
|
|
579
|
+
* @returns Whether all set values satisfy `predicate`.
|
|
580
|
+
*/
|
|
581
|
+
declare const everySetValue: (values: ReadonlySet<unknown>, predicate: (value: unknown) => boolean) => boolean;
|
|
582
|
+
/**
|
|
583
|
+
* Checks whether every map entry satisfies the provided key and value predicates.
|
|
584
|
+
*
|
|
585
|
+
* @param values Map values to validate.
|
|
586
|
+
* @param keyPredicate Predicate applied to each key.
|
|
587
|
+
* @param valuePredicate Predicate applied to each value.
|
|
588
|
+
* @returns Whether all keys and values satisfy their predicates.
|
|
589
|
+
*/
|
|
590
|
+
declare const everyMapEntry: (values: ReadonlyMap<unknown, unknown>, keyPredicate: (key: unknown) => boolean, valuePredicate: (value: unknown) => boolean) => boolean;
|
|
591
|
+
/**
|
|
592
|
+
* Checks whether every own enumerable string key/value pair satisfies the provided predicates.
|
|
593
|
+
*
|
|
594
|
+
* @param values Plain object values to validate.
|
|
595
|
+
* @param keyPredicate Predicate applied to each enumerable own string key.
|
|
596
|
+
* @param valuePredicate Predicate applied to each corresponding value.
|
|
597
|
+
* @returns Whether all enumerable entries satisfy their predicates.
|
|
598
|
+
*/
|
|
599
|
+
declare const everyOwnEnumerableEntry: (values: Record<string, unknown>, keyPredicate: (key: string) => boolean, valuePredicate: (value: unknown) => boolean) => boolean;
|
|
600
|
+
|
|
558
601
|
/**
|
|
559
602
|
* Converts predicates to plain boolean predicates for iteration helpers.
|
|
560
603
|
*
|
|
@@ -563,4 +606,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
563
606
|
*/
|
|
564
607
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
565
608
|
|
|
566
|
-
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OptionalSchemaField, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, type SchemaField, and, andAll, arrayOf, assert, define, equals, equalsBy, equalsKey, guardIn, hasKey, hasKeys, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, mapOf, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf };
|
|
609
|
+
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OptionalSchemaField, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, type SchemaField, and, andAll, arrayOf, assert, define, equals, equalsBy, equalsKey, everyArrayValue, everyMapEntry, everyOwnEnumerableEntry, everySetValue, everyTupleValue, guardIn, hasKey, hasKeys, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, mapOf, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf };
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,11 @@ __export(index_exports, {
|
|
|
28
28
|
equals: () => equals,
|
|
29
29
|
equalsBy: () => equalsBy,
|
|
30
30
|
equalsKey: () => equalsKey,
|
|
31
|
+
everyArrayValue: () => everyArrayValue,
|
|
32
|
+
everyMapEntry: () => everyMapEntry,
|
|
33
|
+
everyOwnEnumerableEntry: () => everyOwnEnumerableEntry,
|
|
34
|
+
everySetValue: () => everySetValue,
|
|
35
|
+
everyTupleValue: () => everyTupleValue,
|
|
31
36
|
guardIn: () => guardIn,
|
|
32
37
|
hasKey: () => hasKey,
|
|
33
38
|
hasKeys: () => hasKeys,
|
|
@@ -106,6 +111,39 @@ function assert(fn, value, message) {
|
|
|
106
111
|
throw new Error(message ?? DEFAULT_ASSERT_MESSAGE);
|
|
107
112
|
}
|
|
108
113
|
|
|
114
|
+
// src/utils/guard-collections.ts
|
|
115
|
+
var everyArrayValue = (values, predicate) => {
|
|
116
|
+
for (const value of values) {
|
|
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
|
+
|
|
109
147
|
// src/utils/to-boolean-predicates.ts
|
|
110
148
|
var toBooleanPredicates = (predicates) => predicates;
|
|
111
149
|
|
|
@@ -145,20 +183,22 @@ function applyRefinements(value, steps) {
|
|
|
145
183
|
}
|
|
146
184
|
|
|
147
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);
|
|
148
188
|
function nullable(fn) {
|
|
149
|
-
return (value) => value === null
|
|
189
|
+
return allowWhen((value) => value === null, fn);
|
|
150
190
|
}
|
|
151
191
|
function nonNull(fn) {
|
|
152
|
-
return (value) => value
|
|
192
|
+
return requireWhen((value) => value === null, fn);
|
|
153
193
|
}
|
|
154
194
|
function nullish(fn) {
|
|
155
|
-
return (value) => value == null
|
|
195
|
+
return allowWhen((value) => value == null, fn);
|
|
156
196
|
}
|
|
157
197
|
function optional(fn) {
|
|
158
|
-
return (value) => value === void 0
|
|
198
|
+
return allowWhen((value) => value === void 0, fn);
|
|
159
199
|
}
|
|
160
200
|
function required(fn) {
|
|
161
|
-
return (value) => value
|
|
201
|
+
return requireWhen((value) => value === void 0, fn);
|
|
162
202
|
}
|
|
163
203
|
|
|
164
204
|
// src/utils/object-tags.ts
|
|
@@ -251,7 +291,7 @@ function equalsBy(guard, selector) {
|
|
|
251
291
|
}
|
|
252
292
|
function equalsKey(key, target) {
|
|
253
293
|
const hasMatchingKey = define((input) => {
|
|
254
|
-
return isObject(input) && Object.
|
|
294
|
+
return isObject(input) && Object.hasOwn(input, key) && Object.is(input[key], target);
|
|
255
295
|
});
|
|
256
296
|
return (input) => hasMatchingKey(input);
|
|
257
297
|
}
|
|
@@ -308,69 +348,45 @@ var isPrimitive = or(
|
|
|
308
348
|
|
|
309
349
|
// src/core/combinators/array.ts
|
|
310
350
|
function arrayOf(elementGuard) {
|
|
311
|
-
return define(
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (!elementGuard(element)) return false;
|
|
315
|
-
}
|
|
316
|
-
return true;
|
|
317
|
-
});
|
|
351
|
+
return define(
|
|
352
|
+
(input) => Array.isArray(input) && everyArrayValue(input, elementGuard)
|
|
353
|
+
);
|
|
318
354
|
}
|
|
319
355
|
|
|
320
356
|
// src/core/combinators/set.ts
|
|
321
357
|
function setOf(valueGuard) {
|
|
322
|
-
return define(
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
if (!valueGuard(value)) return false;
|
|
326
|
-
}
|
|
327
|
-
return true;
|
|
328
|
-
});
|
|
358
|
+
return define(
|
|
359
|
+
(input) => isSet(input) && everySetValue(input, valueGuard)
|
|
360
|
+
);
|
|
329
361
|
}
|
|
330
362
|
|
|
331
363
|
// src/core/combinators/map.ts
|
|
332
364
|
function mapOf(keyGuard, valueGuard) {
|
|
333
|
-
return define(
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
if (!keyGuard(key)) return false;
|
|
337
|
-
if (!valueGuard(value)) return false;
|
|
338
|
-
}
|
|
339
|
-
return true;
|
|
340
|
-
});
|
|
365
|
+
return define(
|
|
366
|
+
(input) => isMap(input) && everyMapEntry(input, keyGuard, valueGuard)
|
|
367
|
+
);
|
|
341
368
|
}
|
|
342
369
|
|
|
343
370
|
// src/core/combinators/tuple.ts
|
|
344
371
|
function tupleOf(...guards) {
|
|
345
|
-
return define(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
);
|
|
349
|
-
});
|
|
372
|
+
return define(
|
|
373
|
+
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
374
|
+
);
|
|
350
375
|
}
|
|
351
376
|
|
|
352
377
|
// src/core/combinators/one-of.ts
|
|
353
378
|
function oneOf(...guards) {
|
|
354
|
-
|
|
355
|
-
return (input) => predicates.some((guard) => guard(input));
|
|
379
|
+
return or(...guards);
|
|
356
380
|
}
|
|
357
381
|
|
|
358
382
|
// src/core/combinators/record.ts
|
|
359
383
|
function recordOf(keyFunction, valueFunction) {
|
|
360
|
-
return (
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
for (const key of Object.keys(obj)) {
|
|
364
|
-
if (!keyFunction(key)) return false;
|
|
365
|
-
const value = obj[key];
|
|
366
|
-
if (!valueFunction(value)) return false;
|
|
367
|
-
}
|
|
368
|
-
return true;
|
|
369
|
-
};
|
|
384
|
+
return define(
|
|
385
|
+
(input) => isPlainObject(input) && everyOwnEnumerableEntry(input, keyFunction, valueFunction)
|
|
386
|
+
);
|
|
370
387
|
}
|
|
371
388
|
|
|
372
389
|
// src/core/combinators/struct.ts
|
|
373
|
-
var hasOwnKey = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
374
390
|
var isOptionalSchemaField = define(
|
|
375
391
|
// WHY: Optional fields are represented as a small tagged object so `struct`
|
|
376
392
|
// can distinguish schema metadata from plain predicate functions up front.
|
|
@@ -379,12 +395,12 @@ var isOptionalSchemaField = define(
|
|
|
379
395
|
var hasRequiredKeys = (obj, entries) => (
|
|
380
396
|
// WHY: Required fields must be own properties; inherited values should not
|
|
381
397
|
// satisfy a schema because `struct` models object payload shape, not prototype chains.
|
|
382
|
-
entries.every(([key, guard]) =>
|
|
398
|
+
entries.every(([key, guard]) => Object.hasOwn(obj, key) && guard(obj[key]))
|
|
383
399
|
);
|
|
384
400
|
var hasValidOptionalKeys = (obj, entries) => (
|
|
385
401
|
// WHY: Optional keys are validated only when present. Missing keys stay valid
|
|
386
402
|
// without forcing callers to encode `undefined` into the value guard.
|
|
387
|
-
entries.every(([key, guard]) => !
|
|
403
|
+
entries.every(([key, guard]) => !Object.hasOwn(obj, key) || guard(obj[key]))
|
|
388
404
|
);
|
|
389
405
|
var hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
|
|
390
406
|
function optionalKey(guard) {
|
|
@@ -450,14 +466,14 @@ function oneOfValues(...args) {
|
|
|
450
466
|
|
|
451
467
|
// src/core/key.ts
|
|
452
468
|
var hasKey = (key) => define(
|
|
453
|
-
(input) => isObject(input) && Object.
|
|
469
|
+
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
454
470
|
);
|
|
455
471
|
var hasKeys = (...keys) => {
|
|
456
472
|
if (keys.length === 0) {
|
|
457
473
|
return define(() => false);
|
|
458
474
|
}
|
|
459
475
|
return define(
|
|
460
|
-
(input) => isObject(input) && keys.every((key) => Object.
|
|
476
|
+
(input) => isObject(input) && keys.every((key) => Object.hasOwn(input, key))
|
|
461
477
|
);
|
|
462
478
|
};
|
|
463
479
|
function narrowKeyTo(guard, key) {
|
|
@@ -478,6 +494,11 @@ function narrowKeyTo(guard, key) {
|
|
|
478
494
|
equals,
|
|
479
495
|
equalsBy,
|
|
480
496
|
equalsKey,
|
|
497
|
+
everyArrayValue,
|
|
498
|
+
everyMapEntry,
|
|
499
|
+
everyOwnEnumerableEntry,
|
|
500
|
+
everySetValue,
|
|
501
|
+
everyTupleValue,
|
|
481
502
|
guardIn,
|
|
482
503
|
hasKey,
|
|
483
504
|
hasKeys,
|
package/dist/index.mjs
CHANGED
|
@@ -13,6 +13,39 @@ function assert(fn, value, message) {
|
|
|
13
13
|
throw new Error(message ?? DEFAULT_ASSERT_MESSAGE);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
// src/utils/guard-collections.ts
|
|
17
|
+
var everyArrayValue = (values, predicate) => {
|
|
18
|
+
for (const value of values) {
|
|
19
|
+
if (!predicate(value)) return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
var everyTupleValue = (values, predicates) => {
|
|
24
|
+
if (values.length !== predicates.length) return false;
|
|
25
|
+
for (const [index, predicate] of predicates.entries()) {
|
|
26
|
+
if (!predicate(values[index])) return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
};
|
|
30
|
+
var everySetValue = (values, predicate) => {
|
|
31
|
+
for (const value of values) {
|
|
32
|
+
if (!predicate(value)) return false;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
};
|
|
36
|
+
var everyMapEntry = (values, keyPredicate, valuePredicate) => {
|
|
37
|
+
for (const [key, value] of values) {
|
|
38
|
+
if (!keyPredicate(key) || !valuePredicate(value)) return false;
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
};
|
|
42
|
+
var everyOwnEnumerableEntry = (values, keyPredicate, valuePredicate) => {
|
|
43
|
+
for (const key of Object.keys(values)) {
|
|
44
|
+
if (!keyPredicate(key) || !valuePredicate(values[key])) return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
};
|
|
48
|
+
|
|
16
49
|
// src/utils/to-boolean-predicates.ts
|
|
17
50
|
var toBooleanPredicates = (predicates) => predicates;
|
|
18
51
|
|
|
@@ -52,20 +85,22 @@ function applyRefinements(value, steps) {
|
|
|
52
85
|
}
|
|
53
86
|
|
|
54
87
|
// src/core/nullish.ts
|
|
88
|
+
var allowWhen = (acceptedValue, predicate) => (value) => acceptedValue(value) || predicate(value);
|
|
89
|
+
var requireWhen = (rejectedValue, predicate) => (value) => !rejectedValue(value) && predicate(value);
|
|
55
90
|
function nullable(fn) {
|
|
56
|
-
return (value) => value === null
|
|
91
|
+
return allowWhen((value) => value === null, fn);
|
|
57
92
|
}
|
|
58
93
|
function nonNull(fn) {
|
|
59
|
-
return (value) => value
|
|
94
|
+
return requireWhen((value) => value === null, fn);
|
|
60
95
|
}
|
|
61
96
|
function nullish(fn) {
|
|
62
|
-
return (value) => value == null
|
|
97
|
+
return allowWhen((value) => value == null, fn);
|
|
63
98
|
}
|
|
64
99
|
function optional(fn) {
|
|
65
|
-
return (value) => value === void 0
|
|
100
|
+
return allowWhen((value) => value === void 0, fn);
|
|
66
101
|
}
|
|
67
102
|
function required(fn) {
|
|
68
|
-
return (value) => value
|
|
103
|
+
return requireWhen((value) => value === void 0, fn);
|
|
69
104
|
}
|
|
70
105
|
|
|
71
106
|
// src/utils/object-tags.ts
|
|
@@ -158,7 +193,7 @@ function equalsBy(guard, selector) {
|
|
|
158
193
|
}
|
|
159
194
|
function equalsKey(key, target) {
|
|
160
195
|
const hasMatchingKey = define((input) => {
|
|
161
|
-
return isObject(input) && Object.
|
|
196
|
+
return isObject(input) && Object.hasOwn(input, key) && Object.is(input[key], target);
|
|
162
197
|
});
|
|
163
198
|
return (input) => hasMatchingKey(input);
|
|
164
199
|
}
|
|
@@ -215,69 +250,45 @@ var isPrimitive = or(
|
|
|
215
250
|
|
|
216
251
|
// src/core/combinators/array.ts
|
|
217
252
|
function arrayOf(elementGuard) {
|
|
218
|
-
return define(
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
if (!elementGuard(element)) return false;
|
|
222
|
-
}
|
|
223
|
-
return true;
|
|
224
|
-
});
|
|
253
|
+
return define(
|
|
254
|
+
(input) => Array.isArray(input) && everyArrayValue(input, elementGuard)
|
|
255
|
+
);
|
|
225
256
|
}
|
|
226
257
|
|
|
227
258
|
// src/core/combinators/set.ts
|
|
228
259
|
function setOf(valueGuard) {
|
|
229
|
-
return define(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if (!valueGuard(value)) return false;
|
|
233
|
-
}
|
|
234
|
-
return true;
|
|
235
|
-
});
|
|
260
|
+
return define(
|
|
261
|
+
(input) => isSet(input) && everySetValue(input, valueGuard)
|
|
262
|
+
);
|
|
236
263
|
}
|
|
237
264
|
|
|
238
265
|
// src/core/combinators/map.ts
|
|
239
266
|
function mapOf(keyGuard, valueGuard) {
|
|
240
|
-
return define(
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
if (!keyGuard(key)) return false;
|
|
244
|
-
if (!valueGuard(value)) return false;
|
|
245
|
-
}
|
|
246
|
-
return true;
|
|
247
|
-
});
|
|
267
|
+
return define(
|
|
268
|
+
(input) => isMap(input) && everyMapEntry(input, keyGuard, valueGuard)
|
|
269
|
+
);
|
|
248
270
|
}
|
|
249
271
|
|
|
250
272
|
// src/core/combinators/tuple.ts
|
|
251
273
|
function tupleOf(...guards) {
|
|
252
|
-
return define(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
);
|
|
256
|
-
});
|
|
274
|
+
return define(
|
|
275
|
+
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
276
|
+
);
|
|
257
277
|
}
|
|
258
278
|
|
|
259
279
|
// src/core/combinators/one-of.ts
|
|
260
280
|
function oneOf(...guards) {
|
|
261
|
-
|
|
262
|
-
return (input) => predicates.some((guard) => guard(input));
|
|
281
|
+
return or(...guards);
|
|
263
282
|
}
|
|
264
283
|
|
|
265
284
|
// src/core/combinators/record.ts
|
|
266
285
|
function recordOf(keyFunction, valueFunction) {
|
|
267
|
-
return (
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
for (const key of Object.keys(obj)) {
|
|
271
|
-
if (!keyFunction(key)) return false;
|
|
272
|
-
const value = obj[key];
|
|
273
|
-
if (!valueFunction(value)) return false;
|
|
274
|
-
}
|
|
275
|
-
return true;
|
|
276
|
-
};
|
|
286
|
+
return define(
|
|
287
|
+
(input) => isPlainObject(input) && everyOwnEnumerableEntry(input, keyFunction, valueFunction)
|
|
288
|
+
);
|
|
277
289
|
}
|
|
278
290
|
|
|
279
291
|
// src/core/combinators/struct.ts
|
|
280
|
-
var hasOwnKey = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
281
292
|
var isOptionalSchemaField = define(
|
|
282
293
|
// WHY: Optional fields are represented as a small tagged object so `struct`
|
|
283
294
|
// can distinguish schema metadata from plain predicate functions up front.
|
|
@@ -286,12 +297,12 @@ var isOptionalSchemaField = define(
|
|
|
286
297
|
var hasRequiredKeys = (obj, entries) => (
|
|
287
298
|
// WHY: Required fields must be own properties; inherited values should not
|
|
288
299
|
// satisfy a schema because `struct` models object payload shape, not prototype chains.
|
|
289
|
-
entries.every(([key, guard]) =>
|
|
300
|
+
entries.every(([key, guard]) => Object.hasOwn(obj, key) && guard(obj[key]))
|
|
290
301
|
);
|
|
291
302
|
var hasValidOptionalKeys = (obj, entries) => (
|
|
292
303
|
// WHY: Optional keys are validated only when present. Missing keys stay valid
|
|
293
304
|
// without forcing callers to encode `undefined` into the value guard.
|
|
294
|
-
entries.every(([key, guard]) => !
|
|
305
|
+
entries.every(([key, guard]) => !Object.hasOwn(obj, key) || guard(obj[key]))
|
|
295
306
|
);
|
|
296
307
|
var hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
|
|
297
308
|
function optionalKey(guard) {
|
|
@@ -357,14 +368,14 @@ function oneOfValues(...args) {
|
|
|
357
368
|
|
|
358
369
|
// src/core/key.ts
|
|
359
370
|
var hasKey = (key) => define(
|
|
360
|
-
(input) => isObject(input) && Object.
|
|
371
|
+
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
361
372
|
);
|
|
362
373
|
var hasKeys = (...keys) => {
|
|
363
374
|
if (keys.length === 0) {
|
|
364
375
|
return define(() => false);
|
|
365
376
|
}
|
|
366
377
|
return define(
|
|
367
|
-
(input) => isObject(input) && keys.every((key) => Object.
|
|
378
|
+
(input) => isObject(input) && keys.every((key) => Object.hasOwn(input, key))
|
|
368
379
|
);
|
|
369
380
|
};
|
|
370
381
|
function narrowKeyTo(guard, key) {
|
|
@@ -384,6 +395,11 @@ export {
|
|
|
384
395
|
equals,
|
|
385
396
|
equalsBy,
|
|
386
397
|
equalsKey,
|
|
398
|
+
everyArrayValue,
|
|
399
|
+
everyMapEntry,
|
|
400
|
+
everyOwnEnumerableEntry,
|
|
401
|
+
everySetValue,
|
|
402
|
+
everyTupleValue,
|
|
387
403
|
guardIn,
|
|
388
404
|
hasKey,
|
|
389
405
|
hasKeys,
|