is-kit 1.1.2 → 1.1.3

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
@@ -116,6 +116,19 @@ Composed guards stay reusable:
116
116
 
117
117
  When validating complex shapes, reach for `struct` — and friends like `arrayOf`, `recordOf`, or `oneOf`.
118
118
 
119
+ ### Primitive guards
120
+
121
+ Built-in primitives: `isString`, `isNumber` (finite), `isBoolean`, `isBigInt`, `isSymbol`, `isUndefined`, `isNull` — and a preset `isPrimitive` for any primitive.
122
+
123
+ ```ts
124
+ import { isPrimitive, isNumber } from 'is-kit';
125
+
126
+ isPrimitive('x'); // true
127
+ isPrimitive(123); // true
128
+ isPrimitive(NaN); // true (use isNumber for finite only)
129
+ isPrimitive({}); // false
130
+ ```
131
+
119
132
  ## Core Ideas
120
133
 
121
134
  - **Define once**: `define<T>(fn)` turns a plain function into a type guard.
package/dist/index.d.mts CHANGED
@@ -218,6 +218,15 @@ declare const isUndefined: Predicate<undefined>;
218
218
  * @returns Predicate narrowing to `null`.
219
219
  */
220
220
  declare const isNull: Predicate<null>;
221
+ /**
222
+ * Checks whether a value is a JavaScript primitive.
223
+ *
224
+ * Primitives: string, number, boolean, bigint, symbol, undefined, null.
225
+ * Note: `number` here includes NaN and ±Infinity (use `isNumber` for finite only).
226
+ *
227
+ * @returns Predicate narrowing to `Primitive`.
228
+ */
229
+ declare const isPrimitive: Guard<string | number | bigint | boolean | symbol | null | undefined>;
221
230
 
222
231
  /**
223
232
  * Validates an array where every element satisfies the provided guard.
@@ -413,4 +422,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
413
422
  */
414
423
  declare const toBooleanPredicates: (guards: readonly Guard<unknown>[]) => ReadonlyArray<(value: unknown) => boolean>;
415
424
 
416
- 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, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
425
+ 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, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPrimitive, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
package/dist/index.d.ts CHANGED
@@ -218,6 +218,15 @@ declare const isUndefined: Predicate<undefined>;
218
218
  * @returns Predicate narrowing to `null`.
219
219
  */
220
220
  declare const isNull: Predicate<null>;
221
+ /**
222
+ * Checks whether a value is a JavaScript primitive.
223
+ *
224
+ * Primitives: string, number, boolean, bigint, symbol, undefined, null.
225
+ * Note: `number` here includes NaN and ±Infinity (use `isNumber` for finite only).
226
+ *
227
+ * @returns Predicate narrowing to `Primitive`.
228
+ */
229
+ declare const isPrimitive: Guard<string | number | bigint | boolean | symbol | null | undefined>;
221
230
 
222
231
  /**
223
232
  * Validates an array where every element satisfies the provided guard.
@@ -413,4 +422,4 @@ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <co
413
422
  */
414
423
  declare const toBooleanPredicates: (guards: readonly Guard<unknown>[]) => ReadonlyArray<(value: unknown) => boolean>;
415
424
 
416
- 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, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
425
+ 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, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPrimitive, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, narrowKeyTo, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
package/dist/index.js CHANGED
@@ -46,6 +46,7 @@ __export(index_exports, {
46
46
  isNumberPrimitive: () => isNumberPrimitive,
47
47
  isObject: () => isObject,
48
48
  isPlainObject: () => isPlainObject,
49
+ isPrimitive: () => isPrimitive,
49
50
  isPromiseLike: () => isPromiseLike,
50
51
  isRegExp: () => isRegExp,
51
52
  isSet: () => isSet,
@@ -238,6 +239,15 @@ var isBigInt = define((value) => typeof value === "bigint");
238
239
  var isSymbol = define((value) => typeof value === "symbol");
239
240
  var isUndefined = define((value) => value === void 0);
240
241
  var isNull = define((value) => value === null);
242
+ var isPrimitive = or(
243
+ isString,
244
+ isNumberPrimitive,
245
+ isBoolean,
246
+ isBigInt,
247
+ isSymbol,
248
+ isUndefined,
249
+ isNull
250
+ );
241
251
 
242
252
  // src/core/combinators/array.ts
243
253
  function arrayOf(elementGuard) {
@@ -280,16 +290,17 @@ function recordOf(keyFunction, valueFunction) {
280
290
 
281
291
  // src/core/combinators/struct.ts
282
292
  function struct(schema, options) {
293
+ const schemaKeys = Object.keys(schema);
294
+ const allowed = options?.exact ? new Set(schemaKeys) : null;
283
295
  return (input) => {
284
296
  if (!isPlainObject(input)) return false;
285
297
  const obj = input;
286
- for (const key of Object.keys(schema)) {
298
+ for (const key of schemaKeys) {
287
299
  if (!(key in obj)) return false;
288
300
  const guard = schema[key];
289
301
  if (!guard(obj[key])) return false;
290
302
  }
291
- if (options?.exact) {
292
- const allowed = new Set(Object.keys(schema));
303
+ if (allowed) {
293
304
  for (const key of Object.keys(obj)) {
294
305
  if (!allowed.has(key)) return false;
295
306
  }
@@ -350,6 +361,7 @@ function narrowKeyTo(guard, key) {
350
361
  isNumberPrimitive,
351
362
  isObject,
352
363
  isPlainObject,
364
+ isPrimitive,
353
365
  isPromiseLike,
354
366
  isRegExp,
355
367
  isSet,
package/dist/index.mjs CHANGED
@@ -160,6 +160,15 @@ var isBigInt = define((value) => typeof value === "bigint");
160
160
  var isSymbol = define((value) => typeof value === "symbol");
161
161
  var isUndefined = define((value) => value === void 0);
162
162
  var isNull = define((value) => value === null);
163
+ var isPrimitive = or(
164
+ isString,
165
+ isNumberPrimitive,
166
+ isBoolean,
167
+ isBigInt,
168
+ isSymbol,
169
+ isUndefined,
170
+ isNull
171
+ );
163
172
 
164
173
  // src/core/combinators/array.ts
165
174
  function arrayOf(elementGuard) {
@@ -202,16 +211,17 @@ function recordOf(keyFunction, valueFunction) {
202
211
 
203
212
  // src/core/combinators/struct.ts
204
213
  function struct(schema, options) {
214
+ const schemaKeys = Object.keys(schema);
215
+ const allowed = options?.exact ? new Set(schemaKeys) : null;
205
216
  return (input) => {
206
217
  if (!isPlainObject(input)) return false;
207
218
  const obj = input;
208
- for (const key of Object.keys(schema)) {
219
+ for (const key of schemaKeys) {
209
220
  if (!(key in obj)) return false;
210
221
  const guard = schema[key];
211
222
  if (!guard(obj[key])) return false;
212
223
  }
213
- if (options?.exact) {
214
- const allowed = new Set(Object.keys(schema));
224
+ if (allowed) {
215
225
  for (const key of Object.keys(obj)) {
216
226
  if (!allowed.has(key)) return false;
217
227
  }
@@ -271,6 +281,7 @@ export {
271
281
  isNumberPrimitive,
272
282
  isObject,
273
283
  isPlainObject,
284
+ isPrimitive,
274
285
  isPromiseLike,
275
286
  isRegExp,
276
287
  isSet,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "is-kit",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
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",