is-kit 1.5.0 → 1.6.0
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 +37 -2
- package/dist/index.d.mts +18 -1
- package/dist/index.d.ts +18 -1
- package/dist/index.js +27 -0
- package/dist/index.mjs +25 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,7 +69,9 @@ import {
|
|
|
69
69
|
and,
|
|
70
70
|
or,
|
|
71
71
|
not,
|
|
72
|
+
mapOf,
|
|
72
73
|
optionalKey,
|
|
74
|
+
setOf,
|
|
73
75
|
struct,
|
|
74
76
|
oneOfValues,
|
|
75
77
|
isNumber,
|
|
@@ -90,6 +92,8 @@ const isOther = not(isShortOrPositive);
|
|
|
90
92
|
|
|
91
93
|
// Define object shapes
|
|
92
94
|
const isRole = oneOfValues(['admin', 'member'] as const);
|
|
95
|
+
const isTags = setOf(isString);
|
|
96
|
+
const isScores = mapOf(isString, isNumber);
|
|
93
97
|
const isUser = struct({
|
|
94
98
|
id: isPositive, // number > 0
|
|
95
99
|
name: isString, // string
|
|
@@ -102,6 +106,8 @@ isPositive(3); // true
|
|
|
102
106
|
isShortOrPositive('foo'); // true
|
|
103
107
|
isShortOrPositive(false); // false
|
|
104
108
|
isOther('x'); // true
|
|
109
|
+
isTags(new Set(['new', 'vip'])); // true
|
|
110
|
+
isScores(new Map([['math', 98]])); // true
|
|
105
111
|
|
|
106
112
|
const maybeUser: unknown = { id: 7, name: 'Rin', role: 'admin' };
|
|
107
113
|
if (isUser(maybeUser)) {
|
|
@@ -118,7 +124,34 @@ Combine them as `optionalKey(optional(guard))` when both are allowed.
|
|
|
118
124
|
Composed guards stay reusable:
|
|
119
125
|
`isPositive` can be used standalone or as part of a `struct` definition.
|
|
120
126
|
|
|
121
|
-
When validating complex shapes, reach for `struct`
|
|
127
|
+
When validating complex shapes, reach for `struct` and collection
|
|
128
|
+
combinators like `arrayOf`, `setOf`, `mapOf`, `recordOf`, or `oneOf`.
|
|
129
|
+
|
|
130
|
+
### Collection combinators
|
|
131
|
+
|
|
132
|
+
Use `arrayOf`, `setOf`, `mapOf`, and `recordOf` to validate homogeneous
|
|
133
|
+
collections while preserving precise readonly types.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import {
|
|
137
|
+
arrayOf,
|
|
138
|
+
mapOf,
|
|
139
|
+
recordOf,
|
|
140
|
+
setOf,
|
|
141
|
+
isNumber,
|
|
142
|
+
isString
|
|
143
|
+
} from 'is-kit';
|
|
144
|
+
|
|
145
|
+
const isStringArray = arrayOf(isString);
|
|
146
|
+
const isStringSet = setOf(isString);
|
|
147
|
+
const isScoreMap = mapOf(isString, isNumber);
|
|
148
|
+
const isStringRecord = recordOf(isString, isString);
|
|
149
|
+
|
|
150
|
+
isStringArray(['a', 'b']); // true
|
|
151
|
+
isStringSet(new Set(['a', 'b'])); // true
|
|
152
|
+
isScoreMap(new Map([['math', 98]])); // true
|
|
153
|
+
isStringRecord({ a: 'x', b: 'y' }); // true
|
|
154
|
+
```
|
|
122
155
|
|
|
123
156
|
### Primitive guards
|
|
124
157
|
|
|
@@ -165,6 +198,8 @@ isInfiniteNumber(1); // false
|
|
|
165
198
|
|
|
166
199
|
Object/built-in guards cover arrays, dates, maps/sets, and more. Use
|
|
167
200
|
`isInstanceOf` when you want a reusable guard from a class constructor.
|
|
201
|
+
For content-aware map/set validation, pair `isMap` / `isSet` with `mapOf`
|
|
202
|
+
and `setOf`.
|
|
168
203
|
|
|
169
204
|
```ts
|
|
170
205
|
import { isArray, isDate, isInstanceOf } from 'is-kit';
|
|
@@ -184,7 +219,7 @@ isAnimal({}); // false
|
|
|
184
219
|
|
|
185
220
|
- **Define once**: `define<T>(fn)` turns a plain function into a type guard.
|
|
186
221
|
- **Upgrade predicates**: `predicateToRefine(fn)` adds narrowing.
|
|
187
|
-
- **Compose freely**: `and`, `or`, `not`, `oneOf`, `arrayOf`, `struct` …
|
|
222
|
+
- **Compose freely**: `and`, `or`, `not`, `oneOf`, `arrayOf`, `setOf`, `mapOf`, `struct` …
|
|
188
223
|
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`, `assert`, `hasKey`, `hasKeys`, `narrowKeyTo`.
|
|
189
224
|
|
|
190
225
|
### Key Helpers
|
package/dist/index.d.mts
CHANGED
|
@@ -325,6 +325,23 @@ declare const isPrimitive: Guard<string | number | bigint | boolean | symbol | n
|
|
|
325
325
|
*/
|
|
326
326
|
declare function arrayOf<F extends Predicate<unknown>>(elementGuard: F): Predicate<readonly GuardedOf<F>[]>;
|
|
327
327
|
|
|
328
|
+
/**
|
|
329
|
+
* Validates a set where every value satisfies the provided guard.
|
|
330
|
+
*
|
|
331
|
+
* @param valueGuard Guard applied to each set value.
|
|
332
|
+
* @returns Predicate narrowing to a readonly set of the guarded value type.
|
|
333
|
+
*/
|
|
334
|
+
declare function setOf<VF extends Predicate<unknown>>(valueGuard: VF): Predicate<ReadonlySet<GuardedOf<VF>>>;
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Validates a map where every key and value satisfies the provided guards.
|
|
338
|
+
*
|
|
339
|
+
* @param keyGuard Guard applied to each map key.
|
|
340
|
+
* @param valueGuard Guard applied to each map value.
|
|
341
|
+
* @returns Predicate narrowing to a readonly map with guarded key/value types.
|
|
342
|
+
*/
|
|
343
|
+
declare function mapOf<KF extends Predicate<unknown>, VF extends Predicate<unknown>>(keyGuard: KF, valueGuard: VF): Predicate<ReadonlyMap<GuardedOf<KF>, GuardedOf<VF>>>;
|
|
344
|
+
|
|
328
345
|
/**
|
|
329
346
|
* Validates a fixed-length tuple by applying element-wise guards.
|
|
330
347
|
*
|
|
@@ -546,4 +563,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
546
563
|
*/
|
|
547
564
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
548
565
|
|
|
549
|
-
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, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
|
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -325,6 +325,23 @@ declare const isPrimitive: Guard<string | number | bigint | boolean | symbol | n
|
|
|
325
325
|
*/
|
|
326
326
|
declare function arrayOf<F extends Predicate<unknown>>(elementGuard: F): Predicate<readonly GuardedOf<F>[]>;
|
|
327
327
|
|
|
328
|
+
/**
|
|
329
|
+
* Validates a set where every value satisfies the provided guard.
|
|
330
|
+
*
|
|
331
|
+
* @param valueGuard Guard applied to each set value.
|
|
332
|
+
* @returns Predicate narrowing to a readonly set of the guarded value type.
|
|
333
|
+
*/
|
|
334
|
+
declare function setOf<VF extends Predicate<unknown>>(valueGuard: VF): Predicate<ReadonlySet<GuardedOf<VF>>>;
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Validates a map where every key and value satisfies the provided guards.
|
|
338
|
+
*
|
|
339
|
+
* @param keyGuard Guard applied to each map key.
|
|
340
|
+
* @param valueGuard Guard applied to each map value.
|
|
341
|
+
* @returns Predicate narrowing to a readonly map with guarded key/value types.
|
|
342
|
+
*/
|
|
343
|
+
declare function mapOf<KF extends Predicate<unknown>, VF extends Predicate<unknown>>(keyGuard: KF, valueGuard: VF): Predicate<ReadonlyMap<GuardedOf<KF>, GuardedOf<VF>>>;
|
|
344
|
+
|
|
328
345
|
/**
|
|
329
346
|
* Validates a fixed-length tuple by applying element-wise guards.
|
|
330
347
|
*
|
|
@@ -546,4 +563,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
546
563
|
*/
|
|
547
564
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
548
565
|
|
|
549
|
-
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, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -68,6 +68,7 @@ __export(index_exports, {
|
|
|
68
68
|
isWeakMap: () => isWeakMap,
|
|
69
69
|
isWeakSet: () => isWeakSet,
|
|
70
70
|
isZero: () => isZero,
|
|
71
|
+
mapOf: () => mapOf,
|
|
71
72
|
narrowKeyTo: () => narrowKeyTo,
|
|
72
73
|
nonNull: () => nonNull,
|
|
73
74
|
not: () => not,
|
|
@@ -83,6 +84,7 @@ __export(index_exports, {
|
|
|
83
84
|
required: () => required,
|
|
84
85
|
safeParse: () => safeParse,
|
|
85
86
|
safeParseWith: () => safeParseWith,
|
|
87
|
+
setOf: () => setOf,
|
|
86
88
|
struct: () => struct,
|
|
87
89
|
toBooleanPredicates: () => toBooleanPredicates,
|
|
88
90
|
tupleOf: () => tupleOf
|
|
@@ -303,6 +305,29 @@ function arrayOf(elementGuard) {
|
|
|
303
305
|
};
|
|
304
306
|
}
|
|
305
307
|
|
|
308
|
+
// src/core/combinators/set.ts
|
|
309
|
+
function setOf(valueGuard) {
|
|
310
|
+
return define((input) => {
|
|
311
|
+
if (!isSet(input)) return false;
|
|
312
|
+
for (const value of input.values()) {
|
|
313
|
+
if (!valueGuard(value)) return false;
|
|
314
|
+
}
|
|
315
|
+
return true;
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// src/core/combinators/map.ts
|
|
320
|
+
function mapOf(keyGuard, valueGuard) {
|
|
321
|
+
return define((input) => {
|
|
322
|
+
if (!isMap(input)) return false;
|
|
323
|
+
for (const [key, value] of input.entries()) {
|
|
324
|
+
if (!keyGuard(key)) return false;
|
|
325
|
+
if (!valueGuard(value)) return false;
|
|
326
|
+
}
|
|
327
|
+
return true;
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
|
|
306
331
|
// src/core/combinators/tuple.ts
|
|
307
332
|
function tupleOf(...guards) {
|
|
308
333
|
return (input) => {
|
|
@@ -485,6 +510,7 @@ function narrowKeyTo(guard, key) {
|
|
|
485
510
|
isWeakMap,
|
|
486
511
|
isWeakSet,
|
|
487
512
|
isZero,
|
|
513
|
+
mapOf,
|
|
488
514
|
narrowKeyTo,
|
|
489
515
|
nonNull,
|
|
490
516
|
not,
|
|
@@ -500,6 +526,7 @@ function narrowKeyTo(guard, key) {
|
|
|
500
526
|
required,
|
|
501
527
|
safeParse,
|
|
502
528
|
safeParseWith,
|
|
529
|
+
setOf,
|
|
503
530
|
struct,
|
|
504
531
|
toBooleanPredicates,
|
|
505
532
|
tupleOf
|
package/dist/index.mjs
CHANGED
|
@@ -212,6 +212,29 @@ function arrayOf(elementGuard) {
|
|
|
212
212
|
};
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
+
// src/core/combinators/set.ts
|
|
216
|
+
function setOf(valueGuard) {
|
|
217
|
+
return define((input) => {
|
|
218
|
+
if (!isSet(input)) return false;
|
|
219
|
+
for (const value of input.values()) {
|
|
220
|
+
if (!valueGuard(value)) return false;
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/core/combinators/map.ts
|
|
227
|
+
function mapOf(keyGuard, valueGuard) {
|
|
228
|
+
return define((input) => {
|
|
229
|
+
if (!isMap(input)) return false;
|
|
230
|
+
for (const [key, value] of input.entries()) {
|
|
231
|
+
if (!keyGuard(key)) return false;
|
|
232
|
+
if (!valueGuard(value)) return false;
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
215
238
|
// src/core/combinators/tuple.ts
|
|
216
239
|
function tupleOf(...guards) {
|
|
217
240
|
return (input) => {
|
|
@@ -393,6 +416,7 @@ export {
|
|
|
393
416
|
isWeakMap,
|
|
394
417
|
isWeakSet,
|
|
395
418
|
isZero,
|
|
419
|
+
mapOf,
|
|
396
420
|
narrowKeyTo,
|
|
397
421
|
nonNull,
|
|
398
422
|
not,
|
|
@@ -408,6 +432,7 @@ export {
|
|
|
408
432
|
required,
|
|
409
433
|
safeParse,
|
|
410
434
|
safeParseWith,
|
|
435
|
+
setOf,
|
|
411
436
|
struct,
|
|
412
437
|
toBooleanPredicates,
|
|
413
438
|
tupleOf
|