is-kit 1.1.13 → 1.3.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 +20 -1
- package/dist/index.d.mts +19 -1
- package/dist/index.d.ts +19 -1
- package/dist/index.js +12 -0
- package/dist/index.mjs +10 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -157,12 +157,31 @@ 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`, `hasKey`, `narrowKeyTo`.
|
|
184
|
+
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`, `assert`, `hasKey`, `narrowKeyTo`.
|
|
166
185
|
|
|
167
186
|
### Key Helpers
|
|
168
187
|
|
package/dist/index.d.mts
CHANGED
|
@@ -40,6 +40,17 @@ declare function define<T>(fn: (value: unknown) => boolean): Predicate<T>;
|
|
|
40
40
|
*/
|
|
41
41
|
declare const predicateToRefine: <A>(fn: (value: A) => boolean) => Refine<A, A>;
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Asserts that a value satisfies a guard or refinement, otherwise throws.
|
|
45
|
+
*
|
|
46
|
+
* @param guard Guard/refinement to evaluate.
|
|
47
|
+
* @param value Value to assert.
|
|
48
|
+
* @param message Optional error message when assertion fails.
|
|
49
|
+
* @returns Nothing when assertion passes; throws an `Error` on failure.
|
|
50
|
+
*/
|
|
51
|
+
declare function assert<T>(guard: Guard<T>, value: unknown, message?: string): asserts value is T;
|
|
52
|
+
declare function assert<A, B extends A>(refine: Refine<A, B>, value: A, message?: string): asserts value is B;
|
|
53
|
+
|
|
43
54
|
/**
|
|
44
55
|
* Combines a precondition guard with an additional refinement to narrow the type.
|
|
45
56
|
*
|
|
@@ -450,6 +461,13 @@ declare const isURL: Predicate<URL>;
|
|
|
450
461
|
* @returns Predicate narrowing to `Blob` when supported; otherwise always false.
|
|
451
462
|
*/
|
|
452
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>>;
|
|
453
471
|
|
|
454
472
|
/**
|
|
455
473
|
* Checks whether a value has the specified own key.
|
|
@@ -480,4 +498,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
480
498
|
*/
|
|
481
499
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
482
500
|
|
|
483
|
-
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, 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 };
|
|
501
|
+
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, 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
|
@@ -40,6 +40,17 @@ declare function define<T>(fn: (value: unknown) => boolean): Predicate<T>;
|
|
|
40
40
|
*/
|
|
41
41
|
declare const predicateToRefine: <A>(fn: (value: A) => boolean) => Refine<A, A>;
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Asserts that a value satisfies a guard or refinement, otherwise throws.
|
|
45
|
+
*
|
|
46
|
+
* @param guard Guard/refinement to evaluate.
|
|
47
|
+
* @param value Value to assert.
|
|
48
|
+
* @param message Optional error message when assertion fails.
|
|
49
|
+
* @returns Nothing when assertion passes; throws an `Error` on failure.
|
|
50
|
+
*/
|
|
51
|
+
declare function assert<T>(guard: Guard<T>, value: unknown, message?: string): asserts value is T;
|
|
52
|
+
declare function assert<A, B extends A>(refine: Refine<A, B>, value: A, message?: string): asserts value is B;
|
|
53
|
+
|
|
43
54
|
/**
|
|
44
55
|
* Combines a precondition guard with an additional refinement to narrow the type.
|
|
45
56
|
*
|
|
@@ -450,6 +461,13 @@ declare const isURL: Predicate<URL>;
|
|
|
450
461
|
* @returns Predicate narrowing to `Blob` when supported; otherwise always false.
|
|
451
462
|
*/
|
|
452
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>>;
|
|
453
471
|
|
|
454
472
|
/**
|
|
455
473
|
* Checks whether a value has the specified own key.
|
|
@@ -480,4 +498,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
480
498
|
*/
|
|
481
499
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
482
500
|
|
|
483
|
-
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, 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 };
|
|
501
|
+
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, 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
|
@@ -23,6 +23,7 @@ __export(index_exports, {
|
|
|
23
23
|
and: () => and,
|
|
24
24
|
andAll: () => andAll,
|
|
25
25
|
arrayOf: () => arrayOf,
|
|
26
|
+
assert: () => assert,
|
|
26
27
|
define: () => define,
|
|
27
28
|
equals: () => equals,
|
|
28
29
|
equalsBy: () => equalsBy,
|
|
@@ -41,6 +42,7 @@ __export(index_exports, {
|
|
|
41
42
|
isFiniteNumber: () => isFiniteNumber,
|
|
42
43
|
isFunction: () => isFunction,
|
|
43
44
|
isInfiniteNumber: () => isInfiniteNumber,
|
|
45
|
+
isInstanceOf: () => isInstanceOf,
|
|
44
46
|
isInteger: () => isInteger,
|
|
45
47
|
isIterable: () => isIterable,
|
|
46
48
|
isMap: () => isMap,
|
|
@@ -93,6 +95,13 @@ function define(fn) {
|
|
|
93
95
|
// src/core/predicate.ts
|
|
94
96
|
var predicateToRefine = (fn) => (value) => fn(value);
|
|
95
97
|
|
|
98
|
+
// src/core/assert.ts
|
|
99
|
+
var DEFAULT_ASSERT_MESSAGE = "Assertion failed";
|
|
100
|
+
function assert(fn, value, message) {
|
|
101
|
+
if (fn(value)) return;
|
|
102
|
+
throw new Error(message ?? DEFAULT_ASSERT_MESSAGE);
|
|
103
|
+
}
|
|
104
|
+
|
|
96
105
|
// src/utils/to-boolean-predicates.ts
|
|
97
106
|
var toBooleanPredicates = (predicates) => predicates;
|
|
98
107
|
|
|
@@ -210,6 +219,7 @@ var isError = define(
|
|
|
210
219
|
);
|
|
211
220
|
var isURL = typeof URL !== "undefined" ? define((value) => value instanceof URL) : define(() => false);
|
|
212
221
|
var isBlob = typeof Blob !== "undefined" ? define((value) => value instanceof Blob) : define(() => false);
|
|
222
|
+
var isInstanceOf = (constructor) => define((value) => value instanceof constructor);
|
|
213
223
|
|
|
214
224
|
// src/core/equals.ts
|
|
215
225
|
function equals(target) {
|
|
@@ -394,6 +404,7 @@ function narrowKeyTo(guard, key) {
|
|
|
394
404
|
and,
|
|
395
405
|
andAll,
|
|
396
406
|
arrayOf,
|
|
407
|
+
assert,
|
|
397
408
|
define,
|
|
398
409
|
equals,
|
|
399
410
|
equalsBy,
|
|
@@ -412,6 +423,7 @@ function narrowKeyTo(guard, key) {
|
|
|
412
423
|
isFiniteNumber,
|
|
413
424
|
isFunction,
|
|
414
425
|
isInfiniteNumber,
|
|
426
|
+
isInstanceOf,
|
|
415
427
|
isInteger,
|
|
416
428
|
isIterable,
|
|
417
429
|
isMap,
|
package/dist/index.mjs
CHANGED
|
@@ -6,6 +6,13 @@ function define(fn) {
|
|
|
6
6
|
// src/core/predicate.ts
|
|
7
7
|
var predicateToRefine = (fn) => (value) => fn(value);
|
|
8
8
|
|
|
9
|
+
// src/core/assert.ts
|
|
10
|
+
var DEFAULT_ASSERT_MESSAGE = "Assertion failed";
|
|
11
|
+
function assert(fn, value, message) {
|
|
12
|
+
if (fn(value)) return;
|
|
13
|
+
throw new Error(message ?? DEFAULT_ASSERT_MESSAGE);
|
|
14
|
+
}
|
|
15
|
+
|
|
9
16
|
// src/utils/to-boolean-predicates.ts
|
|
10
17
|
var toBooleanPredicates = (predicates) => predicates;
|
|
11
18
|
|
|
@@ -123,6 +130,7 @@ var isError = define(
|
|
|
123
130
|
);
|
|
124
131
|
var isURL = typeof URL !== "undefined" ? define((value) => value instanceof URL) : define(() => false);
|
|
125
132
|
var isBlob = typeof Blob !== "undefined" ? define((value) => value instanceof Blob) : define(() => false);
|
|
133
|
+
var isInstanceOf = (constructor) => define((value) => value instanceof constructor);
|
|
126
134
|
|
|
127
135
|
// src/core/equals.ts
|
|
128
136
|
function equals(target) {
|
|
@@ -306,6 +314,7 @@ export {
|
|
|
306
314
|
and,
|
|
307
315
|
andAll,
|
|
308
316
|
arrayOf,
|
|
317
|
+
assert,
|
|
309
318
|
define,
|
|
310
319
|
equals,
|
|
311
320
|
equalsBy,
|
|
@@ -324,6 +333,7 @@ export {
|
|
|
324
333
|
isFiniteNumber,
|
|
325
334
|
isFunction,
|
|
326
335
|
isInfiniteNumber,
|
|
336
|
+
isInstanceOf,
|
|
327
337
|
isInteger,
|
|
328
338
|
isIterable,
|
|
329
339
|
isMap,
|