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 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), // string <= 3 | undefined
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
- for (const key of schemaKeys) {
328
- if (!(key in obj)) return false;
329
- const guard = schema[key];
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
- function oneOfValues(...args) {
344
- const isSingleArrayArg = define((value) => {
345
- if (!Array.isArray(value)) return false;
346
- return value.length === 1 && Array.isArray(value[0]);
347
- });
348
- const items = isSingleArrayArg(args) ? args[0] : args;
349
- if (items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX) {
350
- return function(input) {
351
- return items.some((value) => Object.is(value, input));
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 (typeof literalValue === "number" && (Object.is(literalValue, 0) || Object.is(literalValue, -0))) {
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 (typeof input === "number" && (Object.is(input, 0) || Object.is(input, -0))) {
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
- for (const key of schemaKeys) {
242
- if (!(key in obj)) return false;
243
- const guard = schema[key];
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
- function oneOfValues(...args) {
258
- const isSingleArrayArg = define((value) => {
259
- if (!Array.isArray(value)) return false;
260
- return value.length === 1 && Array.isArray(value[0]);
261
- });
262
- const items = isSingleArrayArg(args) ? args[0] : args;
263
- if (items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX) {
264
- return function(input) {
265
- return items.some((value) => Object.is(value, input));
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 (typeof literalValue === "number" && (Object.is(literalValue, 0) || Object.is(literalValue, -0))) {
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 (typeof input === "number" && (Object.is(input, 0) || Object.is(input, -0))) {
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "is-kit",
3
- "version": "1.1.9",
3
+ "version": "1.1.11",
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",