is-kit 1.3.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 +18 -3
- package/dist/index.d.mts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +10 -0
- package/dist/index.mjs +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -181,15 +181,24 @@ isAnimal({}); // false
|
|
|
181
181
|
- **Define once**: `define<T>(fn)` turns a plain function into a type guard.
|
|
182
182
|
- **Upgrade predicates**: `predicateToRefine(fn)` adds narrowing.
|
|
183
183
|
- **Compose freely**: `and`, `or`, `not`, `oneOf`, `arrayOf`, `struct` …
|
|
184
|
-
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`, `assert`, `hasKey`, `narrowKeyTo`.
|
|
184
|
+
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`, `assert`, `hasKey`, `hasKeys`, `narrowKeyTo`.
|
|
185
185
|
|
|
186
186
|
### Key Helpers
|
|
187
187
|
|
|
188
|
-
Use `hasKey` to check
|
|
188
|
+
Use `hasKey`/`hasKeys` to check required own properties before refining, and
|
|
189
189
|
`narrowKeyTo` when you need to narrow a property to a specific literal value.
|
|
190
190
|
|
|
191
191
|
```ts
|
|
192
|
-
import {
|
|
192
|
+
import {
|
|
193
|
+
hasKey,
|
|
194
|
+
hasKeys,
|
|
195
|
+
isString,
|
|
196
|
+
isNumber,
|
|
197
|
+
narrowKeyTo,
|
|
198
|
+
oneOfValues,
|
|
199
|
+
or,
|
|
200
|
+
struct
|
|
201
|
+
} from 'is-kit';
|
|
193
202
|
|
|
194
203
|
// Base guard (e.g., via struct)
|
|
195
204
|
type User = { id: string; age: number; role: 'admin' | 'guest' | 'trial' };
|
|
@@ -200,6 +209,7 @@ const isUser = struct({
|
|
|
200
209
|
});
|
|
201
210
|
|
|
202
211
|
const hasRole = hasKey('role');
|
|
212
|
+
const hasRoleAndId = hasKeys('role', 'id');
|
|
203
213
|
const byRole = narrowKeyTo(isUser, 'role');
|
|
204
214
|
const isGuest = byRole('guest'); // Readonly<User> & { role: 'guest' }
|
|
205
215
|
const isTrial = byRole('trial'); // Readonly<User> & { role: 'trial' }
|
|
@@ -210,6 +220,11 @@ if (hasRole(value)) {
|
|
|
210
220
|
value.role;
|
|
211
221
|
}
|
|
212
222
|
|
|
223
|
+
if (hasRoleAndId(value)) {
|
|
224
|
+
value.role;
|
|
225
|
+
value.id;
|
|
226
|
+
}
|
|
227
|
+
|
|
213
228
|
if (isGuestOrTrial(value)) {
|
|
214
229
|
// value.role is 'guest' | 'trial'
|
|
215
230
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -476,6 +476,13 @@ declare const isInstanceOf: <C extends abstract new (...args: unknown[]) => unkn
|
|
|
476
476
|
* @returns Predicate narrowing to an object with the key present.
|
|
477
477
|
*/
|
|
478
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>>;
|
|
479
486
|
/**
|
|
480
487
|
* Builds a guard that narrows a specific key to the provided literal value.
|
|
481
488
|
*
|
|
@@ -498,4 +505,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
498
505
|
*/
|
|
499
506
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
500
507
|
|
|
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 };
|
|
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
|
@@ -476,6 +476,13 @@ declare const isInstanceOf: <C extends abstract new (...args: unknown[]) => unkn
|
|
|
476
476
|
* @returns Predicate narrowing to an object with the key present.
|
|
477
477
|
*/
|
|
478
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>>;
|
|
479
486
|
/**
|
|
480
487
|
* Builds a guard that narrows a specific key to the provided literal value.
|
|
481
488
|
*
|
|
@@ -498,4 +505,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
|
|
|
498
505
|
*/
|
|
499
506
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
500
507
|
|
|
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 };
|
|
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,
|
|
@@ -391,6 +392,14 @@ function oneOfValues(...args) {
|
|
|
391
392
|
var hasKey = (key) => define(
|
|
392
393
|
(input) => isObject(input) && Object.prototype.hasOwnProperty.call(input, key)
|
|
393
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
|
+
};
|
|
394
403
|
function narrowKeyTo(guard, key) {
|
|
395
404
|
return function(target) {
|
|
396
405
|
const keyEquals = equalsKey(key, target);
|
|
@@ -411,6 +420,7 @@ function narrowKeyTo(guard, key) {
|
|
|
411
420
|
equalsKey,
|
|
412
421
|
guardIn,
|
|
413
422
|
hasKey,
|
|
423
|
+
hasKeys,
|
|
414
424
|
isArray,
|
|
415
425
|
isArrayBuffer,
|
|
416
426
|
isAsyncIterable,
|
package/dist/index.mjs
CHANGED
|
@@ -302,6 +302,14 @@ function oneOfValues(...args) {
|
|
|
302
302
|
var hasKey = (key) => define(
|
|
303
303
|
(input) => isObject(input) && Object.prototype.hasOwnProperty.call(input, key)
|
|
304
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
|
+
};
|
|
305
313
|
function narrowKeyTo(guard, key) {
|
|
306
314
|
return function(target) {
|
|
307
315
|
const keyEquals = equalsKey(key, target);
|
|
@@ -321,6 +329,7 @@ export {
|
|
|
321
329
|
equalsKey,
|
|
322
330
|
guardIn,
|
|
323
331
|
hasKey,
|
|
332
|
+
hasKeys,
|
|
324
333
|
isArray,
|
|
325
334
|
isArrayBuffer,
|
|
326
335
|
isAsyncIterable,
|