is-kit 1.7.0 → 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/dist/index.d.mts +149 -149
- package/dist/index.d.ts +149 -149
- package/dist/index.js +162 -162
- package/dist/index.mjs +162 -162
- 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(
|
|
@@ -260,13 +281,6 @@ function nonEmptyArrayOf(elementGuard) {
|
|
|
260
281
|
);
|
|
261
282
|
}
|
|
262
283
|
|
|
263
|
-
// src/core/combinators/set.ts
|
|
264
|
-
function setOf(valueGuard) {
|
|
265
|
-
return define(
|
|
266
|
-
(input) => isSet(input) && everySetValue(input, valueGuard)
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
284
|
// src/core/combinators/map.ts
|
|
271
285
|
function mapOf(keyGuard, valueGuard) {
|
|
272
286
|
return define(
|
|
@@ -274,18 +288,46 @@ function mapOf(keyGuard, valueGuard) {
|
|
|
274
288
|
);
|
|
275
289
|
}
|
|
276
290
|
|
|
277
|
-
// src/core/combinators/tuple.ts
|
|
278
|
-
function tupleOf(...guards) {
|
|
279
|
-
return define(
|
|
280
|
-
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
281
|
-
);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
291
|
// src/core/combinators/one-of.ts
|
|
285
292
|
function oneOf(...guards) {
|
|
286
293
|
return or(...guards);
|
|
287
294
|
}
|
|
288
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
|
+
|
|
289
331
|
// src/core/combinators/record.ts
|
|
290
332
|
function recordOf(keyFunction, valueFunction) {
|
|
291
333
|
return define(
|
|
@@ -293,6 +335,13 @@ function recordOf(keyFunction, valueFunction) {
|
|
|
293
335
|
);
|
|
294
336
|
}
|
|
295
337
|
|
|
338
|
+
// src/core/combinators/set.ts
|
|
339
|
+
function setOf(valueGuard) {
|
|
340
|
+
return define(
|
|
341
|
+
(input) => isSet(input) && everySetValue(input, valueGuard)
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
296
345
|
// src/core/combinators/struct.ts
|
|
297
346
|
var isOptionalSchemaField = define(
|
|
298
347
|
// WHY: Optional fields are represented as a small tagged object so `struct`
|
|
@@ -336,60 +385,11 @@ function struct(schema, options) {
|
|
|
336
385
|
});
|
|
337
386
|
}
|
|
338
387
|
|
|
339
|
-
// src/core/combinators/
|
|
340
|
-
|
|
341
|
-
var isSingleArrayArg = define(
|
|
342
|
-
(value) => Array.isArray(value) && value.length === 1 && Array.isArray(value[0])
|
|
343
|
-
);
|
|
344
|
-
var normalizeValues = (args) => isSingleArrayArg(args) ? args[0] : args;
|
|
345
|
-
var createLinearPredicate = (items) => {
|
|
346
|
-
return define((input) => {
|
|
347
|
-
return items.some((value) => Object.is(value, input));
|
|
348
|
-
});
|
|
349
|
-
};
|
|
350
|
-
var createSetPredicate = (items) => {
|
|
351
|
-
let hasPositiveZero = false;
|
|
352
|
-
let hasNegativeZero = false;
|
|
353
|
-
const valueSet = /* @__PURE__ */ new Set();
|
|
354
|
-
for (const literalValue of items) {
|
|
355
|
-
if (isZero(literalValue)) {
|
|
356
|
-
if (Object.is(literalValue, -0)) hasNegativeZero = true;
|
|
357
|
-
else hasPositiveZero = true;
|
|
358
|
-
} else {
|
|
359
|
-
valueSet.add(literalValue);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return define((input) => {
|
|
363
|
-
if (isZero(input)) {
|
|
364
|
-
return Object.is(input, -0) ? hasNegativeZero : hasPositiveZero;
|
|
365
|
-
}
|
|
366
|
-
return valueSet.has(input);
|
|
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);
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
// src/core/key.ts
|
|
375
|
-
var hasKey = (key) => define(
|
|
376
|
-
(input) => isObject(input) && Object.hasOwn(input, key)
|
|
377
|
-
);
|
|
378
|
-
var hasKeys = (...keys) => {
|
|
379
|
-
if (keys.length === 0) {
|
|
380
|
-
return define(() => false);
|
|
381
|
-
}
|
|
388
|
+
// src/core/combinators/tuple.ts
|
|
389
|
+
function tupleOf(...guards) {
|
|
382
390
|
return define(
|
|
383
|
-
(input) =>
|
|
391
|
+
(input) => Array.isArray(input) && everyTupleValue(input, guards)
|
|
384
392
|
);
|
|
385
|
-
};
|
|
386
|
-
function narrowKeyTo(guard, key) {
|
|
387
|
-
return (target) => {
|
|
388
|
-
const keyEquals = equalsKey(key, target);
|
|
389
|
-
return define(
|
|
390
|
-
(input) => guard(input) && keyEquals(input)
|
|
391
|
-
);
|
|
392
|
-
};
|
|
393
393
|
}
|
|
394
394
|
export {
|
|
395
395
|
and,
|