is-kit 1.1.11 → 1.1.13

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 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`, `hasKey`, `narrowKeyTo`.
166
166
 
167
- ### Key Narrowing
167
+ ### Key Helpers
168
168
 
169
- `equalsBy` preserves the base type and does not narrow selected fields to literals.
170
- When you need to narrow a property to a specific literal value, use `narrowKeyTo`.
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
@@ -451,6 +451,13 @@ declare const isURL: Predicate<URL>;
451
451
  */
452
452
  declare const isBlob: Predicate<Blob>;
453
453
 
454
+ /**
455
+ * Checks whether a value has the specified own key.
456
+ *
457
+ * @param key Property key to check.
458
+ * @returns Predicate narrowing to an object with the key present.
459
+ */
460
+ declare const hasKey: <K extends PropertyKey>(key: K) => Predicate<Record<K, unknown>>;
454
461
  /**
455
462
  * Builds a guard that narrows a specific key to the provided literal value.
456
463
  *
@@ -466,11 +473,11 @@ declare const isBlob: Predicate<Blob>;
466
473
  declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <const T extends A[K]>(target: T) => Predicate<A & Record<K, T>>;
467
474
 
468
475
  /**
469
- * Converts an array of guards to plain boolean predicates for iteration helpers.
476
+ * Converts predicates to plain boolean predicates for iteration helpers.
470
477
  *
471
- * @param guards Guards to be treated as simple predicates.
478
+ * @param predicates Predicates to be treated as simple boolean functions.
472
479
  * @returns Readonly array of boolean-returning functions.
473
480
  */
474
- declare const toBooleanPredicates: (guards: readonly Guard<unknown>[]) => ReadonlyArray<(value: unknown) => boolean>;
481
+ declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
475
482
 
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 };
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 };
package/dist/index.d.ts CHANGED
@@ -451,6 +451,13 @@ declare const isURL: Predicate<URL>;
451
451
  */
452
452
  declare const isBlob: Predicate<Blob>;
453
453
 
454
+ /**
455
+ * Checks whether a value has the specified own key.
456
+ *
457
+ * @param key Property key to check.
458
+ * @returns Predicate narrowing to an object with the key present.
459
+ */
460
+ declare const hasKey: <K extends PropertyKey>(key: K) => Predicate<Record<K, unknown>>;
454
461
  /**
455
462
  * Builds a guard that narrows a specific key to the provided literal value.
456
463
  *
@@ -466,11 +473,11 @@ declare const isBlob: Predicate<Blob>;
466
473
  declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <const T extends A[K]>(target: T) => Predicate<A & Record<K, T>>;
467
474
 
468
475
  /**
469
- * Converts an array of guards to plain boolean predicates for iteration helpers.
476
+ * Converts predicates to plain boolean predicates for iteration helpers.
470
477
  *
471
- * @param guards Guards to be treated as simple predicates.
478
+ * @param predicates Predicates to be treated as simple boolean functions.
472
479
  * @returns Readonly array of boolean-returning functions.
473
480
  */
474
- declare const toBooleanPredicates: (guards: readonly Guard<unknown>[]) => ReadonlyArray<(value: unknown) => boolean>;
481
+ declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
475
482
 
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 };
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 };
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ __export(index_exports, {
28
28
  equalsBy: () => equalsBy,
29
29
  equalsKey: () => equalsKey,
30
30
  guardIn: () => guardIn,
31
+ hasKey: () => hasKey,
31
32
  isArray: () => isArray,
32
33
  isArrayBuffer: () => isArrayBuffer,
33
34
  isAsyncIterable: () => isAsyncIterable,
@@ -93,7 +94,7 @@ function define(fn) {
93
94
  var predicateToRefine = (fn) => (value) => fn(value);
94
95
 
95
96
  // src/utils/to-boolean-predicates.ts
96
- var toBooleanPredicates = (guards) => guards;
97
+ var toBooleanPredicates = (predicates) => predicates;
97
98
 
98
99
  // src/core/logic.ts
99
100
  function and(precondition, condition) {
@@ -105,9 +106,9 @@ function and(precondition, condition) {
105
106
  };
106
107
  }
107
108
  function andAll(precondition, ...steps) {
108
- const boolSteps = steps;
109
109
  return function(input) {
110
- return precondition(input) && boolSteps.every((step) => step(input));
110
+ if (!precondition(input)) return false;
111
+ return applyRefinements(input, steps);
111
112
  };
112
113
  }
113
114
  function or(...guards) {
@@ -126,6 +127,13 @@ function guardIn() {
126
127
  function not(fn) {
127
128
  return (input) => !fn(input);
128
129
  }
130
+ function applyRefinements(value, steps) {
131
+ if (!steps || steps.length === 0) return true;
132
+ for (const step of steps) {
133
+ if (!step(value)) return false;
134
+ }
135
+ return true;
136
+ }
129
137
 
130
138
  // src/core/nullish.ts
131
139
  function nullable(fn) {
@@ -370,6 +378,9 @@ function oneOfValues(...args) {
370
378
  }
371
379
 
372
380
  // src/core/key.ts
381
+ var hasKey = (key) => define(
382
+ (input) => isObject(input) && Object.prototype.hasOwnProperty.call(input, key)
383
+ );
373
384
  function narrowKeyTo(guard, key) {
374
385
  return function(target) {
375
386
  const keyEquals = equalsKey(key, target);
@@ -388,6 +399,7 @@ function narrowKeyTo(guard, key) {
388
399
  equalsBy,
389
400
  equalsKey,
390
401
  guardIn,
402
+ hasKey,
391
403
  isArray,
392
404
  isArrayBuffer,
393
405
  isAsyncIterable,
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ function define(fn) {
7
7
  var predicateToRefine = (fn) => (value) => fn(value);
8
8
 
9
9
  // src/utils/to-boolean-predicates.ts
10
- var toBooleanPredicates = (guards) => guards;
10
+ var toBooleanPredicates = (predicates) => predicates;
11
11
 
12
12
  // src/core/logic.ts
13
13
  function and(precondition, condition) {
@@ -19,9 +19,9 @@ function and(precondition, condition) {
19
19
  };
20
20
  }
21
21
  function andAll(precondition, ...steps) {
22
- const boolSteps = steps;
23
22
  return function(input) {
24
- return precondition(input) && boolSteps.every((step) => step(input));
23
+ if (!precondition(input)) return false;
24
+ return applyRefinements(input, steps);
25
25
  };
26
26
  }
27
27
  function or(...guards) {
@@ -40,6 +40,13 @@ function guardIn() {
40
40
  function not(fn) {
41
41
  return (input) => !fn(input);
42
42
  }
43
+ function applyRefinements(value, steps) {
44
+ if (!steps || steps.length === 0) return true;
45
+ for (const step of steps) {
46
+ if (!step(value)) return false;
47
+ }
48
+ return true;
49
+ }
43
50
 
44
51
  // src/core/nullish.ts
45
52
  function nullable(fn) {
@@ -284,6 +291,9 @@ function oneOfValues(...args) {
284
291
  }
285
292
 
286
293
  // src/core/key.ts
294
+ var hasKey = (key) => define(
295
+ (input) => isObject(input) && Object.prototype.hasOwnProperty.call(input, key)
296
+ );
287
297
  function narrowKeyTo(guard, key) {
288
298
  return function(target) {
289
299
  const keyEquals = equalsKey(key, target);
@@ -301,6 +311,7 @@ export {
301
311
  equalsBy,
302
312
  equalsKey,
303
313
  guardIn,
314
+ hasKey,
304
315
  isArray,
305
316
  isArrayBuffer,
306
317
  isAsyncIterable,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "is-kit",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "Make 'isXXX' easier. Let's make your code type safe and more readable!",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",