is-kit 1.2.0 → 1.4.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 -3
- package/dist/index.d.mts +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/index.js +13 -0
- package/dist/index.mjs +11 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -157,20 +157,48 @@ isInfiniteNumber(Infinity); // true
|
|
|
157
157
|
isInfiniteNumber(1); // false
|
|
158
158
|
```
|
|
159
159
|
|
|
160
|
+
### Object guards
|
|
161
|
+
|
|
162
|
+
Object/built-in guards cover arrays, dates, maps/sets, and more. Use
|
|
163
|
+
`isInstanceOf` when you want a reusable guard from a class constructor.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { isArray, isDate, isInstanceOf } from 'is-kit';
|
|
167
|
+
|
|
168
|
+
class Animal {}
|
|
169
|
+
class Dog extends Animal {}
|
|
170
|
+
|
|
171
|
+
const isAnimal = isInstanceOf(Animal);
|
|
172
|
+
|
|
173
|
+
isArray([]); // true
|
|
174
|
+
isDate(new Date()); // true
|
|
175
|
+
isAnimal(new Dog()); // true
|
|
176
|
+
isAnimal({}); // false
|
|
177
|
+
```
|
|
178
|
+
|
|
160
179
|
## Core Ideas
|
|
161
180
|
|
|
162
181
|
- **Define once**: `define<T>(fn)` turns a plain function into a type guard.
|
|
163
182
|
- **Upgrade predicates**: `predicateToRefine(fn)` adds narrowing.
|
|
164
183
|
- **Compose freely**: `and`, `or`, `not`, `oneOf`, `arrayOf`, `struct` …
|
|
165
|
-
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`, `assert`, `hasKey`, `narrowKeyTo`.
|
|
184
|
+
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`, `assert`, `hasKey`, `hasKeys`, `narrowKeyTo`.
|
|
166
185
|
|
|
167
186
|
### Key Helpers
|
|
168
187
|
|
|
169
|
-
Use `hasKey` to check
|
|
188
|
+
Use `hasKey`/`hasKeys` to check required own properties before refining, and
|
|
170
189
|
`narrowKeyTo` when you need to narrow a property to a specific literal value.
|
|
171
190
|
|
|
172
191
|
```ts
|
|
173
|
-
import {
|
|
192
|
+
import {
|
|
193
|
+
hasKey,
|
|
194
|
+
hasKeys,
|
|
195
|
+
isString,
|
|
196
|
+
isNumber,
|
|
197
|
+
narrowKeyTo,
|
|
198
|
+
oneOfValues,
|
|
199
|
+
or,
|
|
200
|
+
struct
|
|
201
|
+
} from 'is-kit';
|
|
174
202
|
|
|
175
203
|
// Base guard (e.g., via struct)
|
|
176
204
|
type User = { id: string; age: number; role: 'admin' | 'guest' | 'trial' };
|
|
@@ -181,6 +209,7 @@ const isUser = struct({
|
|
|
181
209
|
});
|
|
182
210
|
|
|
183
211
|
const hasRole = hasKey('role');
|
|
212
|
+
const hasRoleAndId = hasKeys('role', 'id');
|
|
184
213
|
const byRole = narrowKeyTo(isUser, 'role');
|
|
185
214
|
const isGuest = byRole('guest'); // Readonly<User> & { role: 'guest' }
|
|
186
215
|
const isTrial = byRole('trial'); // Readonly<User> & { role: 'trial' }
|
|
@@ -191,6 +220,11 @@ if (hasRole(value)) {
|
|
|
191
220
|
value.role;
|
|
192
221
|
}
|
|
193
222
|
|
|
223
|
+
if (hasRoleAndId(value)) {
|
|
224
|
+
value.role;
|
|
225
|
+
value.id;
|
|
226
|
+
}
|
|
227
|
+
|
|
194
228
|
if (isGuestOrTrial(value)) {
|
|
195
229
|
// value.role is 'guest' | 'trial'
|
|
196
230
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -461,6 +461,13 @@ declare const isURL: Predicate<URL>;
|
|
|
461
461
|
* @returns Predicate narrowing to `Blob` when supported; otherwise always false.
|
|
462
462
|
*/
|
|
463
463
|
declare const isBlob: Predicate<Blob>;
|
|
464
|
+
/**
|
|
465
|
+
* Creates a guard that checks whether a value is an instance of `constructor`.
|
|
466
|
+
*
|
|
467
|
+
* @param constructor Constructor used as the right-hand side of `instanceof`.
|
|
468
|
+
* @returns Predicate narrowing to `InstanceType<C>`.
|
|
469
|
+
*/
|
|
470
|
+
declare const isInstanceOf: <C extends abstract new (...args: unknown[]) => unknown>(constructor: C) => Guard<InstanceType<C>>;
|
|
464
471
|
|
|
465
472
|
/**
|
|
466
473
|
* Checks whether a value has the specified own key.
|
|
@@ -469,6 +476,13 @@ declare const isBlob: Predicate<Blob>;
|
|
|
469
476
|
* @returns Predicate narrowing to an object with the key present.
|
|
470
477
|
*/
|
|
471
478
|
declare const hasKey: <K extends PropertyKey>(key: K) => Predicate<Record<K, unknown>>;
|
|
479
|
+
/**
|
|
480
|
+
* Checks whether a value has all specified own keys.
|
|
481
|
+
*
|
|
482
|
+
* @param keys Property keys to check.
|
|
483
|
+
* @returns Predicate narrowing to an object with all keys present.
|
|
484
|
+
*/
|
|
485
|
+
declare const hasKeys: <const KS extends readonly [PropertyKey, ...PropertyKey[]]>(...keys: KS) => Predicate<Record<KS[number], unknown>>;
|
|
472
486
|
/**
|
|
473
487
|
* Builds a guard that narrows a specific key to the provided literal value.
|
|
474
488
|
*
|
|
@@ -491,4 +505,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
491
505
|
*/
|
|
492
506
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
493
507
|
|
|
494
|
-
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, assert, define, equals, equalsBy, equalsKey, guardIn, hasKey, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isInfiniteNumber, 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, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
|
508
|
+
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, 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, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
package/dist/index.d.ts
CHANGED
|
@@ -461,6 +461,13 @@ declare const isURL: Predicate<URL>;
|
|
|
461
461
|
* @returns Predicate narrowing to `Blob` when supported; otherwise always false.
|
|
462
462
|
*/
|
|
463
463
|
declare const isBlob: Predicate<Blob>;
|
|
464
|
+
/**
|
|
465
|
+
* Creates a guard that checks whether a value is an instance of `constructor`.
|
|
466
|
+
*
|
|
467
|
+
* @param constructor Constructor used as the right-hand side of `instanceof`.
|
|
468
|
+
* @returns Predicate narrowing to `InstanceType<C>`.
|
|
469
|
+
*/
|
|
470
|
+
declare const isInstanceOf: <C extends abstract new (...args: unknown[]) => unknown>(constructor: C) => Guard<InstanceType<C>>;
|
|
464
471
|
|
|
465
472
|
/**
|
|
466
473
|
* Checks whether a value has the specified own key.
|
|
@@ -469,6 +476,13 @@ declare const isBlob: Predicate<Blob>;
|
|
|
469
476
|
* @returns Predicate narrowing to an object with the key present.
|
|
470
477
|
*/
|
|
471
478
|
declare const hasKey: <K extends PropertyKey>(key: K) => Predicate<Record<K, unknown>>;
|
|
479
|
+
/**
|
|
480
|
+
* Checks whether a value has all specified own keys.
|
|
481
|
+
*
|
|
482
|
+
* @param keys Property keys to check.
|
|
483
|
+
* @returns Predicate narrowing to an object with all keys present.
|
|
484
|
+
*/
|
|
485
|
+
declare const hasKeys: <const KS extends readonly [PropertyKey, ...PropertyKey[]]>(...keys: KS) => Predicate<Record<KS[number], unknown>>;
|
|
472
486
|
/**
|
|
473
487
|
* Builds a guard that narrows a specific key to the provided literal value.
|
|
474
488
|
*
|
|
@@ -491,4 +505,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
491
505
|
*/
|
|
492
506
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
493
507
|
|
|
494
|
-
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, assert, define, equals, equalsBy, equalsKey, guardIn, hasKey, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isInfiniteNumber, 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, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
|
508
|
+
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, 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, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,7 @@ __export(index_exports, {
|
|
|
30
30
|
equalsKey: () => equalsKey,
|
|
31
31
|
guardIn: () => guardIn,
|
|
32
32
|
hasKey: () => hasKey,
|
|
33
|
+
hasKeys: () => hasKeys,
|
|
33
34
|
isArray: () => isArray,
|
|
34
35
|
isArrayBuffer: () => isArrayBuffer,
|
|
35
36
|
isAsyncIterable: () => isAsyncIterable,
|
|
@@ -42,6 +43,7 @@ __export(index_exports, {
|
|
|
42
43
|
isFiniteNumber: () => isFiniteNumber,
|
|
43
44
|
isFunction: () => isFunction,
|
|
44
45
|
isInfiniteNumber: () => isInfiniteNumber,
|
|
46
|
+
isInstanceOf: () => isInstanceOf,
|
|
45
47
|
isInteger: () => isInteger,
|
|
46
48
|
isIterable: () => isIterable,
|
|
47
49
|
isMap: () => isMap,
|
|
@@ -218,6 +220,7 @@ var isError = define(
|
|
|
218
220
|
);
|
|
219
221
|
var isURL = typeof URL !== "undefined" ? define((value) => value instanceof URL) : define(() => false);
|
|
220
222
|
var isBlob = typeof Blob !== "undefined" ? define((value) => value instanceof Blob) : define(() => false);
|
|
223
|
+
var isInstanceOf = (constructor) => define((value) => value instanceof constructor);
|
|
221
224
|
|
|
222
225
|
// src/core/equals.ts
|
|
223
226
|
function equals(target) {
|
|
@@ -389,6 +392,14 @@ function oneOfValues(...args) {
|
|
|
389
392
|
var hasKey = (key) => define(
|
|
390
393
|
(input) => isObject(input) && Object.prototype.hasOwnProperty.call(input, key)
|
|
391
394
|
);
|
|
395
|
+
var hasKeys = (...keys) => {
|
|
396
|
+
if (keys.length === 0) {
|
|
397
|
+
return define(() => false);
|
|
398
|
+
}
|
|
399
|
+
return define(
|
|
400
|
+
(input) => isObject(input) && keys.every((key) => Object.prototype.hasOwnProperty.call(input, key))
|
|
401
|
+
);
|
|
402
|
+
};
|
|
392
403
|
function narrowKeyTo(guard, key) {
|
|
393
404
|
return function(target) {
|
|
394
405
|
const keyEquals = equalsKey(key, target);
|
|
@@ -409,6 +420,7 @@ function narrowKeyTo(guard, key) {
|
|
|
409
420
|
equalsKey,
|
|
410
421
|
guardIn,
|
|
411
422
|
hasKey,
|
|
423
|
+
hasKeys,
|
|
412
424
|
isArray,
|
|
413
425
|
isArrayBuffer,
|
|
414
426
|
isAsyncIterable,
|
|
@@ -421,6 +433,7 @@ function narrowKeyTo(guard, key) {
|
|
|
421
433
|
isFiniteNumber,
|
|
422
434
|
isFunction,
|
|
423
435
|
isInfiniteNumber,
|
|
436
|
+
isInstanceOf,
|
|
424
437
|
isInteger,
|
|
425
438
|
isIterable,
|
|
426
439
|
isMap,
|
package/dist/index.mjs
CHANGED
|
@@ -130,6 +130,7 @@ var isError = define(
|
|
|
130
130
|
);
|
|
131
131
|
var isURL = typeof URL !== "undefined" ? define((value) => value instanceof URL) : define(() => false);
|
|
132
132
|
var isBlob = typeof Blob !== "undefined" ? define((value) => value instanceof Blob) : define(() => false);
|
|
133
|
+
var isInstanceOf = (constructor) => define((value) => value instanceof constructor);
|
|
133
134
|
|
|
134
135
|
// src/core/equals.ts
|
|
135
136
|
function equals(target) {
|
|
@@ -301,6 +302,14 @@ function oneOfValues(...args) {
|
|
|
301
302
|
var hasKey = (key) => define(
|
|
302
303
|
(input) => isObject(input) && Object.prototype.hasOwnProperty.call(input, key)
|
|
303
304
|
);
|
|
305
|
+
var hasKeys = (...keys) => {
|
|
306
|
+
if (keys.length === 0) {
|
|
307
|
+
return define(() => false);
|
|
308
|
+
}
|
|
309
|
+
return define(
|
|
310
|
+
(input) => isObject(input) && keys.every((key) => Object.prototype.hasOwnProperty.call(input, key))
|
|
311
|
+
);
|
|
312
|
+
};
|
|
304
313
|
function narrowKeyTo(guard, key) {
|
|
305
314
|
return function(target) {
|
|
306
315
|
const keyEquals = equalsKey(key, target);
|
|
@@ -320,6 +329,7 @@ export {
|
|
|
320
329
|
equalsKey,
|
|
321
330
|
guardIn,
|
|
322
331
|
hasKey,
|
|
332
|
+
hasKeys,
|
|
323
333
|
isArray,
|
|
324
334
|
isArrayBuffer,
|
|
325
335
|
isAsyncIterable,
|
|
@@ -332,6 +342,7 @@ export {
|
|
|
332
342
|
isFiniteNumber,
|
|
333
343
|
isFunction,
|
|
334
344
|
isInfiniteNumber,
|
|
345
|
+
isInstanceOf,
|
|
335
346
|
isInteger,
|
|
336
347
|
isIterable,
|
|
337
348
|
isMap,
|