is-kit 1.6.2 → 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 +97 -64
- package/dist/index.mjs +92 -64
- 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,24 +183,38 @@ 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
|
|
|
204
|
+
// src/utils/object-tags.ts
|
|
205
|
+
var objectToString = Object.prototype.toString;
|
|
206
|
+
var OBJECT_TAG_DATE = "[object Date]";
|
|
207
|
+
var OBJECT_TAG_REGEXP = "[object RegExp]";
|
|
208
|
+
var OBJECT_TAG_MAP = "[object Map]";
|
|
209
|
+
var OBJECT_TAG_SET = "[object Set]";
|
|
210
|
+
var OBJECT_TAG_WEAK_MAP = "[object WeakMap]";
|
|
211
|
+
var OBJECT_TAG_WEAK_SET = "[object WeakSet]";
|
|
212
|
+
var OBJECT_TAG_ARRAY_BUFFER = "[object ArrayBuffer]";
|
|
213
|
+
var OBJECT_TAG_DATA_VIEW = "[object DataView]";
|
|
214
|
+
var OBJECT_TAG_ERROR = "[object Error]";
|
|
215
|
+
var getTag = (value) => objectToString.call(value);
|
|
216
|
+
|
|
164
217
|
// src/core/object.ts
|
|
165
|
-
var getTag = (value) => Object.prototype.toString.call(value);
|
|
166
218
|
var isFunction = define(
|
|
167
219
|
(value) => typeof value === "function"
|
|
168
220
|
);
|
|
@@ -176,46 +228,46 @@ var isPlainObject = define((value) => {
|
|
|
176
228
|
});
|
|
177
229
|
var isArray = define(Array.isArray);
|
|
178
230
|
var isDate = define(
|
|
179
|
-
(value) => getTag(value) ===
|
|
231
|
+
(value) => getTag(value) === OBJECT_TAG_DATE && !Number.isNaN(value.getTime())
|
|
180
232
|
);
|
|
181
233
|
var isRegExp = define(
|
|
182
|
-
(value) => getTag(value) ===
|
|
234
|
+
(value) => getTag(value) === OBJECT_TAG_REGEXP
|
|
183
235
|
);
|
|
184
236
|
var isMap = define(
|
|
185
|
-
(value) => getTag(value) ===
|
|
237
|
+
(value) => getTag(value) === OBJECT_TAG_MAP
|
|
186
238
|
);
|
|
187
239
|
var isSet = define(
|
|
188
|
-
(value) => getTag(value) ===
|
|
240
|
+
(value) => getTag(value) === OBJECT_TAG_SET
|
|
189
241
|
);
|
|
190
242
|
var isWeakMap = define(
|
|
191
|
-
(value) => getTag(value) ===
|
|
243
|
+
(value) => getTag(value) === OBJECT_TAG_WEAK_MAP
|
|
192
244
|
);
|
|
193
245
|
var isWeakSet = define(
|
|
194
|
-
(value) => getTag(value) ===
|
|
246
|
+
(value) => getTag(value) === OBJECT_TAG_WEAK_SET
|
|
195
247
|
);
|
|
196
248
|
var isPromiseLike = define((value) => {
|
|
197
249
|
if (!isObject(value) && !isFunction(value)) return false;
|
|
198
|
-
return
|
|
250
|
+
return isFunction(value.then);
|
|
199
251
|
});
|
|
200
252
|
var isIterable = define((value) => {
|
|
201
253
|
if (!isObject(value) && !isFunction(value)) return false;
|
|
202
|
-
return
|
|
254
|
+
return isFunction(value[Symbol.iterator]);
|
|
203
255
|
});
|
|
204
256
|
var isAsyncIterable = define((value) => {
|
|
205
257
|
if (!isObject(value) && !isFunction(value)) return false;
|
|
206
|
-
return
|
|
258
|
+
return isFunction(value[Symbol.asyncIterator]);
|
|
207
259
|
});
|
|
208
260
|
var isArrayBuffer = define(
|
|
209
|
-
(value) => getTag(value) ===
|
|
261
|
+
(value) => getTag(value) === OBJECT_TAG_ARRAY_BUFFER
|
|
210
262
|
);
|
|
211
263
|
var isDataView = define(
|
|
212
|
-
(value) => getTag(value) ===
|
|
264
|
+
(value) => getTag(value) === OBJECT_TAG_DATA_VIEW
|
|
213
265
|
);
|
|
214
266
|
var isTypedArray = define(
|
|
215
|
-
(value) => ArrayBuffer.isView(value) && getTag(value) !==
|
|
267
|
+
(value) => ArrayBuffer.isView(value) && getTag(value) !== OBJECT_TAG_DATA_VIEW
|
|
216
268
|
);
|
|
217
269
|
var isError = define(
|
|
218
|
-
(value) => value instanceof Error || getTag(value) ===
|
|
270
|
+
(value) => value instanceof Error || getTag(value) === OBJECT_TAG_ERROR
|
|
219
271
|
);
|
|
220
272
|
var isURL = typeof URL !== "undefined" ? define((value) => value instanceof URL) : define(() => false);
|
|
221
273
|
var isBlob = typeof Blob !== "undefined" ? define((value) => value instanceof Blob) : define(() => false);
|
|
@@ -239,7 +291,7 @@ function equalsBy(guard, selector) {
|
|
|
239
291
|
}
|
|
240
292
|
function equalsKey(key, target) {
|
|
241
293
|
const hasMatchingKey = define((input) => {
|
|
242
|
-
return isObject(input) && Object.
|
|
294
|
+
return isObject(input) && Object.hasOwn(input, key) && Object.is(input[key], target);
|
|
243
295
|
});
|
|
244
296
|
return (input) => hasMatchingKey(input);
|
|
245
297
|
}
|
|
@@ -296,69 +348,45 @@ var isPrimitive = or(
|
|
|
296
348
|
|
|
297
349
|
// src/core/combinators/array.ts
|
|
298
350
|
function arrayOf(elementGuard) {
|
|
299
|
-
return define(
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (!elementGuard(element)) return false;
|
|
303
|
-
}
|
|
304
|
-
return true;
|
|
305
|
-
});
|
|
351
|
+
return define(
|
|
352
|
+
(input) => Array.isArray(input) && everyArrayValue(input, elementGuard)
|
|
353
|
+
);
|
|
306
354
|
}
|
|
307
355
|
|
|
308
356
|
// src/core/combinators/set.ts
|
|
309
357
|
function setOf(valueGuard) {
|
|
310
|
-
return define(
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
if (!valueGuard(value)) return false;
|
|
314
|
-
}
|
|
315
|
-
return true;
|
|
316
|
-
});
|
|
358
|
+
return define(
|
|
359
|
+
(input) => isSet(input) && everySetValue(input, valueGuard)
|
|
360
|
+
);
|
|
317
361
|
}
|
|
318
362
|
|
|
319
363
|
// src/core/combinators/map.ts
|
|
320
364
|
function mapOf(keyGuard, valueGuard) {
|
|
321
|
-
return define(
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (!keyGuard(key)) return false;
|
|
325
|
-
if (!valueGuard(value)) return false;
|
|
326
|
-
}
|
|
327
|
-
return true;
|
|
328
|
-
});
|
|
365
|
+
return define(
|
|
366
|
+
(input) => isMap(input) && everyMapEntry(input, keyGuard, valueGuard)
|
|
367
|
+
);
|
|
329
368
|
}
|
|
330
369
|
|
|
331
370
|
// src/core/combinators/tuple.ts
|
|
332
371
|
function tupleOf(...guards) {
|
|
333
|
-
return define(
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
);
|
|
337
|
-
});
|
|
372
|
+
return define(
|
|
373
|
+
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
374
|
+
);
|
|
338
375
|
}
|
|
339
376
|
|
|
340
377
|
// src/core/combinators/one-of.ts
|
|
341
378
|
function oneOf(...guards) {
|
|
342
|
-
|
|
343
|
-
return (input) => predicates.some((guard) => guard(input));
|
|
379
|
+
return or(...guards);
|
|
344
380
|
}
|
|
345
381
|
|
|
346
382
|
// src/core/combinators/record.ts
|
|
347
383
|
function recordOf(keyFunction, valueFunction) {
|
|
348
|
-
return (
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
for (const key of Object.keys(obj)) {
|
|
352
|
-
if (!keyFunction(key)) return false;
|
|
353
|
-
const value = obj[key];
|
|
354
|
-
if (!valueFunction(value)) return false;
|
|
355
|
-
}
|
|
356
|
-
return true;
|
|
357
|
-
};
|
|
384
|
+
return define(
|
|
385
|
+
(input) => isPlainObject(input) && everyOwnEnumerableEntry(input, keyFunction, valueFunction)
|
|
386
|
+
);
|
|
358
387
|
}
|
|
359
388
|
|
|
360
389
|
// src/core/combinators/struct.ts
|
|
361
|
-
var hasOwnKey = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
362
390
|
var isOptionalSchemaField = define(
|
|
363
391
|
// WHY: Optional fields are represented as a small tagged object so `struct`
|
|
364
392
|
// can distinguish schema metadata from plain predicate functions up front.
|
|
@@ -367,12 +395,12 @@ var isOptionalSchemaField = define(
|
|
|
367
395
|
var hasRequiredKeys = (obj, entries) => (
|
|
368
396
|
// WHY: Required fields must be own properties; inherited values should not
|
|
369
397
|
// satisfy a schema because `struct` models object payload shape, not prototype chains.
|
|
370
|
-
entries.every(([key, guard]) =>
|
|
398
|
+
entries.every(([key, guard]) => Object.hasOwn(obj, key) && guard(obj[key]))
|
|
371
399
|
);
|
|
372
400
|
var hasValidOptionalKeys = (obj, entries) => (
|
|
373
401
|
// WHY: Optional keys are validated only when present. Missing keys stay valid
|
|
374
402
|
// without forcing callers to encode `undefined` into the value guard.
|
|
375
|
-
entries.every(([key, guard]) => !
|
|
403
|
+
entries.every(([key, guard]) => !Object.hasOwn(obj, key) || guard(obj[key]))
|
|
376
404
|
);
|
|
377
405
|
var hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
|
|
378
406
|
function optionalKey(guard) {
|
|
@@ -438,14 +466,14 @@ function oneOfValues(...args) {
|
|
|
438
466
|
|
|
439
467
|
// src/core/key.ts
|
|
440
468
|
var hasKey = (key) => define(
|
|
441
|
-
(input) => isObject(input) && Object.
|
|
469
|
+
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
442
470
|
);
|
|
443
471
|
var hasKeys = (...keys) => {
|
|
444
472
|
if (keys.length === 0) {
|
|
445
473
|
return define(() => false);
|
|
446
474
|
}
|
|
447
475
|
return define(
|
|
448
|
-
(input) => isObject(input) && keys.every((key) => Object.
|
|
476
|
+
(input) => isObject(input) && keys.every((key) => Object.hasOwn(input, key))
|
|
449
477
|
);
|
|
450
478
|
};
|
|
451
479
|
function narrowKeyTo(guard, key) {
|
|
@@ -466,6 +494,11 @@ function narrowKeyTo(guard, key) {
|
|
|
466
494
|
equals,
|
|
467
495
|
equalsBy,
|
|
468
496
|
equalsKey,
|
|
497
|
+
everyArrayValue,
|
|
498
|
+
everyMapEntry,
|
|
499
|
+
everyOwnEnumerableEntry,
|
|
500
|
+
everySetValue,
|
|
501
|
+
everyTupleValue,
|
|
469
502
|
guardIn,
|
|
470
503
|
hasKey,
|
|
471
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,24 +85,38 @@ 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
|
|
|
106
|
+
// src/utils/object-tags.ts
|
|
107
|
+
var objectToString = Object.prototype.toString;
|
|
108
|
+
var OBJECT_TAG_DATE = "[object Date]";
|
|
109
|
+
var OBJECT_TAG_REGEXP = "[object RegExp]";
|
|
110
|
+
var OBJECT_TAG_MAP = "[object Map]";
|
|
111
|
+
var OBJECT_TAG_SET = "[object Set]";
|
|
112
|
+
var OBJECT_TAG_WEAK_MAP = "[object WeakMap]";
|
|
113
|
+
var OBJECT_TAG_WEAK_SET = "[object WeakSet]";
|
|
114
|
+
var OBJECT_TAG_ARRAY_BUFFER = "[object ArrayBuffer]";
|
|
115
|
+
var OBJECT_TAG_DATA_VIEW = "[object DataView]";
|
|
116
|
+
var OBJECT_TAG_ERROR = "[object Error]";
|
|
117
|
+
var getTag = (value) => objectToString.call(value);
|
|
118
|
+
|
|
71
119
|
// src/core/object.ts
|
|
72
|
-
var getTag = (value) => Object.prototype.toString.call(value);
|
|
73
120
|
var isFunction = define(
|
|
74
121
|
(value) => typeof value === "function"
|
|
75
122
|
);
|
|
@@ -83,46 +130,46 @@ var isPlainObject = define((value) => {
|
|
|
83
130
|
});
|
|
84
131
|
var isArray = define(Array.isArray);
|
|
85
132
|
var isDate = define(
|
|
86
|
-
(value) => getTag(value) ===
|
|
133
|
+
(value) => getTag(value) === OBJECT_TAG_DATE && !Number.isNaN(value.getTime())
|
|
87
134
|
);
|
|
88
135
|
var isRegExp = define(
|
|
89
|
-
(value) => getTag(value) ===
|
|
136
|
+
(value) => getTag(value) === OBJECT_TAG_REGEXP
|
|
90
137
|
);
|
|
91
138
|
var isMap = define(
|
|
92
|
-
(value) => getTag(value) ===
|
|
139
|
+
(value) => getTag(value) === OBJECT_TAG_MAP
|
|
93
140
|
);
|
|
94
141
|
var isSet = define(
|
|
95
|
-
(value) => getTag(value) ===
|
|
142
|
+
(value) => getTag(value) === OBJECT_TAG_SET
|
|
96
143
|
);
|
|
97
144
|
var isWeakMap = define(
|
|
98
|
-
(value) => getTag(value) ===
|
|
145
|
+
(value) => getTag(value) === OBJECT_TAG_WEAK_MAP
|
|
99
146
|
);
|
|
100
147
|
var isWeakSet = define(
|
|
101
|
-
(value) => getTag(value) ===
|
|
148
|
+
(value) => getTag(value) === OBJECT_TAG_WEAK_SET
|
|
102
149
|
);
|
|
103
150
|
var isPromiseLike = define((value) => {
|
|
104
151
|
if (!isObject(value) && !isFunction(value)) return false;
|
|
105
|
-
return
|
|
152
|
+
return isFunction(value.then);
|
|
106
153
|
});
|
|
107
154
|
var isIterable = define((value) => {
|
|
108
155
|
if (!isObject(value) && !isFunction(value)) return false;
|
|
109
|
-
return
|
|
156
|
+
return isFunction(value[Symbol.iterator]);
|
|
110
157
|
});
|
|
111
158
|
var isAsyncIterable = define((value) => {
|
|
112
159
|
if (!isObject(value) && !isFunction(value)) return false;
|
|
113
|
-
return
|
|
160
|
+
return isFunction(value[Symbol.asyncIterator]);
|
|
114
161
|
});
|
|
115
162
|
var isArrayBuffer = define(
|
|
116
|
-
(value) => getTag(value) ===
|
|
163
|
+
(value) => getTag(value) === OBJECT_TAG_ARRAY_BUFFER
|
|
117
164
|
);
|
|
118
165
|
var isDataView = define(
|
|
119
|
-
(value) => getTag(value) ===
|
|
166
|
+
(value) => getTag(value) === OBJECT_TAG_DATA_VIEW
|
|
120
167
|
);
|
|
121
168
|
var isTypedArray = define(
|
|
122
|
-
(value) => ArrayBuffer.isView(value) && getTag(value) !==
|
|
169
|
+
(value) => ArrayBuffer.isView(value) && getTag(value) !== OBJECT_TAG_DATA_VIEW
|
|
123
170
|
);
|
|
124
171
|
var isError = define(
|
|
125
|
-
(value) => value instanceof Error || getTag(value) ===
|
|
172
|
+
(value) => value instanceof Error || getTag(value) === OBJECT_TAG_ERROR
|
|
126
173
|
);
|
|
127
174
|
var isURL = typeof URL !== "undefined" ? define((value) => value instanceof URL) : define(() => false);
|
|
128
175
|
var isBlob = typeof Blob !== "undefined" ? define((value) => value instanceof Blob) : define(() => false);
|
|
@@ -146,7 +193,7 @@ function equalsBy(guard, selector) {
|
|
|
146
193
|
}
|
|
147
194
|
function equalsKey(key, target) {
|
|
148
195
|
const hasMatchingKey = define((input) => {
|
|
149
|
-
return isObject(input) && Object.
|
|
196
|
+
return isObject(input) && Object.hasOwn(input, key) && Object.is(input[key], target);
|
|
150
197
|
});
|
|
151
198
|
return (input) => hasMatchingKey(input);
|
|
152
199
|
}
|
|
@@ -203,69 +250,45 @@ var isPrimitive = or(
|
|
|
203
250
|
|
|
204
251
|
// src/core/combinators/array.ts
|
|
205
252
|
function arrayOf(elementGuard) {
|
|
206
|
-
return define(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (!elementGuard(element)) return false;
|
|
210
|
-
}
|
|
211
|
-
return true;
|
|
212
|
-
});
|
|
253
|
+
return define(
|
|
254
|
+
(input) => Array.isArray(input) && everyArrayValue(input, elementGuard)
|
|
255
|
+
);
|
|
213
256
|
}
|
|
214
257
|
|
|
215
258
|
// src/core/combinators/set.ts
|
|
216
259
|
function setOf(valueGuard) {
|
|
217
|
-
return define(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
if (!valueGuard(value)) return false;
|
|
221
|
-
}
|
|
222
|
-
return true;
|
|
223
|
-
});
|
|
260
|
+
return define(
|
|
261
|
+
(input) => isSet(input) && everySetValue(input, valueGuard)
|
|
262
|
+
);
|
|
224
263
|
}
|
|
225
264
|
|
|
226
265
|
// src/core/combinators/map.ts
|
|
227
266
|
function mapOf(keyGuard, valueGuard) {
|
|
228
|
-
return define(
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (!keyGuard(key)) return false;
|
|
232
|
-
if (!valueGuard(value)) return false;
|
|
233
|
-
}
|
|
234
|
-
return true;
|
|
235
|
-
});
|
|
267
|
+
return define(
|
|
268
|
+
(input) => isMap(input) && everyMapEntry(input, keyGuard, valueGuard)
|
|
269
|
+
);
|
|
236
270
|
}
|
|
237
271
|
|
|
238
272
|
// src/core/combinators/tuple.ts
|
|
239
273
|
function tupleOf(...guards) {
|
|
240
|
-
return define(
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
);
|
|
244
|
-
});
|
|
274
|
+
return define(
|
|
275
|
+
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
276
|
+
);
|
|
245
277
|
}
|
|
246
278
|
|
|
247
279
|
// src/core/combinators/one-of.ts
|
|
248
280
|
function oneOf(...guards) {
|
|
249
|
-
|
|
250
|
-
return (input) => predicates.some((guard) => guard(input));
|
|
281
|
+
return or(...guards);
|
|
251
282
|
}
|
|
252
283
|
|
|
253
284
|
// src/core/combinators/record.ts
|
|
254
285
|
function recordOf(keyFunction, valueFunction) {
|
|
255
|
-
return (
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
for (const key of Object.keys(obj)) {
|
|
259
|
-
if (!keyFunction(key)) return false;
|
|
260
|
-
const value = obj[key];
|
|
261
|
-
if (!valueFunction(value)) return false;
|
|
262
|
-
}
|
|
263
|
-
return true;
|
|
264
|
-
};
|
|
286
|
+
return define(
|
|
287
|
+
(input) => isPlainObject(input) && everyOwnEnumerableEntry(input, keyFunction, valueFunction)
|
|
288
|
+
);
|
|
265
289
|
}
|
|
266
290
|
|
|
267
291
|
// src/core/combinators/struct.ts
|
|
268
|
-
var hasOwnKey = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
269
292
|
var isOptionalSchemaField = define(
|
|
270
293
|
// WHY: Optional fields are represented as a small tagged object so `struct`
|
|
271
294
|
// can distinguish schema metadata from plain predicate functions up front.
|
|
@@ -274,12 +297,12 @@ var isOptionalSchemaField = define(
|
|
|
274
297
|
var hasRequiredKeys = (obj, entries) => (
|
|
275
298
|
// WHY: Required fields must be own properties; inherited values should not
|
|
276
299
|
// satisfy a schema because `struct` models object payload shape, not prototype chains.
|
|
277
|
-
entries.every(([key, guard]) =>
|
|
300
|
+
entries.every(([key, guard]) => Object.hasOwn(obj, key) && guard(obj[key]))
|
|
278
301
|
);
|
|
279
302
|
var hasValidOptionalKeys = (obj, entries) => (
|
|
280
303
|
// WHY: Optional keys are validated only when present. Missing keys stay valid
|
|
281
304
|
// without forcing callers to encode `undefined` into the value guard.
|
|
282
|
-
entries.every(([key, guard]) => !
|
|
305
|
+
entries.every(([key, guard]) => !Object.hasOwn(obj, key) || guard(obj[key]))
|
|
283
306
|
);
|
|
284
307
|
var hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
|
|
285
308
|
function optionalKey(guard) {
|
|
@@ -345,14 +368,14 @@ function oneOfValues(...args) {
|
|
|
345
368
|
|
|
346
369
|
// src/core/key.ts
|
|
347
370
|
var hasKey = (key) => define(
|
|
348
|
-
(input) => isObject(input) && Object.
|
|
371
|
+
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
349
372
|
);
|
|
350
373
|
var hasKeys = (...keys) => {
|
|
351
374
|
if (keys.length === 0) {
|
|
352
375
|
return define(() => false);
|
|
353
376
|
}
|
|
354
377
|
return define(
|
|
355
|
-
(input) => isObject(input) && keys.every((key) => Object.
|
|
378
|
+
(input) => isObject(input) && keys.every((key) => Object.hasOwn(input, key))
|
|
356
379
|
);
|
|
357
380
|
};
|
|
358
381
|
function narrowKeyTo(guard, key) {
|
|
@@ -372,6 +395,11 @@ export {
|
|
|
372
395
|
equals,
|
|
373
396
|
equalsBy,
|
|
374
397
|
equalsKey,
|
|
398
|
+
everyArrayValue,
|
|
399
|
+
everyMapEntry,
|
|
400
|
+
everyOwnEnumerableEntry,
|
|
401
|
+
everySetValue,
|
|
402
|
+
everyTupleValue,
|
|
375
403
|
guardIn,
|
|
376
404
|
hasKey,
|
|
377
405
|
hasKeys,
|