is-kit 1.6.4 → 1.7.1
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 +10 -10
- package/dist/index.d.mts +150 -143
- package/dist/index.d.ts +150 -143
- package/dist/index.js +166 -159
- package/dist/index.mjs +165 -159
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
// src/core/define.ts
|
|
2
|
-
function define(fn) {
|
|
3
|
-
return (value) => !!fn(value);
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// src/core/predicate.ts
|
|
7
|
-
var predicateToRefine = (fn) => (value) => fn(value);
|
|
8
|
-
|
|
9
1
|
// src/core/assert.ts
|
|
10
2
|
var DEFAULT_ASSERT_MESSAGE = "Assertion failed";
|
|
11
3
|
function assert(fn, value, message) {
|
|
@@ -13,94 +5,9 @@ function assert(fn, value, message) {
|
|
|
13
5
|
throw new Error(message ?? DEFAULT_ASSERT_MESSAGE);
|
|
14
6
|
}
|
|
15
7
|
|
|
16
|
-
// src/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (!predicate(value)) return false;
|
|
20
|
-
}
|
|
21
|
-
return true;
|
|
22
|
-
};
|
|
23
|
-
var everyTupleValue = (values, predicates) => {
|
|
24
|
-
if (values.length !== predicates.length) return false;
|
|
25
|
-
for (const [index, predicate] of predicates.entries()) {
|
|
26
|
-
if (!predicate(values[index])) return false;
|
|
27
|
-
}
|
|
28
|
-
return true;
|
|
29
|
-
};
|
|
30
|
-
var everySetValue = (values, predicate) => {
|
|
31
|
-
for (const value of values) {
|
|
32
|
-
if (!predicate(value)) return false;
|
|
33
|
-
}
|
|
34
|
-
return true;
|
|
35
|
-
};
|
|
36
|
-
var everyMapEntry = (values, keyPredicate, valuePredicate) => {
|
|
37
|
-
for (const [key, value] of values) {
|
|
38
|
-
if (!keyPredicate(key) || !valuePredicate(value)) return false;
|
|
39
|
-
}
|
|
40
|
-
return true;
|
|
41
|
-
};
|
|
42
|
-
var everyOwnEnumerableEntry = (values, keyPredicate, valuePredicate) => {
|
|
43
|
-
for (const key of Object.keys(values)) {
|
|
44
|
-
if (!keyPredicate(key) || !valuePredicate(values[key])) return false;
|
|
45
|
-
}
|
|
46
|
-
return true;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
// src/utils/to-boolean-predicates.ts
|
|
50
|
-
var toBooleanPredicates = (predicates) => predicates;
|
|
51
|
-
|
|
52
|
-
// src/core/logic.ts
|
|
53
|
-
function and(precondition, condition) {
|
|
54
|
-
return define((input) => {
|
|
55
|
-
if (precondition(input)) {
|
|
56
|
-
return condition(input);
|
|
57
|
-
}
|
|
58
|
-
return false;
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
function andAll(precondition, ...steps) {
|
|
62
|
-
return define((input) => {
|
|
63
|
-
if (!precondition(input)) return false;
|
|
64
|
-
return applyRefinements(input, steps);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
function or(...guards) {
|
|
68
|
-
const predicates = toBooleanPredicates(guards);
|
|
69
|
-
return define(
|
|
70
|
-
(input) => predicates.some((guard) => guard(input))
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
function guardIn() {
|
|
74
|
-
return (guard) => (input) => guard(input);
|
|
75
|
-
}
|
|
76
|
-
function not(fn) {
|
|
77
|
-
return (input) => !fn(input);
|
|
78
|
-
}
|
|
79
|
-
function applyRefinements(value, steps) {
|
|
80
|
-
if (!steps || steps.length === 0) return true;
|
|
81
|
-
for (const step of steps) {
|
|
82
|
-
if (!step(value)) return false;
|
|
83
|
-
}
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// src/core/nullish.ts
|
|
88
|
-
var allowWhen = (acceptedValue, predicate) => (value) => acceptedValue(value) || predicate(value);
|
|
89
|
-
var requireWhen = (rejectedValue, predicate) => (value) => !rejectedValue(value) && predicate(value);
|
|
90
|
-
function nullable(fn) {
|
|
91
|
-
return allowWhen((value) => value === null, fn);
|
|
92
|
-
}
|
|
93
|
-
function nonNull(fn) {
|
|
94
|
-
return requireWhen((value) => value === null, fn);
|
|
95
|
-
}
|
|
96
|
-
function nullish(fn) {
|
|
97
|
-
return allowWhen((value) => value == null, fn);
|
|
98
|
-
}
|
|
99
|
-
function optional(fn) {
|
|
100
|
-
return allowWhen((value) => value === void 0, fn);
|
|
101
|
-
}
|
|
102
|
-
function required(fn) {
|
|
103
|
-
return requireWhen((value) => value === void 0, fn);
|
|
8
|
+
// src/core/define.ts
|
|
9
|
+
function define(fn) {
|
|
10
|
+
return (value) => !!fn(value);
|
|
104
11
|
}
|
|
105
12
|
|
|
106
13
|
// src/utils/object-tags.ts
|
|
@@ -198,6 +105,117 @@ function equalsKey(key, target) {
|
|
|
198
105
|
return (input) => hasMatchingKey(input);
|
|
199
106
|
}
|
|
200
107
|
|
|
108
|
+
// src/core/key.ts
|
|
109
|
+
var hasKey = (key) => define(
|
|
110
|
+
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
111
|
+
);
|
|
112
|
+
var hasKeys = (...keys) => {
|
|
113
|
+
if (keys.length === 0) {
|
|
114
|
+
return define(() => false);
|
|
115
|
+
}
|
|
116
|
+
return define(
|
|
117
|
+
(input) => isObject(input) && keys.every((key) => Object.hasOwn(input, key))
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
function narrowKeyTo(guard, key) {
|
|
121
|
+
return (target) => {
|
|
122
|
+
const keyEquals = equalsKey(key, target);
|
|
123
|
+
return define(
|
|
124
|
+
(input) => guard(input) && keyEquals(input)
|
|
125
|
+
);
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/utils/guard-collections.ts
|
|
130
|
+
var everyArrayValue = (values, predicate) => {
|
|
131
|
+
for (const value of values) {
|
|
132
|
+
if (!predicate(value)) return false;
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
};
|
|
136
|
+
var everyTupleValue = (values, predicates) => {
|
|
137
|
+
if (values.length !== predicates.length) return false;
|
|
138
|
+
for (const [index, predicate] of predicates.entries()) {
|
|
139
|
+
if (!predicate(values[index])) return false;
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
};
|
|
143
|
+
var everySetValue = (values, predicate) => {
|
|
144
|
+
for (const value of values) {
|
|
145
|
+
if (!predicate(value)) return false;
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
};
|
|
149
|
+
var everyMapEntry = (values, keyPredicate, valuePredicate) => {
|
|
150
|
+
for (const [key, value] of values) {
|
|
151
|
+
if (!keyPredicate(key) || !valuePredicate(value)) return false;
|
|
152
|
+
}
|
|
153
|
+
return true;
|
|
154
|
+
};
|
|
155
|
+
var everyOwnEnumerableEntry = (values, keyPredicate, valuePredicate) => {
|
|
156
|
+
for (const key of Object.keys(values)) {
|
|
157
|
+
if (!keyPredicate(key) || !valuePredicate(values[key])) return false;
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// src/utils/to-boolean-predicates.ts
|
|
163
|
+
var toBooleanPredicates = (predicates) => predicates;
|
|
164
|
+
|
|
165
|
+
// src/core/logic.ts
|
|
166
|
+
function and(precondition, condition) {
|
|
167
|
+
return define((input) => {
|
|
168
|
+
if (precondition(input)) {
|
|
169
|
+
return condition(input);
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function andAll(precondition, ...steps) {
|
|
175
|
+
return define((input) => {
|
|
176
|
+
if (!precondition(input)) return false;
|
|
177
|
+
return applyRefinements(input, steps);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
function or(...guards) {
|
|
181
|
+
const predicates = toBooleanPredicates(guards);
|
|
182
|
+
return define(
|
|
183
|
+
(input) => predicates.some((guard) => guard(input))
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
function guardIn() {
|
|
187
|
+
return (guard) => (input) => guard(input);
|
|
188
|
+
}
|
|
189
|
+
function not(fn) {
|
|
190
|
+
return (input) => !fn(input);
|
|
191
|
+
}
|
|
192
|
+
function applyRefinements(value, steps) {
|
|
193
|
+
if (!steps || steps.length === 0) return true;
|
|
194
|
+
for (const step of steps) {
|
|
195
|
+
if (!step(value)) return false;
|
|
196
|
+
}
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/core/nullish.ts
|
|
201
|
+
var allowWhen = (acceptedValue, predicate) => (value) => acceptedValue(value) || predicate(value);
|
|
202
|
+
var requireWhen = (rejectedValue, predicate) => (value) => !rejectedValue(value) && predicate(value);
|
|
203
|
+
function nullable(fn) {
|
|
204
|
+
return allowWhen((value) => value === null, fn);
|
|
205
|
+
}
|
|
206
|
+
function nonNull(fn) {
|
|
207
|
+
return requireWhen((value) => value === null, fn);
|
|
208
|
+
}
|
|
209
|
+
function nullish(fn) {
|
|
210
|
+
return allowWhen((value) => value == null, fn);
|
|
211
|
+
}
|
|
212
|
+
function optional(fn) {
|
|
213
|
+
return allowWhen((value) => value === void 0, fn);
|
|
214
|
+
}
|
|
215
|
+
function required(fn) {
|
|
216
|
+
return requireWhen((value) => value === void 0, fn);
|
|
217
|
+
}
|
|
218
|
+
|
|
201
219
|
// src/core/parse.ts
|
|
202
220
|
function safeParse(fn, value) {
|
|
203
221
|
const valid = fn(value);
|
|
@@ -207,6 +225,9 @@ function safeParseWith(fn) {
|
|
|
207
225
|
return (value) => safeParse(fn, value);
|
|
208
226
|
}
|
|
209
227
|
|
|
228
|
+
// src/core/predicate.ts
|
|
229
|
+
var predicateToRefine = (fn) => (value) => fn(value);
|
|
230
|
+
|
|
210
231
|
// src/core/primitive.ts
|
|
211
232
|
var isString = define((value) => typeof value === "string");
|
|
212
233
|
var isNumberPrimitive = define(
|
|
@@ -254,11 +275,9 @@ function arrayOf(elementGuard) {
|
|
|
254
275
|
(input) => Array.isArray(input) && everyArrayValue(input, elementGuard)
|
|
255
276
|
);
|
|
256
277
|
}
|
|
257
|
-
|
|
258
|
-
// src/core/combinators/set.ts
|
|
259
|
-
function setOf(valueGuard) {
|
|
278
|
+
function nonEmptyArrayOf(elementGuard) {
|
|
260
279
|
return define(
|
|
261
|
-
(input) =>
|
|
280
|
+
(input) => Array.isArray(input) && input.length > 0 && everyArrayValue(input, elementGuard)
|
|
262
281
|
);
|
|
263
282
|
}
|
|
264
283
|
|
|
@@ -269,18 +288,46 @@ function mapOf(keyGuard, valueGuard) {
|
|
|
269
288
|
);
|
|
270
289
|
}
|
|
271
290
|
|
|
272
|
-
// src/core/combinators/tuple.ts
|
|
273
|
-
function tupleOf(...guards) {
|
|
274
|
-
return define(
|
|
275
|
-
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
276
|
-
);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
291
|
// src/core/combinators/one-of.ts
|
|
280
292
|
function oneOf(...guards) {
|
|
281
293
|
return or(...guards);
|
|
282
294
|
}
|
|
283
295
|
|
|
296
|
+
// src/core/combinators/one-of-values.ts
|
|
297
|
+
var ONE_OF_VALUES_LINEAR_SCAN_MAX = 8;
|
|
298
|
+
var isSingleArrayArg = define(
|
|
299
|
+
(value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
|
|
300
|
+
);
|
|
301
|
+
var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
|
|
302
|
+
var createLinearPredicate = (items) => {
|
|
303
|
+
return define((input) => {
|
|
304
|
+
return items.some((value) => Object.is(value, input));
|
|
305
|
+
});
|
|
306
|
+
};
|
|
307
|
+
var createSetPredicate = (items) => {
|
|
308
|
+
let hasPositiveZero = false;
|
|
309
|
+
let hasNegativeZero = false;
|
|
310
|
+
const valueSet = /* @__PURE__ */ new Set();
|
|
311
|
+
for (const literalValue of items) {
|
|
312
|
+
if (isZero(literalValue)) {
|
|
313
|
+
if (Object.is(literalValue, -0)) hasNegativeZero = true;
|
|
314
|
+
else hasPositiveZero = true;
|
|
315
|
+
} else {
|
|
316
|
+
valueSet.add(literalValue);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return define((input) => {
|
|
320
|
+
if (isZero(input)) {
|
|
321
|
+
return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
|
|
322
|
+
}
|
|
323
|
+
return valueSet.has(input);
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
function oneOfValues(...args) {
|
|
327
|
+
const items = normalizeValues(args);
|
|
328
|
+
return items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX ? createLinearPredicate(items) : createSetPredicate(items);
|
|
329
|
+
}
|
|
330
|
+
|
|
284
331
|
// src/core/combinators/record.ts
|
|
285
332
|
function recordOf(keyFunction, valueFunction) {
|
|
286
333
|
return define(
|
|
@@ -288,6 +335,13 @@ function recordOf(keyFunction, valueFunction) {
|
|
|
288
335
|
);
|
|
289
336
|
}
|
|
290
337
|
|
|
338
|
+
// src/core/combinators/set.ts
|
|
339
|
+
function setOf(valueGuard) {
|
|
340
|
+
return define(
|
|
341
|
+
(input) => isSet(input) && everySetValue(input, valueGuard)
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
291
345
|
// src/core/combinators/struct.ts
|
|
292
346
|
var isOptionalSchemaField = define(
|
|
293
347
|
// WHY: Optional fields are represented as a small tagged object so `struct`
|
|
@@ -331,60 +385,11 @@ function struct(schema, options) {
|
|
|
331
385
|
});
|
|
332
386
|
}
|
|
333
387
|
|
|
334
|
-
// src/core/combinators/
|
|
335
|
-
|
|
336
|
-
var isSingleArrayArg = define(
|
|
337
|
-
(value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
|
|
338
|
-
);
|
|
339
|
-
var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
|
|
340
|
-
var createLinearPredicate = (items) => {
|
|
341
|
-
return define((input) => {
|
|
342
|
-
return items.some((value) => Object.is(value, input));
|
|
343
|
-
});
|
|
344
|
-
};
|
|
345
|
-
var createSetPredicate = (items) => {
|
|
346
|
-
let hasPositiveZero = false;
|
|
347
|
-
let hasNegativeZero = false;
|
|
348
|
-
const valueSet = /* @__PURE__ */ new Set();
|
|
349
|
-
for (const literalValue of items) {
|
|
350
|
-
if (isZero(literalValue)) {
|
|
351
|
-
if (Object.is(literalValue, -0)) hasNegativeZero = true;
|
|
352
|
-
else hasPositiveZero = true;
|
|
353
|
-
} else {
|
|
354
|
-
valueSet.add(literalValue);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
return define((input) => {
|
|
358
|
-
if (isZero(input)) {
|
|
359
|
-
return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
|
|
360
|
-
}
|
|
361
|
-
return valueSet.has(input);
|
|
362
|
-
});
|
|
363
|
-
};
|
|
364
|
-
function oneOfValues(...args) {
|
|
365
|
-
const items = normalizeValues(args);
|
|
366
|
-
return items.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX ? createLinearPredicate(items) : createSetPredicate(items);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// src/core/key.ts
|
|
370
|
-
var hasKey = (key) => define(
|
|
371
|
-
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
372
|
-
);
|
|
373
|
-
var hasKeys = (...keys) => {
|
|
374
|
-
if (keys.length === 0) {
|
|
375
|
-
return define(() => false);
|
|
376
|
-
}
|
|
388
|
+
// src/core/combinators/tuple.ts
|
|
389
|
+
function tupleOf(...guards) {
|
|
377
390
|
return define(
|
|
378
|
-
(input) =>
|
|
391
|
+
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
379
392
|
);
|
|
380
|
-
};
|
|
381
|
-
function narrowKeyTo(guard, key) {
|
|
382
|
-
return (target) => {
|
|
383
|
-
const keyEquals = equalsKey(key, target);
|
|
384
|
-
return define(
|
|
385
|
-
(input) => guard(input) && keyEquals(input)
|
|
386
|
-
);
|
|
387
|
-
};
|
|
388
393
|
}
|
|
389
394
|
export {
|
|
390
395
|
and,
|
|
@@ -442,6 +447,7 @@ export {
|
|
|
442
447
|
isZero,
|
|
443
448
|
mapOf,
|
|
444
449
|
narrowKeyTo,
|
|
450
|
+
nonEmptyArrayOf,
|
|
445
451
|
nonNull,
|
|
446
452
|
not,
|
|
447
453
|
nullable,
|