is-kit 1.1.9 → 1.1.11
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 +4 -4
- package/dist/index.js +26 -27
- package/dist/index.mjs +26 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -74,7 +74,7 @@ import {
|
|
|
74
74
|
optional,
|
|
75
75
|
isNumber,
|
|
76
76
|
isString,
|
|
77
|
-
predicateToRefine
|
|
77
|
+
predicateToRefine
|
|
78
78
|
} from 'is-kit';
|
|
79
79
|
|
|
80
80
|
// Define small guards
|
|
@@ -94,7 +94,7 @@ const isUser = struct({
|
|
|
94
94
|
id: isPositive, // number > 0
|
|
95
95
|
name: isString, // string
|
|
96
96
|
role: isRole, // 'admin' | 'member'
|
|
97
|
-
nickname: optional(isShortString)
|
|
97
|
+
nickname: optional(isShortString) // string <= 3 | undefined
|
|
98
98
|
});
|
|
99
99
|
|
|
100
100
|
// Use them
|
|
@@ -132,7 +132,7 @@ import {
|
|
|
132
132
|
isNegative,
|
|
133
133
|
isZero,
|
|
134
134
|
isNaN,
|
|
135
|
-
isInfiniteNumber
|
|
135
|
+
isInfiniteNumber
|
|
136
136
|
} from 'is-kit';
|
|
137
137
|
|
|
138
138
|
// Any primitive
|
|
@@ -177,7 +177,7 @@ type User = { id: string; age: number; role: 'admin' | 'guest' | 'trial' };
|
|
|
177
177
|
const isUser = struct({
|
|
178
178
|
id: isString,
|
|
179
179
|
age: isNumber,
|
|
180
|
-
role: oneOfValues('admin', 'guest', 'trial')
|
|
180
|
+
role: oneOfValues('admin', 'guest', 'trial')
|
|
181
181
|
});
|
|
182
182
|
|
|
183
183
|
const byRole = narrowKeyTo(isUser, 'role');
|
package/dist/index.js
CHANGED
|
@@ -297,9 +297,7 @@ function tupleOf(...guards) {
|
|
|
297
297
|
|
|
298
298
|
// src/core/combinators/one-of.ts
|
|
299
299
|
function oneOf(...guards) {
|
|
300
|
-
const predicates = toBooleanPredicates(
|
|
301
|
-
guards
|
|
302
|
-
);
|
|
300
|
+
const predicates = toBooleanPredicates(guards);
|
|
303
301
|
return (input) => predicates.some((guard) => guard(input));
|
|
304
302
|
}
|
|
305
303
|
|
|
@@ -318,44 +316,41 @@ function recordOf(keyFunction, valueFunction) {
|
|
|
318
316
|
}
|
|
319
317
|
|
|
320
318
|
// src/core/combinators/struct.ts
|
|
319
|
+
var hasRequiredKeys = (obj, schema, keys) => keys.every((key) => key in obj && schema[key](obj[key]));
|
|
320
|
+
var hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
|
|
321
321
|
function struct(schema, options) {
|
|
322
322
|
const schemaKeys = Object.keys(schema);
|
|
323
323
|
const allowed = options?.exact ? new Set(schemaKeys) : null;
|
|
324
324
|
return (input) => {
|
|
325
325
|
if (!isPlainObject(input)) return false;
|
|
326
326
|
const obj = input;
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
if (!guard(obj[key])) return false;
|
|
331
|
-
}
|
|
332
|
-
if (allowed) {
|
|
333
|
-
for (const key of Object.keys(obj)) {
|
|
334
|
-
if (!allowed.has(key)) return false;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
return true;
|
|
327
|
+
if (!hasRequiredKeys(obj, schema, schemaKeys)) return false;
|
|
328
|
+
if (!allowed) return true;
|
|
329
|
+
return hasOnlyAllowedKeys(obj, allowed);
|
|
338
330
|
};
|
|
339
331
|
}
|
|
340
332
|
|
|
341
333
|
// src/core/combinators/one-of-values.ts
|
|
342
334
|
var ONE_OF_VALUES_LINEAR_SCAN_MAX = 8;
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
335
|
+
var isSingleArrayArg = define(
|
|
336
|
+
(value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
|
|
337
|
+
);
|
|
338
|
+
var isZeroNumber = and(
|
|
339
|
+
isNumber,
|
|
340
|
+
predicateToRefine((value) => value === 0)
|
|
341
|
+
);
|
|
342
|
+
var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
|
|
343
|
+
var createLinearPredicate = (items) => {
|
|
344
|
+
return function(input) {
|
|
345
|
+
return items.some((value) => Object.is(value, input));
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
var createSetPredicate = (items) => {
|
|
354
349
|
let hasPositiveZero = false;
|
|
355
350
|
let hasNegativeZero = false;
|
|
356
351
|
const valueSet = /* @__PURE__ */ new Set();
|
|
357
352
|
for (const literalValue of items) {
|
|
358
|
-
if (
|
|
353
|
+
if (isZeroNumber(literalValue)) {
|
|
359
354
|
if (Object.is(literalValue, -0)) hasNegativeZero = true;
|
|
360
355
|
else hasPositiveZero = true;
|
|
361
356
|
} else {
|
|
@@ -363,11 +358,15 @@ function oneOfValues(...args) {
|
|
|
363
358
|
}
|
|
364
359
|
}
|
|
365
360
|
return function(input) {
|
|
366
|
-
if (
|
|
361
|
+
if (isZeroNumber(input)) {
|
|
367
362
|
return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
|
|
368
363
|
}
|
|
369
364
|
return valueSet.has(input);
|
|
370
365
|
};
|
|
366
|
+
};
|
|
367
|
+
function oneOfValues(...args) {
|
|
368
|
+
const items = normalizeValues(args);
|
|
369
|
+
return items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX ? createLinearPredicate(items) : createSetPredicate(items);
|
|
371
370
|
}
|
|
372
371
|
|
|
373
372
|
// src/core/key.ts
|
package/dist/index.mjs
CHANGED
|
@@ -211,9 +211,7 @@ function tupleOf(...guards) {
|
|
|
211
211
|
|
|
212
212
|
// src/core/combinators/one-of.ts
|
|
213
213
|
function oneOf(...guards) {
|
|
214
|
-
const predicates = toBooleanPredicates(
|
|
215
|
-
guards
|
|
216
|
-
);
|
|
214
|
+
const predicates = toBooleanPredicates(guards);
|
|
217
215
|
return (input) => predicates.some((guard) => guard(input));
|
|
218
216
|
}
|
|
219
217
|
|
|
@@ -232,44 +230,41 @@ function recordOf(keyFunction, valueFunction) {
|
|
|
232
230
|
}
|
|
233
231
|
|
|
234
232
|
// src/core/combinators/struct.ts
|
|
233
|
+
var hasRequiredKeys = (obj, schema, keys) => keys.every((key) => key in obj && schema[key](obj[key]));
|
|
234
|
+
var hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
|
|
235
235
|
function struct(schema, options) {
|
|
236
236
|
const schemaKeys = Object.keys(schema);
|
|
237
237
|
const allowed = options?.exact ? new Set(schemaKeys) : null;
|
|
238
238
|
return (input) => {
|
|
239
239
|
if (!isPlainObject(input)) return false;
|
|
240
240
|
const obj = input;
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (!guard(obj[key])) return false;
|
|
245
|
-
}
|
|
246
|
-
if (allowed) {
|
|
247
|
-
for (const key of Object.keys(obj)) {
|
|
248
|
-
if (!allowed.has(key)) return false;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
return true;
|
|
241
|
+
if (!hasRequiredKeys(obj, schema, schemaKeys)) return false;
|
|
242
|
+
if (!allowed) return true;
|
|
243
|
+
return hasOnlyAllowedKeys(obj, allowed);
|
|
252
244
|
};
|
|
253
245
|
}
|
|
254
246
|
|
|
255
247
|
// src/core/combinators/one-of-values.ts
|
|
256
248
|
var ONE_OF_VALUES_LINEAR_SCAN_MAX = 8;
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
249
|
+
var isSingleArrayArg = define(
|
|
250
|
+
(value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
|
|
251
|
+
);
|
|
252
|
+
var isZeroNumber = and(
|
|
253
|
+
isNumber,
|
|
254
|
+
predicateToRefine((value) => value === 0)
|
|
255
|
+
);
|
|
256
|
+
var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
|
|
257
|
+
var createLinearPredicate = (items) => {
|
|
258
|
+
return function(input) {
|
|
259
|
+
return items.some((value) => Object.is(value, input));
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
var createSetPredicate = (items) => {
|
|
268
263
|
let hasPositiveZero = false;
|
|
269
264
|
let hasNegativeZero = false;
|
|
270
265
|
const valueSet = /* @__PURE__ */ new Set();
|
|
271
266
|
for (const literalValue of items) {
|
|
272
|
-
if (
|
|
267
|
+
if (isZeroNumber(literalValue)) {
|
|
273
268
|
if (Object.is(literalValue, -0)) hasNegativeZero = true;
|
|
274
269
|
else hasPositiveZero = true;
|
|
275
270
|
} else {
|
|
@@ -277,11 +272,15 @@ function oneOfValues(...args) {
|
|
|
277
272
|
}
|
|
278
273
|
}
|
|
279
274
|
return function(input) {
|
|
280
|
-
if (
|
|
275
|
+
if (isZeroNumber(input)) {
|
|
281
276
|
return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
|
|
282
277
|
}
|
|
283
278
|
return valueSet.has(input);
|
|
284
279
|
};
|
|
280
|
+
};
|
|
281
|
+
function oneOfValues(...args) {
|
|
282
|
+
const items = normalizeValues(args);
|
|
283
|
+
return items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX ? createLinearPredicate(items) : createSetPredicate(items);
|
|
285
284
|
}
|
|
286
285
|
|
|
287
286
|
// src/core/key.ts
|