is-kit 1.1.2 → 1.1.4
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 +13 -0
- package/dist/index.d.mts +12 -3
- package/dist/index.d.ts +12 -3
- package/dist/index.js +17 -5
- package/dist/index.mjs +16 -5
- package/package.json +1 -1
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
|
@@ -136,8 +136,8 @@ declare function equals<const T>(target: T): Predicate<T>;
|
|
|
136
136
|
* @param selector Optional selector to project the comparable key; if omitted, returns a function to supply it later.
|
|
137
137
|
* @returns Function that accepts a target and returns a predicate over the original value.
|
|
138
138
|
*/
|
|
139
|
-
declare function equalsBy<F extends
|
|
140
|
-
declare function equalsBy<F extends
|
|
139
|
+
declare function equalsBy<F extends Predicate<unknown>>(guard: F): <K>(selector: (value: GuardedOf<F>) => K) => <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
|
|
140
|
+
declare function equalsBy<F extends Predicate<unknown>, K>(guard: F, selector: (value: GuardedOf<F>) => K): <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
|
|
141
141
|
/**
|
|
142
142
|
* Creates a guard that matches objects having a key equal to the target value.
|
|
143
143
|
*
|
|
@@ -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
|
@@ -136,8 +136,8 @@ declare function equals<const T>(target: T): Predicate<T>;
|
|
|
136
136
|
* @param selector Optional selector to project the comparable key; if omitted, returns a function to supply it later.
|
|
137
137
|
* @returns Function that accepts a target and returns a predicate over the original value.
|
|
138
138
|
*/
|
|
139
|
-
declare function equalsBy<F extends
|
|
140
|
-
declare function equalsBy<F extends
|
|
139
|
+
declare function equalsBy<F extends Predicate<unknown>>(guard: F): <K>(selector: (value: GuardedOf<F>) => K) => <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
|
|
140
|
+
declare function equalsBy<F extends Predicate<unknown>, K>(guard: F, selector: (value: GuardedOf<F>) => K): <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
|
|
141
141
|
/**
|
|
142
142
|
* Creates a guard that matches objects having a key equal to the target value.
|
|
143
143
|
*
|
|
@@ -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,
|
|
@@ -84,7 +85,7 @@ function define(fn) {
|
|
|
84
85
|
// src/core/predicate.ts
|
|
85
86
|
var predicateToRefine = (fn) => (value) => fn(value);
|
|
86
87
|
|
|
87
|
-
// src/utils/
|
|
88
|
+
// src/utils/to-boolean-predicates.ts
|
|
88
89
|
var toBooleanPredicates = (guards) => guards;
|
|
89
90
|
|
|
90
91
|
// src/core/logic.ts
|
|
@@ -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) {
|
|
@@ -259,7 +269,7 @@ function tupleOf(...guards) {
|
|
|
259
269
|
};
|
|
260
270
|
}
|
|
261
271
|
|
|
262
|
-
// src/core/combinators/one.ts
|
|
272
|
+
// src/core/combinators/one-of.ts
|
|
263
273
|
function oneOf(...guards) {
|
|
264
274
|
return (input) => guards.some((guard) => guard(input));
|
|
265
275
|
}
|
|
@@ -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
|
|
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 (
|
|
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
|
@@ -6,7 +6,7 @@ function define(fn) {
|
|
|
6
6
|
// src/core/predicate.ts
|
|
7
7
|
var predicateToRefine = (fn) => (value) => fn(value);
|
|
8
8
|
|
|
9
|
-
// src/utils/
|
|
9
|
+
// src/utils/to-boolean-predicates.ts
|
|
10
10
|
var toBooleanPredicates = (guards) => guards;
|
|
11
11
|
|
|
12
12
|
// src/core/logic.ts
|
|
@@ -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) {
|
|
@@ -181,7 +190,7 @@ function tupleOf(...guards) {
|
|
|
181
190
|
};
|
|
182
191
|
}
|
|
183
192
|
|
|
184
|
-
// src/core/combinators/one.ts
|
|
193
|
+
// src/core/combinators/one-of.ts
|
|
185
194
|
function oneOf(...guards) {
|
|
186
195
|
return (input) => guards.some((guard) => guard(input));
|
|
187
196
|
}
|
|
@@ -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
|
|
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 (
|
|
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,
|