is-kit 1.1.9 → 1.1.10

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/dist/index.js CHANGED
@@ -318,44 +318,41 @@ function recordOf(keyFunction, valueFunction) {
318
318
  }
319
319
 
320
320
  // src/core/combinators/struct.ts
321
+ var hasRequiredKeys = (obj, schema, keys) => keys.every((key) => key in obj && schema[key](obj[key]));
322
+ var hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
321
323
  function struct(schema, options) {
322
324
  const schemaKeys = Object.keys(schema);
323
325
  const allowed = options?.exact ? new Set(schemaKeys) : null;
324
326
  return (input) => {
325
327
  if (!isPlainObject(input)) return false;
326
328
  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;
329
+ if (!hasRequiredKeys(obj, schema, schemaKeys)) return false;
330
+ if (!allowed) return true;
331
+ return hasOnlyAllowedKeys(obj, allowed);
338
332
  };
339
333
  }
340
334
 
341
335
  // src/core/combinators/one-of-values.ts
342
336
  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
- }
337
+ var isSingleArrayArg = define(
338
+ (value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
339
+ );
340
+ var isZeroNumber = and(
341
+ isNumber,
342
+ predicateToRefine((value) => value === 0)
343
+ );
344
+ var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
345
+ var createLinearPredicate = (items) => {
346
+ return function(input) {
347
+ return items.some((value) => Object.is(value, input));
348
+ };
349
+ };
350
+ var createSetPredicate = (items) => {
354
351
  let hasPositiveZero = false;
355
352
  let hasNegativeZero = false;
356
353
  const valueSet = /* @__PURE__ */ new Set();
357
354
  for (const literalValue of items) {
358
- if (typeof literalValue === "number" && (Object.is(literalValue, 0) || Object.is(literalValue, -0))) {
355
+ if (isZeroNumber(literalValue)) {
359
356
  if (Object.is(literalValue, -0)) hasNegativeZero = true;
360
357
  else hasPositiveZero = true;
361
358
  } else {
@@ -363,11 +360,15 @@ function oneOfValues(...args) {
363
360
  }
364
361
  }
365
362
  return function(input) {
366
- if (typeof input === "number" && (Object.is(input, 0) || Object.is(input, -0))) {
363
+ if (isZeroNumber(input)) {
367
364
  return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
368
365
  }
369
366
  return valueSet.has(input);
370
367
  };
368
+ };
369
+ function oneOfValues(...args) {
370
+ const items = normalizeValues(args);
371
+ return items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX ? createLinearPredicate(items) : createSetPredicate(items);
371
372
  }
372
373
 
373
374
  // src/core/key.ts
package/dist/index.mjs CHANGED
@@ -232,44 +232,41 @@ function recordOf(keyFunction, valueFunction) {
232
232
  }
233
233
 
234
234
  // src/core/combinators/struct.ts
235
+ var hasRequiredKeys = (obj, schema, keys) => keys.every((key) => key in obj && schema[key](obj[key]));
236
+ var hasOnlyAllowedKeys = (obj, allowed) => Object.keys(obj).every((key) => allowed.has(key));
235
237
  function struct(schema, options) {
236
238
  const schemaKeys = Object.keys(schema);
237
239
  const allowed = options?.exact ? new Set(schemaKeys) : null;
238
240
  return (input) => {
239
241
  if (!isPlainObject(input)) return false;
240
242
  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;
243
+ if (!hasRequiredKeys(obj, schema, schemaKeys)) return false;
244
+ if (!allowed) return true;
245
+ return hasOnlyAllowedKeys(obj, allowed);
252
246
  };
253
247
  }
254
248
 
255
249
  // src/core/combinators/one-of-values.ts
256
250
  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
- }
251
+ var isSingleArrayArg = define(
252
+ (value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
253
+ );
254
+ var isZeroNumber = and(
255
+ isNumber,
256
+ predicateToRefine((value) => value === 0)
257
+ );
258
+ var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
259
+ var createLinearPredicate = (items) => {
260
+ return function(input) {
261
+ return items.some((value) => Object.is(value, input));
262
+ };
263
+ };
264
+ var createSetPredicate = (items) => {
268
265
  let hasPositiveZero = false;
269
266
  let hasNegativeZero = false;
270
267
  const valueSet = /* @__PURE__ */ new Set();
271
268
  for (const literalValue of items) {
272
- if (typeof literalValue === "number" && (Object.is(literalValue, 0) || Object.is(literalValue, -0))) {
269
+ if (isZeroNumber(literalValue)) {
273
270
  if (Object.is(literalValue, -0)) hasNegativeZero = true;
274
271
  else hasPositiveZero = true;
275
272
  } else {
@@ -277,11 +274,15 @@ function oneOfValues(...args) {
277
274
  }
278
275
  }
279
276
  return function(input) {
280
- if (typeof input === "number" && (Object.is(input, 0) || Object.is(input, -0))) {
277
+ if (isZeroNumber(input)) {
281
278
  return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
282
279
  }
283
280
  return valueSet.has(input);
284
281
  };
282
+ };
283
+ function oneOfValues(...args) {
284
+ const items = normalizeValues(args);
285
+ return items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX ? createLinearPredicate(items) : createSetPredicate(items);
285
286
  }
286
287
 
287
288
  // 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.10",
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",