is-kit 1.1.12 → 1.2.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 +10 -5
- package/dist/index.d.mts +19 -1
- package/dist/index.d.ts +19 -1
- package/dist/index.js +14 -0
- package/dist/index.mjs +12 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -162,15 +162,15 @@ isInfiniteNumber(1); // false
|
|
|
162
162
|
- **Define once**: `define<T>(fn)` turns a plain function into a type guard.
|
|
163
163
|
- **Upgrade predicates**: `predicateToRefine(fn)` adds narrowing.
|
|
164
164
|
- **Compose freely**: `and`, `or`, `not`, `oneOf`, `arrayOf`, `struct` …
|
|
165
|
-
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`, `narrowKeyTo`.
|
|
165
|
+
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`, `assert`, `hasKey`, `narrowKeyTo`.
|
|
166
166
|
|
|
167
|
-
### Key
|
|
167
|
+
### Key Helpers
|
|
168
168
|
|
|
169
|
-
`
|
|
170
|
-
|
|
169
|
+
Use `hasKey` to check for a required own property before refining, and
|
|
170
|
+
`narrowKeyTo` when you need to narrow a property to a specific literal value.
|
|
171
171
|
|
|
172
172
|
```ts
|
|
173
|
-
import { narrowKeyTo, or, struct } from 'is-kit';
|
|
173
|
+
import { hasKey, isString, isNumber, narrowKeyTo, or, struct } from 'is-kit';
|
|
174
174
|
|
|
175
175
|
// Base guard (e.g., via struct)
|
|
176
176
|
type User = { id: string; age: number; role: 'admin' | 'guest' | 'trial' };
|
|
@@ -180,12 +180,17 @@ const isUser = struct({
|
|
|
180
180
|
role: oneOfValues('admin', 'guest', 'trial')
|
|
181
181
|
});
|
|
182
182
|
|
|
183
|
+
const hasRole = hasKey('role');
|
|
183
184
|
const byRole = narrowKeyTo(isUser, 'role');
|
|
184
185
|
const isGuest = byRole('guest'); // Readonly<User> & { role: 'guest' }
|
|
185
186
|
const isTrial = byRole('trial'); // Readonly<User> & { role: 'trial' }
|
|
186
187
|
const isGuestOrTrial = or(isGuest, isTrial);
|
|
187
188
|
|
|
188
189
|
declare const value: unknown;
|
|
190
|
+
if (hasRole(value)) {
|
|
191
|
+
value.role;
|
|
192
|
+
}
|
|
193
|
+
|
|
189
194
|
if (isGuestOrTrial(value)) {
|
|
190
195
|
// value.role is 'guest' | 'trial'
|
|
191
196
|
}
|
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
|
*
|
|
@@ -451,6 +462,13 @@ declare const isURL: Predicate<URL>;
|
|
|
451
462
|
*/
|
|
452
463
|
declare const isBlob: Predicate<Blob>;
|
|
453
464
|
|
|
465
|
+
/**
|
|
466
|
+
* Checks whether a value has the specified own key.
|
|
467
|
+
*
|
|
468
|
+
* @param key Property key to check.
|
|
469
|
+
* @returns Predicate narrowing to an object with the key present.
|
|
470
|
+
*/
|
|
471
|
+
declare const hasKey: <K extends PropertyKey>(key: K) => Predicate<Record<K, unknown>>;
|
|
454
472
|
/**
|
|
455
473
|
* Builds a guard that narrows a specific key to the provided literal value.
|
|
456
474
|
*
|
|
@@ -473,4 +491,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
473
491
|
*/
|
|
474
492
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
475
493
|
|
|
476
|
-
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, 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 };
|
|
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 };
|
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
|
*
|
|
@@ -451,6 +462,13 @@ declare const isURL: Predicate<URL>;
|
|
|
451
462
|
*/
|
|
452
463
|
declare const isBlob: Predicate<Blob>;
|
|
453
464
|
|
|
465
|
+
/**
|
|
466
|
+
* Checks whether a value has the specified own key.
|
|
467
|
+
*
|
|
468
|
+
* @param key Property key to check.
|
|
469
|
+
* @returns Predicate narrowing to an object with the key present.
|
|
470
|
+
*/
|
|
471
|
+
declare const hasKey: <K extends PropertyKey>(key: K) => Predicate<Record<K, unknown>>;
|
|
454
472
|
/**
|
|
455
473
|
* Builds a guard that narrows a specific key to the provided literal value.
|
|
456
474
|
*
|
|
@@ -473,4 +491,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
473
491
|
*/
|
|
474
492
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
475
493
|
|
|
476
|
-
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, 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 };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -23,11 +23,13 @@ __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,
|
|
29
30
|
equalsKey: () => equalsKey,
|
|
30
31
|
guardIn: () => guardIn,
|
|
32
|
+
hasKey: () => hasKey,
|
|
31
33
|
isArray: () => isArray,
|
|
32
34
|
isArrayBuffer: () => isArrayBuffer,
|
|
33
35
|
isAsyncIterable: () => isAsyncIterable,
|
|
@@ -92,6 +94,13 @@ function define(fn) {
|
|
|
92
94
|
// src/core/predicate.ts
|
|
93
95
|
var predicateToRefine = (fn) => (value) => fn(value);
|
|
94
96
|
|
|
97
|
+
// src/core/assert.ts
|
|
98
|
+
var DEFAULT_ASSERT_MESSAGE = "Assertion failed";
|
|
99
|
+
function assert(fn, value, message) {
|
|
100
|
+
if (fn(value)) return;
|
|
101
|
+
throw new Error(message ?? DEFAULT_ASSERT_MESSAGE);
|
|
102
|
+
}
|
|
103
|
+
|
|
95
104
|
// src/utils/to-boolean-predicates.ts
|
|
96
105
|
var toBooleanPredicates = (predicates) => predicates;
|
|
97
106
|
|
|
@@ -377,6 +386,9 @@ function oneOfValues(...args) {
|
|
|
377
386
|
}
|
|
378
387
|
|
|
379
388
|
// src/core/key.ts
|
|
389
|
+
var hasKey = (key) => define(
|
|
390
|
+
(input) => isObject(input) && Object.prototype.hasOwnProperty.call(input, key)
|
|
391
|
+
);
|
|
380
392
|
function narrowKeyTo(guard, key) {
|
|
381
393
|
return function(target) {
|
|
382
394
|
const keyEquals = equalsKey(key, target);
|
|
@@ -390,11 +402,13 @@ function narrowKeyTo(guard, key) {
|
|
|
390
402
|
and,
|
|
391
403
|
andAll,
|
|
392
404
|
arrayOf,
|
|
405
|
+
assert,
|
|
393
406
|
define,
|
|
394
407
|
equals,
|
|
395
408
|
equalsBy,
|
|
396
409
|
equalsKey,
|
|
397
410
|
guardIn,
|
|
411
|
+
hasKey,
|
|
398
412
|
isArray,
|
|
399
413
|
isArrayBuffer,
|
|
400
414
|
isAsyncIterable,
|
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
|
|
|
@@ -291,6 +298,9 @@ function oneOfValues(...args) {
|
|
|
291
298
|
}
|
|
292
299
|
|
|
293
300
|
// src/core/key.ts
|
|
301
|
+
var hasKey = (key) => define(
|
|
302
|
+
(input) => isObject(input) && Object.prototype.hasOwnProperty.call(input, key)
|
|
303
|
+
);
|
|
294
304
|
function narrowKeyTo(guard, key) {
|
|
295
305
|
return function(target) {
|
|
296
306
|
const keyEquals = equalsKey(key, target);
|
|
@@ -303,11 +313,13 @@ export {
|
|
|
303
313
|
and,
|
|
304
314
|
andAll,
|
|
305
315
|
arrayOf,
|
|
316
|
+
assert,
|
|
306
317
|
define,
|
|
307
318
|
equals,
|
|
308
319
|
equalsBy,
|
|
309
320
|
equalsKey,
|
|
310
321
|
guardIn,
|
|
322
|
+
hasKey,
|
|
311
323
|
isArray,
|
|
312
324
|
isArrayBuffer,
|
|
313
325
|
isAsyncIterable,
|