is-kit 1.1.1 → 1.1.3
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 +13 -0
- package/dist/index.d.mts +22 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +27 -5
- package/dist/index.mjs +24 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -116,6 +116,19 @@ Composed guards stay reusable:
|
|
|
116
116
|
|
|
117
117
|
When validating complex shapes, reach for `struct` — and friends like `arrayOf`, `recordOf`, or `oneOf`.
|
|
118
118
|
|
|
119
|
+
### Primitive guards
|
|
120
|
+
|
|
121
|
+
Built-in primitives: `isString`, `isNumber` (finite), `isBoolean`, `isBigInt`, `isSymbol`, `isUndefined`, `isNull` — and a preset `isPrimitive` for any primitive.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { isPrimitive, isNumber } from 'is-kit';
|
|
125
|
+
|
|
126
|
+
isPrimitive('x'); // true
|
|
127
|
+
isPrimitive(123); // true
|
|
128
|
+
isPrimitive(NaN); // true (use isNumber for finite only)
|
|
129
|
+
isPrimitive({}); // false
|
|
130
|
+
```
|
|
131
|
+
|
|
119
132
|
## Core Ideas
|
|
120
133
|
|
|
121
134
|
- **Define once**: `define<T>(fn)` turns a plain function into a type guard.
|
package/dist/index.d.mts
CHANGED
|
@@ -218,6 +218,15 @@ declare const isUndefined: Predicate<undefined>;
|
|
|
218
218
|
* @returns Predicate narrowing to `null`.
|
|
219
219
|
*/
|
|
220
220
|
declare const isNull: Predicate<null>;
|
|
221
|
+
/**
|
|
222
|
+
* Checks whether a value is a JavaScript primitive.
|
|
223
|
+
*
|
|
224
|
+
* Primitives: string, number, boolean, bigint, symbol, undefined, null.
|
|
225
|
+
* Note: `number` here includes NaN and ±Infinity (use `isNumber` for finite only).
|
|
226
|
+
*
|
|
227
|
+
* @returns Predicate narrowing to `Primitive`.
|
|
228
|
+
*/
|
|
229
|
+
declare const isPrimitive: Guard<string | number | bigint | boolean | symbol | null | undefined>;
|
|
221
230
|
|
|
222
231
|
/**
|
|
223
232
|
* Validates an array where every element satisfies the provided guard.
|
|
@@ -324,6 +333,18 @@ declare const isMap: Predicate<Map<unknown, unknown>>;
|
|
|
324
333
|
* @returns Predicate narrowing to `Set<unknown>`.
|
|
325
334
|
*/
|
|
326
335
|
declare const isSet: Predicate<Set<unknown>>;
|
|
336
|
+
/**
|
|
337
|
+
* Checks whether a value is a `WeakMap`.
|
|
338
|
+
*
|
|
339
|
+
* @returns Predicate narrowing to `WeakMap<object, unknown>`.
|
|
340
|
+
*/
|
|
341
|
+
declare const isWeakMap: Predicate<WeakMap<object, unknown>>;
|
|
342
|
+
/**
|
|
343
|
+
* Checks whether a value is a `WeakSet`.
|
|
344
|
+
*
|
|
345
|
+
* @returns Predicate narrowing to `WeakSet<object>`.
|
|
346
|
+
*/
|
|
347
|
+
declare const isWeakSet: Predicate<WeakSet<object>>;
|
|
327
348
|
/**
|
|
328
349
|
* Checks whether a value is promise-like (has a `then` function).
|
|
329
350
|
*
|
|
@@ -401,4 +422,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
401
422
|
*/
|
|
402
423
|
declare const toBooleanPredicates: (guards: readonly Guard<unknown>[]) => ReadonlyArray<(value: unknown) => boolean>;
|
|
403
424
|
|
|
404
|
-
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, and, andAll, arrayOf, define, equals, equalsBy, equalsKey, guardIn, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
|
425
|
+
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, and, andAll, arrayOf, define, equals, equalsBy, equalsKey, guardIn, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPrimitive, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
package/dist/index.d.ts
CHANGED
|
@@ -218,6 +218,15 @@ declare const isUndefined: Predicate<undefined>;
|
|
|
218
218
|
* @returns Predicate narrowing to `null`.
|
|
219
219
|
*/
|
|
220
220
|
declare const isNull: Predicate<null>;
|
|
221
|
+
/**
|
|
222
|
+
* Checks whether a value is a JavaScript primitive.
|
|
223
|
+
*
|
|
224
|
+
* Primitives: string, number, boolean, bigint, symbol, undefined, null.
|
|
225
|
+
* Note: `number` here includes NaN and ±Infinity (use `isNumber` for finite only).
|
|
226
|
+
*
|
|
227
|
+
* @returns Predicate narrowing to `Primitive`.
|
|
228
|
+
*/
|
|
229
|
+
declare const isPrimitive: Guard<string | number | bigint | boolean | symbol | null | undefined>;
|
|
221
230
|
|
|
222
231
|
/**
|
|
223
232
|
* Validates an array where every element satisfies the provided guard.
|
|
@@ -324,6 +333,18 @@ declare const isMap: Predicate<Map<unknown, unknown>>;
|
|
|
324
333
|
* @returns Predicate narrowing to `Set<unknown>`.
|
|
325
334
|
*/
|
|
326
335
|
declare const isSet: Predicate<Set<unknown>>;
|
|
336
|
+
/**
|
|
337
|
+
* Checks whether a value is a `WeakMap`.
|
|
338
|
+
*
|
|
339
|
+
* @returns Predicate narrowing to `WeakMap<object, unknown>`.
|
|
340
|
+
*/
|
|
341
|
+
declare const isWeakMap: Predicate<WeakMap<object, unknown>>;
|
|
342
|
+
/**
|
|
343
|
+
* Checks whether a value is a `WeakSet`.
|
|
344
|
+
*
|
|
345
|
+
* @returns Predicate narrowing to `WeakSet<object>`.
|
|
346
|
+
*/
|
|
347
|
+
declare const isWeakSet: Predicate<WeakSet<object>>;
|
|
327
348
|
/**
|
|
328
349
|
* Checks whether a value is promise-like (has a `then` function).
|
|
329
350
|
*
|
|
@@ -401,4 +422,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
401
422
|
*/
|
|
402
423
|
declare const toBooleanPredicates: (guards: readonly Guard<unknown>[]) => ReadonlyArray<(value: unknown) => boolean>;
|
|
403
424
|
|
|
404
|
-
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, and, andAll, arrayOf, define, equals, equalsBy, equalsKey, guardIn, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
|
425
|
+
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, and, andAll, arrayOf, define, equals, equalsBy, equalsKey, guardIn, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPrimitive, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
package/dist/index.js
CHANGED
|
@@ -46,6 +46,7 @@ __export(index_exports, {
|
|
|
46
46
|
isNumberPrimitive: () => isNumberPrimitive,
|
|
47
47
|
isObject: () => isObject,
|
|
48
48
|
isPlainObject: () => isPlainObject,
|
|
49
|
+
isPrimitive: () => isPrimitive,
|
|
49
50
|
isPromiseLike: () => isPromiseLike,
|
|
50
51
|
isRegExp: () => isRegExp,
|
|
51
52
|
isSet: () => isSet,
|
|
@@ -54,6 +55,8 @@ __export(index_exports, {
|
|
|
54
55
|
isTypedArray: () => isTypedArray,
|
|
55
56
|
isURL: () => isURL,
|
|
56
57
|
isUndefined: () => isUndefined,
|
|
58
|
+
isWeakMap: () => isWeakMap,
|
|
59
|
+
isWeakSet: () => isWeakSet,
|
|
57
60
|
narrowKeyTo: () => narrowKeyTo,
|
|
58
61
|
nonNull: () => nonNull,
|
|
59
62
|
not: () => not,
|
|
@@ -160,6 +163,12 @@ var isMap = define(
|
|
|
160
163
|
var isSet = define(
|
|
161
164
|
(value) => getTag(value) === "[object Set]"
|
|
162
165
|
);
|
|
166
|
+
var isWeakMap = define(
|
|
167
|
+
(value) => getTag(value) === "[object WeakMap]"
|
|
168
|
+
);
|
|
169
|
+
var isWeakSet = define(
|
|
170
|
+
(value) => getTag(value) === "[object WeakSet]"
|
|
171
|
+
);
|
|
163
172
|
var isPromiseLike = define((value) => {
|
|
164
173
|
if (!isObject(value) && !isFunction(value)) return false;
|
|
165
174
|
return typeof value.then === "function";
|
|
@@ -230,6 +239,15 @@ var isBigInt = define((value) => typeof value === "bigint");
|
|
|
230
239
|
var isSymbol = define((value) => typeof value === "symbol");
|
|
231
240
|
var isUndefined = define((value) => value === void 0);
|
|
232
241
|
var isNull = define((value) => value === null);
|
|
242
|
+
var isPrimitive = or(
|
|
243
|
+
isString,
|
|
244
|
+
isNumberPrimitive,
|
|
245
|
+
isBoolean,
|
|
246
|
+
isBigInt,
|
|
247
|
+
isSymbol,
|
|
248
|
+
isUndefined,
|
|
249
|
+
isNull
|
|
250
|
+
);
|
|
233
251
|
|
|
234
252
|
// src/core/combinators/array.ts
|
|
235
253
|
function arrayOf(elementGuard) {
|
|
@@ -272,16 +290,17 @@ function recordOf(keyFunction, valueFunction) {
|
|
|
272
290
|
|
|
273
291
|
// src/core/combinators/struct.ts
|
|
274
292
|
function struct(schema, options) {
|
|
293
|
+
const schemaKeys = Object.keys(schema);
|
|
294
|
+
const allowed = options?.exact ? new Set(schemaKeys) : null;
|
|
275
295
|
return (input) => {
|
|
276
296
|
if (!isPlainObject(input)) return false;
|
|
277
297
|
const obj = input;
|
|
278
|
-
for (const key of
|
|
298
|
+
for (const key of schemaKeys) {
|
|
279
299
|
if (!(key in obj)) return false;
|
|
280
300
|
const guard = schema[key];
|
|
281
301
|
if (!guard(obj[key])) return false;
|
|
282
302
|
}
|
|
283
|
-
if (
|
|
284
|
-
const allowed = new Set(Object.keys(schema));
|
|
303
|
+
if (allowed) {
|
|
285
304
|
for (const key of Object.keys(obj)) {
|
|
286
305
|
if (!allowed.has(key)) return false;
|
|
287
306
|
}
|
|
@@ -309,9 +328,9 @@ function oneOfValues(...values) {
|
|
|
309
328
|
function narrowKeyTo(guard, key) {
|
|
310
329
|
return function(target) {
|
|
311
330
|
const keyEquals = equalsKey(key, target);
|
|
312
|
-
return function(input) {
|
|
331
|
+
return define(function(input) {
|
|
313
332
|
return guard(input) && keyEquals(input);
|
|
314
|
-
};
|
|
333
|
+
});
|
|
315
334
|
};
|
|
316
335
|
}
|
|
317
336
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -342,6 +361,7 @@ function narrowKeyTo(guard, key) {
|
|
|
342
361
|
isNumberPrimitive,
|
|
343
362
|
isObject,
|
|
344
363
|
isPlainObject,
|
|
364
|
+
isPrimitive,
|
|
345
365
|
isPromiseLike,
|
|
346
366
|
isRegExp,
|
|
347
367
|
isSet,
|
|
@@ -350,6 +370,8 @@ function narrowKeyTo(guard, key) {
|
|
|
350
370
|
isTypedArray,
|
|
351
371
|
isURL,
|
|
352
372
|
isUndefined,
|
|
373
|
+
isWeakMap,
|
|
374
|
+
isWeakSet,
|
|
353
375
|
narrowKeyTo,
|
|
354
376
|
nonNull,
|
|
355
377
|
not,
|
package/dist/index.mjs
CHANGED
|
@@ -84,6 +84,12 @@ var isMap = define(
|
|
|
84
84
|
var isSet = define(
|
|
85
85
|
(value) => getTag(value) === "[object Set]"
|
|
86
86
|
);
|
|
87
|
+
var isWeakMap = define(
|
|
88
|
+
(value) => getTag(value) === "[object WeakMap]"
|
|
89
|
+
);
|
|
90
|
+
var isWeakSet = define(
|
|
91
|
+
(value) => getTag(value) === "[object WeakSet]"
|
|
92
|
+
);
|
|
87
93
|
var isPromiseLike = define((value) => {
|
|
88
94
|
if (!isObject(value) && !isFunction(value)) return false;
|
|
89
95
|
return typeof value.then === "function";
|
|
@@ -154,6 +160,15 @@ var isBigInt = define((value) => typeof value === "bigint");
|
|
|
154
160
|
var isSymbol = define((value) => typeof value === "symbol");
|
|
155
161
|
var isUndefined = define((value) => value === void 0);
|
|
156
162
|
var isNull = define((value) => value === null);
|
|
163
|
+
var isPrimitive = or(
|
|
164
|
+
isString,
|
|
165
|
+
isNumberPrimitive,
|
|
166
|
+
isBoolean,
|
|
167
|
+
isBigInt,
|
|
168
|
+
isSymbol,
|
|
169
|
+
isUndefined,
|
|
170
|
+
isNull
|
|
171
|
+
);
|
|
157
172
|
|
|
158
173
|
// src/core/combinators/array.ts
|
|
159
174
|
function arrayOf(elementGuard) {
|
|
@@ -196,16 +211,17 @@ function recordOf(keyFunction, valueFunction) {
|
|
|
196
211
|
|
|
197
212
|
// src/core/combinators/struct.ts
|
|
198
213
|
function struct(schema, options) {
|
|
214
|
+
const schemaKeys = Object.keys(schema);
|
|
215
|
+
const allowed = options?.exact ? new Set(schemaKeys) : null;
|
|
199
216
|
return (input) => {
|
|
200
217
|
if (!isPlainObject(input)) return false;
|
|
201
218
|
const obj = input;
|
|
202
|
-
for (const key of
|
|
219
|
+
for (const key of schemaKeys) {
|
|
203
220
|
if (!(key in obj)) return false;
|
|
204
221
|
const guard = schema[key];
|
|
205
222
|
if (!guard(obj[key])) return false;
|
|
206
223
|
}
|
|
207
|
-
if (
|
|
208
|
-
const allowed = new Set(Object.keys(schema));
|
|
224
|
+
if (allowed) {
|
|
209
225
|
for (const key of Object.keys(obj)) {
|
|
210
226
|
if (!allowed.has(key)) return false;
|
|
211
227
|
}
|
|
@@ -233,9 +249,9 @@ function oneOfValues(...values) {
|
|
|
233
249
|
function narrowKeyTo(guard, key) {
|
|
234
250
|
return function(target) {
|
|
235
251
|
const keyEquals = equalsKey(key, target);
|
|
236
|
-
return function(input) {
|
|
252
|
+
return define(function(input) {
|
|
237
253
|
return guard(input) && keyEquals(input);
|
|
238
|
-
};
|
|
254
|
+
});
|
|
239
255
|
};
|
|
240
256
|
}
|
|
241
257
|
export {
|
|
@@ -265,6 +281,7 @@ export {
|
|
|
265
281
|
isNumberPrimitive,
|
|
266
282
|
isObject,
|
|
267
283
|
isPlainObject,
|
|
284
|
+
isPrimitive,
|
|
268
285
|
isPromiseLike,
|
|
269
286
|
isRegExp,
|
|
270
287
|
isSet,
|
|
@@ -273,6 +290,8 @@ export {
|
|
|
273
290
|
isTypedArray,
|
|
274
291
|
isURL,
|
|
275
292
|
isUndefined,
|
|
293
|
+
isWeakMap,
|
|
294
|
+
isWeakSet,
|
|
276
295
|
narrowKeyTo,
|
|
277
296
|
nonNull,
|
|
278
297
|
not,
|