is-kit 1.0.0
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/LICENSE +21 -0
- package/README.md +112 -0
- package/dist/index.d.mts +390 -0
- package/dist/index.d.ts +390 -0
- package/dist/index.js +355 -0
- package/dist/index.mjs +279 -0
- package/package.json +66 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
and: () => and,
|
|
24
|
+
andAll: () => andAll,
|
|
25
|
+
arrayOf: () => arrayOf,
|
|
26
|
+
define: () => define,
|
|
27
|
+
equals: () => equals,
|
|
28
|
+
equalsBy: () => equalsBy,
|
|
29
|
+
equalsKey: () => equalsKey,
|
|
30
|
+
guardIn: () => guardIn,
|
|
31
|
+
isArray: () => isArray,
|
|
32
|
+
isArrayBuffer: () => isArrayBuffer,
|
|
33
|
+
isAsyncIterable: () => isAsyncIterable,
|
|
34
|
+
isBigInt: () => isBigInt,
|
|
35
|
+
isBlob: () => isBlob,
|
|
36
|
+
isBoolean: () => isBoolean,
|
|
37
|
+
isDataView: () => isDataView,
|
|
38
|
+
isDate: () => isDate,
|
|
39
|
+
isError: () => isError,
|
|
40
|
+
isFiniteNumber: () => isFiniteNumber,
|
|
41
|
+
isFunction: () => isFunction,
|
|
42
|
+
isIterable: () => isIterable,
|
|
43
|
+
isMap: () => isMap,
|
|
44
|
+
isNull: () => isNull,
|
|
45
|
+
isNumber: () => isNumber,
|
|
46
|
+
isNumberPrimitive: () => isNumberPrimitive,
|
|
47
|
+
isObject: () => isObject,
|
|
48
|
+
isPlainObject: () => isPlainObject,
|
|
49
|
+
isPromiseLike: () => isPromiseLike,
|
|
50
|
+
isRegExp: () => isRegExp,
|
|
51
|
+
isSet: () => isSet,
|
|
52
|
+
isString: () => isString,
|
|
53
|
+
isSymbol: () => isSymbol,
|
|
54
|
+
isTypedArray: () => isTypedArray,
|
|
55
|
+
isURL: () => isURL,
|
|
56
|
+
isUndefined: () => isUndefined,
|
|
57
|
+
nonNull: () => nonNull,
|
|
58
|
+
not: () => not,
|
|
59
|
+
nullable: () => nullable,
|
|
60
|
+
nullish: () => nullish,
|
|
61
|
+
oneOf: () => oneOf,
|
|
62
|
+
oneOfValues: () => oneOfValues,
|
|
63
|
+
optional: () => optional,
|
|
64
|
+
or: () => or,
|
|
65
|
+
predicateToRefine: () => predicateToRefine,
|
|
66
|
+
recordOf: () => recordOf,
|
|
67
|
+
required: () => required,
|
|
68
|
+
safeParse: () => safeParse,
|
|
69
|
+
safeParseWith: () => safeParseWith,
|
|
70
|
+
struct: () => struct,
|
|
71
|
+
toBooleanPredicates: () => toBooleanPredicates,
|
|
72
|
+
tupleOf: () => tupleOf
|
|
73
|
+
});
|
|
74
|
+
module.exports = __toCommonJS(index_exports);
|
|
75
|
+
|
|
76
|
+
// src/core/define.ts
|
|
77
|
+
function define(fn) {
|
|
78
|
+
return (value) => !!fn(value);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/core/predicate.ts
|
|
82
|
+
var predicateToRefine = (fn) => (value) => fn(value);
|
|
83
|
+
|
|
84
|
+
// src/utils/util.ts
|
|
85
|
+
var toBooleanPredicates = (guards) => guards;
|
|
86
|
+
|
|
87
|
+
// src/core/logic.ts
|
|
88
|
+
function and(precondition, condition) {
|
|
89
|
+
return function(input) {
|
|
90
|
+
if (precondition(input)) {
|
|
91
|
+
return condition(input);
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function andAll(precondition, ...steps) {
|
|
97
|
+
const boolSteps = steps;
|
|
98
|
+
return function(input) {
|
|
99
|
+
return precondition(input) && boolSteps.every((step) => step(input));
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function or(...guards) {
|
|
103
|
+
const predicates = toBooleanPredicates(guards);
|
|
104
|
+
return function(input) {
|
|
105
|
+
return predicates.some((guard) => guard(input));
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function guardIn() {
|
|
109
|
+
return function(guard) {
|
|
110
|
+
return function(input) {
|
|
111
|
+
return guard(input);
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function not(fn) {
|
|
116
|
+
return (input) => !fn(input);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/core/nullish.ts
|
|
120
|
+
function nullable(fn) {
|
|
121
|
+
return (value) => value === null || fn(value);
|
|
122
|
+
}
|
|
123
|
+
function nonNull(fn) {
|
|
124
|
+
return (value) => value !== null && fn(value);
|
|
125
|
+
}
|
|
126
|
+
function nullish(fn) {
|
|
127
|
+
return (value) => value == null || fn(value);
|
|
128
|
+
}
|
|
129
|
+
function optional(fn) {
|
|
130
|
+
return (value) => value === void 0 || fn(value);
|
|
131
|
+
}
|
|
132
|
+
function required(fn) {
|
|
133
|
+
return (value) => value !== void 0 && fn(value);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/core/object.ts
|
|
137
|
+
var getTag = (value) => Object.prototype.toString.call(value);
|
|
138
|
+
var isFunction = define(
|
|
139
|
+
(value) => typeof value === "function"
|
|
140
|
+
);
|
|
141
|
+
var isObject = define(
|
|
142
|
+
(value) => typeof value === "object" && value !== null
|
|
143
|
+
);
|
|
144
|
+
var isPlainObject = define((value) => {
|
|
145
|
+
if (!isObject(value)) return false;
|
|
146
|
+
const proto = Object.getPrototypeOf(value);
|
|
147
|
+
return proto === Object.prototype || proto === null;
|
|
148
|
+
});
|
|
149
|
+
var isArray = define(Array.isArray);
|
|
150
|
+
var isDate = define(
|
|
151
|
+
(value) => getTag(value) === "[object Date]" && !Number.isNaN(value.getTime())
|
|
152
|
+
);
|
|
153
|
+
var isRegExp = define(
|
|
154
|
+
(value) => getTag(value) === "[object RegExp]"
|
|
155
|
+
);
|
|
156
|
+
var isMap = define(
|
|
157
|
+
(value) => getTag(value) === "[object Map]"
|
|
158
|
+
);
|
|
159
|
+
var isSet = define(
|
|
160
|
+
(value) => getTag(value) === "[object Set]"
|
|
161
|
+
);
|
|
162
|
+
var isPromiseLike = define(
|
|
163
|
+
(value) => !!value && typeof value.then === "function"
|
|
164
|
+
);
|
|
165
|
+
var isIterable = define(
|
|
166
|
+
(value) => !!value && typeof value === "object" && typeof value[Symbol.iterator] === "function"
|
|
167
|
+
);
|
|
168
|
+
var isAsyncIterable = define(
|
|
169
|
+
(value) => !!value && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function"
|
|
170
|
+
);
|
|
171
|
+
var isArrayBuffer = define(
|
|
172
|
+
(value) => getTag(value) === "[object ArrayBuffer]"
|
|
173
|
+
);
|
|
174
|
+
var isDataView = define(
|
|
175
|
+
(value) => getTag(value) === "[object DataView]"
|
|
176
|
+
);
|
|
177
|
+
var isTypedArray = define(
|
|
178
|
+
(value) => ArrayBuffer.isView(value) && getTag(value) !== "[object DataView]"
|
|
179
|
+
);
|
|
180
|
+
var isError = define(
|
|
181
|
+
(value) => value instanceof Error || getTag(value) === "[object Error]"
|
|
182
|
+
);
|
|
183
|
+
var isURL = typeof URL !== "undefined" ? define((value) => value instanceof URL) : define((_value) => false);
|
|
184
|
+
var isBlob = typeof Blob !== "undefined" ? define((value) => value instanceof Blob) : define((_value) => false);
|
|
185
|
+
|
|
186
|
+
// src/core/equals.ts
|
|
187
|
+
function equals(target) {
|
|
188
|
+
return function(input) {
|
|
189
|
+
return Object.is(input, target);
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function equalsBy(guard, selector) {
|
|
193
|
+
const createComparator = (selectorFn) => (target) => (input) => {
|
|
194
|
+
if (!guard(input)) return false;
|
|
195
|
+
return Object.is(selectorFn(input), target);
|
|
196
|
+
};
|
|
197
|
+
if (selector) {
|
|
198
|
+
return createComparator(selector);
|
|
199
|
+
}
|
|
200
|
+
return (selector2) => createComparator(selector2);
|
|
201
|
+
}
|
|
202
|
+
function equalsKey(key, target) {
|
|
203
|
+
return function(input) {
|
|
204
|
+
return isObject(input) && Object.prototype.hasOwnProperty.call(input, key) && Object.is(input[key], target);
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/core/parse.ts
|
|
209
|
+
function safeParse(fn, value) {
|
|
210
|
+
const valid = fn(value);
|
|
211
|
+
return valid ? { valid: true, value } : { valid: false };
|
|
212
|
+
}
|
|
213
|
+
function safeParseWith(fn) {
|
|
214
|
+
return (value) => safeParse(fn, value);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/core/primitive.ts
|
|
218
|
+
var isString = define((value) => typeof value === "string");
|
|
219
|
+
var isNumberPrimitive = define(
|
|
220
|
+
(value) => typeof value === "number"
|
|
221
|
+
);
|
|
222
|
+
var isFiniteNumber = predicateToRefine(Number.isFinite);
|
|
223
|
+
var isNumber = and(isNumberPrimitive, isFiniteNumber);
|
|
224
|
+
var isBoolean = define((value) => typeof value === "boolean");
|
|
225
|
+
var isBigInt = define((value) => typeof value === "bigint");
|
|
226
|
+
var isSymbol = define((value) => typeof value === "symbol");
|
|
227
|
+
var isUndefined = define((value) => value === void 0);
|
|
228
|
+
var isNull = define((value) => value === null);
|
|
229
|
+
|
|
230
|
+
// src/core/combinators/array.ts
|
|
231
|
+
function arrayOf(elementGuard) {
|
|
232
|
+
return function(input) {
|
|
233
|
+
if (!Array.isArray(input)) return false;
|
|
234
|
+
for (const element of input) {
|
|
235
|
+
if (!elementGuard(element)) return false;
|
|
236
|
+
}
|
|
237
|
+
return true;
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// src/core/combinators/tuple.ts
|
|
242
|
+
function tupleOf(...guards) {
|
|
243
|
+
return function(input) {
|
|
244
|
+
return Array.isArray(input) && input.length === guards.length && guards.every(
|
|
245
|
+
(guard, index) => guard(input[index])
|
|
246
|
+
);
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// src/core/combinators/one.ts
|
|
251
|
+
function oneOf(...guards) {
|
|
252
|
+
return (input) => guards.some((guard) => guard(input));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/core/combinators/record.ts
|
|
256
|
+
function recordOf(keyFunction, valueFunction) {
|
|
257
|
+
return (input) => {
|
|
258
|
+
if (!isPlainObject(input)) return false;
|
|
259
|
+
const obj = input;
|
|
260
|
+
for (const key of Object.keys(obj)) {
|
|
261
|
+
if (!keyFunction(key)) return false;
|
|
262
|
+
const value = obj[key];
|
|
263
|
+
if (!valueFunction(value)) return false;
|
|
264
|
+
}
|
|
265
|
+
return true;
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// src/core/combinators/struct.ts
|
|
270
|
+
function struct(schema, options) {
|
|
271
|
+
return (input) => {
|
|
272
|
+
if (!isPlainObject(input)) return false;
|
|
273
|
+
const obj = input;
|
|
274
|
+
for (const key of Object.keys(schema)) {
|
|
275
|
+
const guard = schema[key];
|
|
276
|
+
if (!(key in obj)) return false;
|
|
277
|
+
if (!guard(obj[key])) return false;
|
|
278
|
+
}
|
|
279
|
+
if (options?.exact) {
|
|
280
|
+
const allowed = new Set(Object.keys(schema));
|
|
281
|
+
for (const key of Object.keys(obj)) {
|
|
282
|
+
if (!allowed.has(key)) return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return true;
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/core/combinators/one-of-values.ts
|
|
290
|
+
var ONE_OF_VALUES_LINEAR_SCAN_MAX = 8;
|
|
291
|
+
function oneOfValues(...values) {
|
|
292
|
+
if (values.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX) {
|
|
293
|
+
const items = values;
|
|
294
|
+
return function(input) {
|
|
295
|
+
return items.some((value) => Object.is(value, input));
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
const valueSet = new Set(values);
|
|
299
|
+
return function(input) {
|
|
300
|
+
return valueSet.has(input);
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
304
|
+
0 && (module.exports = {
|
|
305
|
+
and,
|
|
306
|
+
andAll,
|
|
307
|
+
arrayOf,
|
|
308
|
+
define,
|
|
309
|
+
equals,
|
|
310
|
+
equalsBy,
|
|
311
|
+
equalsKey,
|
|
312
|
+
guardIn,
|
|
313
|
+
isArray,
|
|
314
|
+
isArrayBuffer,
|
|
315
|
+
isAsyncIterable,
|
|
316
|
+
isBigInt,
|
|
317
|
+
isBlob,
|
|
318
|
+
isBoolean,
|
|
319
|
+
isDataView,
|
|
320
|
+
isDate,
|
|
321
|
+
isError,
|
|
322
|
+
isFiniteNumber,
|
|
323
|
+
isFunction,
|
|
324
|
+
isIterable,
|
|
325
|
+
isMap,
|
|
326
|
+
isNull,
|
|
327
|
+
isNumber,
|
|
328
|
+
isNumberPrimitive,
|
|
329
|
+
isObject,
|
|
330
|
+
isPlainObject,
|
|
331
|
+
isPromiseLike,
|
|
332
|
+
isRegExp,
|
|
333
|
+
isSet,
|
|
334
|
+
isString,
|
|
335
|
+
isSymbol,
|
|
336
|
+
isTypedArray,
|
|
337
|
+
isURL,
|
|
338
|
+
isUndefined,
|
|
339
|
+
nonNull,
|
|
340
|
+
not,
|
|
341
|
+
nullable,
|
|
342
|
+
nullish,
|
|
343
|
+
oneOf,
|
|
344
|
+
oneOfValues,
|
|
345
|
+
optional,
|
|
346
|
+
or,
|
|
347
|
+
predicateToRefine,
|
|
348
|
+
recordOf,
|
|
349
|
+
required,
|
|
350
|
+
safeParse,
|
|
351
|
+
safeParseWith,
|
|
352
|
+
struct,
|
|
353
|
+
toBooleanPredicates,
|
|
354
|
+
tupleOf
|
|
355
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
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
|
+
// src/utils/util.ts
|
|
10
|
+
var toBooleanPredicates = (guards) => guards;
|
|
11
|
+
|
|
12
|
+
// src/core/logic.ts
|
|
13
|
+
function and(precondition, condition) {
|
|
14
|
+
return function(input) {
|
|
15
|
+
if (precondition(input)) {
|
|
16
|
+
return condition(input);
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function andAll(precondition, ...steps) {
|
|
22
|
+
const boolSteps = steps;
|
|
23
|
+
return function(input) {
|
|
24
|
+
return precondition(input) && boolSteps.every((step) => step(input));
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function or(...guards) {
|
|
28
|
+
const predicates = toBooleanPredicates(guards);
|
|
29
|
+
return function(input) {
|
|
30
|
+
return predicates.some((guard) => guard(input));
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function guardIn() {
|
|
34
|
+
return function(guard) {
|
|
35
|
+
return function(input) {
|
|
36
|
+
return guard(input);
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function not(fn) {
|
|
41
|
+
return (input) => !fn(input);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/core/nullish.ts
|
|
45
|
+
function nullable(fn) {
|
|
46
|
+
return (value) => value === null || fn(value);
|
|
47
|
+
}
|
|
48
|
+
function nonNull(fn) {
|
|
49
|
+
return (value) => value !== null && fn(value);
|
|
50
|
+
}
|
|
51
|
+
function nullish(fn) {
|
|
52
|
+
return (value) => value == null || fn(value);
|
|
53
|
+
}
|
|
54
|
+
function optional(fn) {
|
|
55
|
+
return (value) => value === void 0 || fn(value);
|
|
56
|
+
}
|
|
57
|
+
function required(fn) {
|
|
58
|
+
return (value) => value !== void 0 && fn(value);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/core/object.ts
|
|
62
|
+
var getTag = (value) => Object.prototype.toString.call(value);
|
|
63
|
+
var isFunction = define(
|
|
64
|
+
(value) => typeof value === "function"
|
|
65
|
+
);
|
|
66
|
+
var isObject = define(
|
|
67
|
+
(value) => typeof value === "object" && value !== null
|
|
68
|
+
);
|
|
69
|
+
var isPlainObject = define((value) => {
|
|
70
|
+
if (!isObject(value)) return false;
|
|
71
|
+
const proto = Object.getPrototypeOf(value);
|
|
72
|
+
return proto === Object.prototype || proto === null;
|
|
73
|
+
});
|
|
74
|
+
var isArray = define(Array.isArray);
|
|
75
|
+
var isDate = define(
|
|
76
|
+
(value) => getTag(value) === "[object Date]" && !Number.isNaN(value.getTime())
|
|
77
|
+
);
|
|
78
|
+
var isRegExp = define(
|
|
79
|
+
(value) => getTag(value) === "[object RegExp]"
|
|
80
|
+
);
|
|
81
|
+
var isMap = define(
|
|
82
|
+
(value) => getTag(value) === "[object Map]"
|
|
83
|
+
);
|
|
84
|
+
var isSet = define(
|
|
85
|
+
(value) => getTag(value) === "[object Set]"
|
|
86
|
+
);
|
|
87
|
+
var isPromiseLike = define(
|
|
88
|
+
(value) => !!value && typeof value.then === "function"
|
|
89
|
+
);
|
|
90
|
+
var isIterable = define(
|
|
91
|
+
(value) => !!value && typeof value === "object" && typeof value[Symbol.iterator] === "function"
|
|
92
|
+
);
|
|
93
|
+
var isAsyncIterable = define(
|
|
94
|
+
(value) => !!value && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function"
|
|
95
|
+
);
|
|
96
|
+
var isArrayBuffer = define(
|
|
97
|
+
(value) => getTag(value) === "[object ArrayBuffer]"
|
|
98
|
+
);
|
|
99
|
+
var isDataView = define(
|
|
100
|
+
(value) => getTag(value) === "[object DataView]"
|
|
101
|
+
);
|
|
102
|
+
var isTypedArray = define(
|
|
103
|
+
(value) => ArrayBuffer.isView(value) && getTag(value) !== "[object DataView]"
|
|
104
|
+
);
|
|
105
|
+
var isError = define(
|
|
106
|
+
(value) => value instanceof Error || getTag(value) === "[object Error]"
|
|
107
|
+
);
|
|
108
|
+
var isURL = typeof URL !== "undefined" ? define((value) => value instanceof URL) : define((_value) => false);
|
|
109
|
+
var isBlob = typeof Blob !== "undefined" ? define((value) => value instanceof Blob) : define((_value) => false);
|
|
110
|
+
|
|
111
|
+
// src/core/equals.ts
|
|
112
|
+
function equals(target) {
|
|
113
|
+
return function(input) {
|
|
114
|
+
return Object.is(input, target);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function equalsBy(guard, selector) {
|
|
118
|
+
const createComparator = (selectorFn) => (target) => (input) => {
|
|
119
|
+
if (!guard(input)) return false;
|
|
120
|
+
return Object.is(selectorFn(input), target);
|
|
121
|
+
};
|
|
122
|
+
if (selector) {
|
|
123
|
+
return createComparator(selector);
|
|
124
|
+
}
|
|
125
|
+
return (selector2) => createComparator(selector2);
|
|
126
|
+
}
|
|
127
|
+
function equalsKey(key, target) {
|
|
128
|
+
return function(input) {
|
|
129
|
+
return isObject(input) && Object.prototype.hasOwnProperty.call(input, key) && Object.is(input[key], target);
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/core/parse.ts
|
|
134
|
+
function safeParse(fn, value) {
|
|
135
|
+
const valid = fn(value);
|
|
136
|
+
return valid ? { valid: true, value } : { valid: false };
|
|
137
|
+
}
|
|
138
|
+
function safeParseWith(fn) {
|
|
139
|
+
return (value) => safeParse(fn, value);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/core/primitive.ts
|
|
143
|
+
var isString = define((value) => typeof value === "string");
|
|
144
|
+
var isNumberPrimitive = define(
|
|
145
|
+
(value) => typeof value === "number"
|
|
146
|
+
);
|
|
147
|
+
var isFiniteNumber = predicateToRefine(Number.isFinite);
|
|
148
|
+
var isNumber = and(isNumberPrimitive, isFiniteNumber);
|
|
149
|
+
var isBoolean = define((value) => typeof value === "boolean");
|
|
150
|
+
var isBigInt = define((value) => typeof value === "bigint");
|
|
151
|
+
var isSymbol = define((value) => typeof value === "symbol");
|
|
152
|
+
var isUndefined = define((value) => value === void 0);
|
|
153
|
+
var isNull = define((value) => value === null);
|
|
154
|
+
|
|
155
|
+
// src/core/combinators/array.ts
|
|
156
|
+
function arrayOf(elementGuard) {
|
|
157
|
+
return function(input) {
|
|
158
|
+
if (!Array.isArray(input)) return false;
|
|
159
|
+
for (const element of input) {
|
|
160
|
+
if (!elementGuard(element)) return false;
|
|
161
|
+
}
|
|
162
|
+
return true;
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/core/combinators/tuple.ts
|
|
167
|
+
function tupleOf(...guards) {
|
|
168
|
+
return function(input) {
|
|
169
|
+
return Array.isArray(input) && input.length === guards.length && guards.every(
|
|
170
|
+
(guard, index) => guard(input[index])
|
|
171
|
+
);
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/core/combinators/one.ts
|
|
176
|
+
function oneOf(...guards) {
|
|
177
|
+
return (input) => guards.some((guard) => guard(input));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/core/combinators/record.ts
|
|
181
|
+
function recordOf(keyFunction, valueFunction) {
|
|
182
|
+
return (input) => {
|
|
183
|
+
if (!isPlainObject(input)) return false;
|
|
184
|
+
const obj = input;
|
|
185
|
+
for (const key of Object.keys(obj)) {
|
|
186
|
+
if (!keyFunction(key)) return false;
|
|
187
|
+
const value = obj[key];
|
|
188
|
+
if (!valueFunction(value)) return false;
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// src/core/combinators/struct.ts
|
|
195
|
+
function struct(schema, options) {
|
|
196
|
+
return (input) => {
|
|
197
|
+
if (!isPlainObject(input)) return false;
|
|
198
|
+
const obj = input;
|
|
199
|
+
for (const key of Object.keys(schema)) {
|
|
200
|
+
const guard = schema[key];
|
|
201
|
+
if (!(key in obj)) return false;
|
|
202
|
+
if (!guard(obj[key])) return false;
|
|
203
|
+
}
|
|
204
|
+
if (options?.exact) {
|
|
205
|
+
const allowed = new Set(Object.keys(schema));
|
|
206
|
+
for (const key of Object.keys(obj)) {
|
|
207
|
+
if (!allowed.has(key)) return false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// src/core/combinators/one-of-values.ts
|
|
215
|
+
var ONE_OF_VALUES_LINEAR_SCAN_MAX = 8;
|
|
216
|
+
function oneOfValues(...values) {
|
|
217
|
+
if (values.length <= ONE_OF_VALUES_LINEAR_SCAN_MAX) {
|
|
218
|
+
const items = values;
|
|
219
|
+
return function(input) {
|
|
220
|
+
return items.some((value) => Object.is(value, input));
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
const valueSet = new Set(values);
|
|
224
|
+
return function(input) {
|
|
225
|
+
return valueSet.has(input);
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
export {
|
|
229
|
+
and,
|
|
230
|
+
andAll,
|
|
231
|
+
arrayOf,
|
|
232
|
+
define,
|
|
233
|
+
equals,
|
|
234
|
+
equalsBy,
|
|
235
|
+
equalsKey,
|
|
236
|
+
guardIn,
|
|
237
|
+
isArray,
|
|
238
|
+
isArrayBuffer,
|
|
239
|
+
isAsyncIterable,
|
|
240
|
+
isBigInt,
|
|
241
|
+
isBlob,
|
|
242
|
+
isBoolean,
|
|
243
|
+
isDataView,
|
|
244
|
+
isDate,
|
|
245
|
+
isError,
|
|
246
|
+
isFiniteNumber,
|
|
247
|
+
isFunction,
|
|
248
|
+
isIterable,
|
|
249
|
+
isMap,
|
|
250
|
+
isNull,
|
|
251
|
+
isNumber,
|
|
252
|
+
isNumberPrimitive,
|
|
253
|
+
isObject,
|
|
254
|
+
isPlainObject,
|
|
255
|
+
isPromiseLike,
|
|
256
|
+
isRegExp,
|
|
257
|
+
isSet,
|
|
258
|
+
isString,
|
|
259
|
+
isSymbol,
|
|
260
|
+
isTypedArray,
|
|
261
|
+
isURL,
|
|
262
|
+
isUndefined,
|
|
263
|
+
nonNull,
|
|
264
|
+
not,
|
|
265
|
+
nullable,
|
|
266
|
+
nullish,
|
|
267
|
+
oneOf,
|
|
268
|
+
oneOfValues,
|
|
269
|
+
optional,
|
|
270
|
+
or,
|
|
271
|
+
predicateToRefine,
|
|
272
|
+
recordOf,
|
|
273
|
+
required,
|
|
274
|
+
safeParse,
|
|
275
|
+
safeParseWith,
|
|
276
|
+
struct,
|
|
277
|
+
toBooleanPredicates,
|
|
278
|
+
tupleOf
|
|
279
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "is-kit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Make 'isXXX' easier. Let's make your code type safe and more readable!",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"is",
|
|
17
|
+
"isXXX",
|
|
18
|
+
"isKit",
|
|
19
|
+
"type guards",
|
|
20
|
+
"type safe",
|
|
21
|
+
"type checking"
|
|
22
|
+
],
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/nyaomaru/is-kit.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/nyaomaru/is-kit/issues"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/nyaomaru/is-kit#readme",
|
|
37
|
+
"author": "nyaomaru",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@eslint/js": "^9.33.0",
|
|
41
|
+
"@types/jest": "^30.0.0",
|
|
42
|
+
"eslint": "^9.33.0",
|
|
43
|
+
"eslint-config-prettier": "^10.1.8",
|
|
44
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
45
|
+
"jest": "^30.0.5",
|
|
46
|
+
"prettier": "^3.6.2",
|
|
47
|
+
"ts-jest": "^29.4.1",
|
|
48
|
+
"tsd": "^0.33.0",
|
|
49
|
+
"tsup": "^8.5.0",
|
|
50
|
+
"typedoc": "^0.28.12",
|
|
51
|
+
"typescript": "^5.9.2"
|
|
52
|
+
},
|
|
53
|
+
"tsd": {
|
|
54
|
+
"directory": "tests-d",
|
|
55
|
+
"compilerOptions": {
|
|
56
|
+
"baseUrl": "."
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"test": "jest",
|
|
61
|
+
"test:types": "tsd",
|
|
62
|
+
"lint": "eslint .",
|
|
63
|
+
"build": "tsup",
|
|
64
|
+
"typedoc": "typedoc --out docs/public/api"
|
|
65
|
+
}
|
|
66
|
+
}
|