lang-json 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +0 -0
- package/babel.config.js +8 -0
- package/coverage/clover.xml +6 -0
- package/coverage/coverage-final.json +1 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +101 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/dist/esm/src/index.d.ts +18 -0
- package/dist/esm/src/index.js +369 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/modules/is-this/index.d.ts +136 -0
- package/dist/esm/src/modules/is-this/index.js +484 -0
- package/dist/esm/src/modules/is-this/index.js.map +1 -0
- package/dist/esm/tests/helpers.test.d.ts +1 -0
- package/dist/esm/tests/helpers.test.js +284 -0
- package/dist/esm/tests/helpers.test.js.map +1 -0
- package/dist/esm/tests/index.test.d.ts +1 -0
- package/dist/esm/tests/index.test.js +537 -0
- package/dist/esm/tests/index.test.js.map +1 -0
- package/dist/esm/tests/readme.test.d.ts +1 -0
- package/dist/esm/tests/readme.test.js +73 -0
- package/dist/esm/tests/readme.test.js.map +1 -0
- package/jest.config.ts +212 -0
- package/package.json +40 -12
- package/src/index.ts +404 -295
- package/src/modules/is-this/index.ts +682 -0
- package/tests/helpers.test.ts +331 -0
- package/tests/index.test.ts +681 -0
- package/tests/readme.test.ts +78 -0
- package/tsconfig.json +15 -16
- package/dist/esm/dump.js +0 -2
- package/dist/esm/dump.js.map +0 -1
- package/dist/esm/example.d.ts +0 -13
- package/dist/esm/example.js +0 -93
- package/dist/esm/example.js.map +0 -1
- package/dist/esm/index.d.ts +0 -36
- package/dist/esm/index.js +0 -326
- package/dist/esm/index.js.map +0 -1
- package/src/example.ts +0 -116
- /package/{dist/esm/dump.d.ts → coverage/lcov.info} +0 -0
@@ -0,0 +1,682 @@
|
|
1
|
+
class IsThis {
|
2
|
+
// ------------------ Primitive Type Checks ------------------
|
3
|
+
|
4
|
+
isString(value: any): value is string {
|
5
|
+
return typeof value === "string";
|
6
|
+
}
|
7
|
+
|
8
|
+
isNumber(value: any): value is number {
|
9
|
+
return typeof value === "number" && !isNaN(value);
|
10
|
+
}
|
11
|
+
|
12
|
+
isBoolean(value: any): value is boolean {
|
13
|
+
return typeof value === "boolean";
|
14
|
+
}
|
15
|
+
|
16
|
+
isNull(value: any): value is null {
|
17
|
+
return value === null;
|
18
|
+
}
|
19
|
+
|
20
|
+
isUndefined(value: any): value is undefined {
|
21
|
+
return typeof value === "undefined";
|
22
|
+
}
|
23
|
+
|
24
|
+
isNullOrUndefined(value: any): boolean {
|
25
|
+
return this.isNull(value) || this.isUndefined(value);
|
26
|
+
}
|
27
|
+
|
28
|
+
isBigInt(value: any): value is bigint {
|
29
|
+
return typeof value === "bigint";
|
30
|
+
}
|
31
|
+
|
32
|
+
isSymbol(value: any): value is symbol {
|
33
|
+
return typeof value === "symbol";
|
34
|
+
}
|
35
|
+
|
36
|
+
// ------------------ Collection Type Checks ------------------
|
37
|
+
|
38
|
+
isArray(value: any): value is any[] {
|
39
|
+
return Array.isArray(value);
|
40
|
+
}
|
41
|
+
|
42
|
+
isObject(value: any): value is object {
|
43
|
+
return value !== null && typeof value === "object" && !this.isArray(value);
|
44
|
+
}
|
45
|
+
|
46
|
+
isArrayOrObject(value: any) {
|
47
|
+
return this.isArray(value) || this.isObject(value);
|
48
|
+
}
|
49
|
+
|
50
|
+
isFunction(value: any): value is Function {
|
51
|
+
return typeof value === "function";
|
52
|
+
}
|
53
|
+
|
54
|
+
isHTMLElement(value: any): boolean {
|
55
|
+
return value instanceof HTMLElement;
|
56
|
+
}
|
57
|
+
|
58
|
+
isMap(value: any): boolean {
|
59
|
+
return value instanceof Map;
|
60
|
+
}
|
61
|
+
|
62
|
+
isSet(value: any): boolean {
|
63
|
+
return value instanceof Set;
|
64
|
+
}
|
65
|
+
|
66
|
+
isWeakMap(value: any): value is WeakMap<any, any> {
|
67
|
+
return value instanceof WeakMap;
|
68
|
+
}
|
69
|
+
|
70
|
+
isWeakSet(value: any): value is WeakSet<any> {
|
71
|
+
return value instanceof WeakSet;
|
72
|
+
}
|
73
|
+
|
74
|
+
// ------------------ Data Structure Checks ------------------
|
75
|
+
|
76
|
+
isPromise(value: any): boolean {
|
77
|
+
return (
|
78
|
+
this.isObject(value) &&
|
79
|
+
typeof (value as { then?: Function }).then === "function"
|
80
|
+
);
|
81
|
+
}
|
82
|
+
|
83
|
+
isArrayBuffer(value: any): value is ArrayBuffer {
|
84
|
+
return value instanceof ArrayBuffer;
|
85
|
+
}
|
86
|
+
|
87
|
+
isTypedArray(value: any): boolean {
|
88
|
+
return (
|
89
|
+
value instanceof Int8Array ||
|
90
|
+
value instanceof Uint8Array ||
|
91
|
+
value instanceof Uint8ClampedArray ||
|
92
|
+
value instanceof Int16Array ||
|
93
|
+
value instanceof Uint16Array ||
|
94
|
+
value instanceof Int32Array ||
|
95
|
+
value instanceof Uint32Array ||
|
96
|
+
value instanceof Float32Array ||
|
97
|
+
value instanceof Float64Array ||
|
98
|
+
value instanceof BigInt64Array ||
|
99
|
+
value instanceof BigUint64Array
|
100
|
+
);
|
101
|
+
}
|
102
|
+
|
103
|
+
isBlob(value: any): value is Blob {
|
104
|
+
return value instanceof Blob;
|
105
|
+
}
|
106
|
+
|
107
|
+
isFile(value: any): value is File {
|
108
|
+
return value instanceof File;
|
109
|
+
}
|
110
|
+
|
111
|
+
isDataView(value: any): value is DataView {
|
112
|
+
return value instanceof DataView;
|
113
|
+
}
|
114
|
+
|
115
|
+
// ------------------ Number Checks ------------------
|
116
|
+
|
117
|
+
isInt(value: any): boolean {
|
118
|
+
return this.isNumber(value) && Number.isInteger(value);
|
119
|
+
}
|
120
|
+
|
121
|
+
isFloat(value: any): boolean {
|
122
|
+
return this.isNumber(value) && !Number.isInteger(value);
|
123
|
+
}
|
124
|
+
|
125
|
+
isFiniteNumber(value: any): boolean {
|
126
|
+
return this.isNumber(value) && Number.isFinite(value);
|
127
|
+
}
|
128
|
+
|
129
|
+
isInfinity(value: any): boolean {
|
130
|
+
return this.isNumber(value) && (value === Infinity || value === -Infinity);
|
131
|
+
}
|
132
|
+
|
133
|
+
isEven(value: any): boolean {
|
134
|
+
return this.isInt(value) && value % 2 === 0;
|
135
|
+
}
|
136
|
+
|
137
|
+
isOdd(value: any): boolean {
|
138
|
+
return this.isInt(value) && value % 2 !== 0;
|
139
|
+
}
|
140
|
+
|
141
|
+
isPositive(value: any): boolean {
|
142
|
+
return this.isNumber(value) && value > 0;
|
143
|
+
}
|
144
|
+
|
145
|
+
isNegative(value: any): boolean {
|
146
|
+
return this.isNumber(value) && value < 0;
|
147
|
+
}
|
148
|
+
|
149
|
+
isPrime(value: any): boolean {
|
150
|
+
if (!this.isInt(value) || value < 2) return false;
|
151
|
+
for (let i = 2; i <= Math.sqrt(value); i++) {
|
152
|
+
if (value % i === 0) return false;
|
153
|
+
}
|
154
|
+
return true;
|
155
|
+
}
|
156
|
+
|
157
|
+
isSafeInteger(value: any): boolean {
|
158
|
+
return this.isInt(value) && Number.isSafeInteger(value);
|
159
|
+
}
|
160
|
+
|
161
|
+
isDivisibleBy(value: any, divisor: number): boolean {
|
162
|
+
return (
|
163
|
+
this.isNumber(value) && this.isNumber(divisor) && value % divisor === 0
|
164
|
+
);
|
165
|
+
}
|
166
|
+
|
167
|
+
isPerfectSquare(value: any): boolean {
|
168
|
+
return this.isNumber(value) && Math.sqrt(value) % 1 === 0;
|
169
|
+
}
|
170
|
+
|
171
|
+
isFibonacci(value: any): boolean {
|
172
|
+
if (!this.isInt(value) || value < 0) return false;
|
173
|
+
const isPerfectSquare = (x: number) => Math.sqrt(x) % 1 === 0;
|
174
|
+
return (
|
175
|
+
isPerfectSquare(5 * value * value + 4) ||
|
176
|
+
isPerfectSquare(5 * value * value - 4)
|
177
|
+
);
|
178
|
+
}
|
179
|
+
|
180
|
+
// ------------------ String Checks ------------------
|
181
|
+
|
182
|
+
isEmptyString(value: any): boolean {
|
183
|
+
return this.isString(value) && value.trim().length === 0;
|
184
|
+
}
|
185
|
+
|
186
|
+
isNumberString(value: any): boolean {
|
187
|
+
return this.isString(value) && !this.isEmptyString(value) && !isNaN(+value);
|
188
|
+
}
|
189
|
+
|
190
|
+
isBooleanString(value: any): boolean {
|
191
|
+
return this.isString(value) && (value === "true" || value === "false");
|
192
|
+
}
|
193
|
+
|
194
|
+
isNullString(value: any): boolean {
|
195
|
+
return this.isString(value) && value === "null";
|
196
|
+
}
|
197
|
+
|
198
|
+
isUndefinedString(value: any): boolean {
|
199
|
+
return this.isString(value) && value === "undefined";
|
200
|
+
}
|
201
|
+
|
202
|
+
isNullOrUndefinedString(value: any): boolean {
|
203
|
+
return this.isString(value) && (value === "null" || value === "undefined");
|
204
|
+
}
|
205
|
+
|
206
|
+
isDateString(value: any): boolean {
|
207
|
+
const date = new Date(value);
|
208
|
+
return !isNaN(date.getTime());
|
209
|
+
}
|
210
|
+
|
211
|
+
isUUID(value: any): boolean {
|
212
|
+
return (
|
213
|
+
this.isString(value) &&
|
214
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(
|
215
|
+
value
|
216
|
+
)
|
217
|
+
);
|
218
|
+
}
|
219
|
+
|
220
|
+
isHexColor(value: any): boolean {
|
221
|
+
return (
|
222
|
+
this.isString(value) && /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(value)
|
223
|
+
);
|
224
|
+
}
|
225
|
+
|
226
|
+
isPhoneNumber(value: any): boolean {
|
227
|
+
return this.isString(value) && /^\+?[1-9]\d{1,14}$/.test(value);
|
228
|
+
}
|
229
|
+
|
230
|
+
isValidEmail(value: any): boolean {
|
231
|
+
return this.isString(value) && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
|
232
|
+
}
|
233
|
+
|
234
|
+
isAlpha(value: any): boolean {
|
235
|
+
return this.isString(value) && /^[A-Za-z]+$/.test(value);
|
236
|
+
}
|
237
|
+
|
238
|
+
isAlphanumeric(value: any): boolean {
|
239
|
+
return this.isString(value) && /^[A-Za-z0-9]+$/.test(value);
|
240
|
+
}
|
241
|
+
|
242
|
+
isLowerCase(value: any): boolean {
|
243
|
+
return this.isString(value) && value === value.toLowerCase();
|
244
|
+
}
|
245
|
+
|
246
|
+
isUpperCase(value: any): boolean {
|
247
|
+
return this.isString(value) && value === value.toUpperCase();
|
248
|
+
}
|
249
|
+
|
250
|
+
isJSONString(value: any): boolean {
|
251
|
+
if (!this.isString(value)) return false;
|
252
|
+
try {
|
253
|
+
JSON.parse(value);
|
254
|
+
return true;
|
255
|
+
} catch {
|
256
|
+
return false;
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
isIPv4(value: any): boolean {
|
261
|
+
return (
|
262
|
+
this.isString(value) &&
|
263
|
+
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
|
264
|
+
value
|
265
|
+
)
|
266
|
+
);
|
267
|
+
}
|
268
|
+
|
269
|
+
isIPv6(value: any): boolean {
|
270
|
+
return (
|
271
|
+
this.isString(value) &&
|
272
|
+
/^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})$/.test(value)
|
273
|
+
);
|
274
|
+
}
|
275
|
+
|
276
|
+
isMACAddress(value: any): boolean {
|
277
|
+
return (
|
278
|
+
this.isString(value) &&
|
279
|
+
/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.test(value)
|
280
|
+
);
|
281
|
+
}
|
282
|
+
|
283
|
+
isBase64(value: any): boolean {
|
284
|
+
return (
|
285
|
+
this.isString(value) &&
|
286
|
+
/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/.test(
|
287
|
+
value
|
288
|
+
)
|
289
|
+
);
|
290
|
+
}
|
291
|
+
|
292
|
+
isCreditCard(value: any): boolean {
|
293
|
+
return (
|
294
|
+
this.isString(value) &&
|
295
|
+
/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9]{2})[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/.test(
|
296
|
+
value
|
297
|
+
)
|
298
|
+
);
|
299
|
+
}
|
300
|
+
|
301
|
+
isPalindrome(value: any): boolean {
|
302
|
+
return this.isString(value) && value === value.split("").reverse().join("");
|
303
|
+
}
|
304
|
+
|
305
|
+
// ------------------ Object Checks ------------------
|
306
|
+
|
307
|
+
isEmptyObject(value: any): boolean {
|
308
|
+
return this.isObject(value) && Object.keys(value).length === 0;
|
309
|
+
}
|
310
|
+
|
311
|
+
isObjectWithKeys(value: any, keys: string[]): boolean {
|
312
|
+
return this.isObject(value) && keys.every((key) => key in value);
|
313
|
+
}
|
314
|
+
|
315
|
+
isPlainObject(value: any): boolean {
|
316
|
+
return this.isObject(value) && value.constructor === Object;
|
317
|
+
}
|
318
|
+
|
319
|
+
isInstanceOf(value: any, classRef: Function): boolean {
|
320
|
+
return value instanceof classRef;
|
321
|
+
}
|
322
|
+
|
323
|
+
hasProperty(value: any, prop: string | symbol): boolean {
|
324
|
+
return value != null && prop in value;
|
325
|
+
}
|
326
|
+
|
327
|
+
hasMethod(value: any, methodName: string): boolean {
|
328
|
+
return (
|
329
|
+
this.hasProperty(value, methodName) && this.isFunction(value[methodName])
|
330
|
+
);
|
331
|
+
}
|
332
|
+
|
333
|
+
hasKeys(value: any, keys: string[]): boolean {
|
334
|
+
return this.isObject(value) && keys.every((key) => key in value);
|
335
|
+
}
|
336
|
+
|
337
|
+
isPrototypeOf(value: any, prototype: any): boolean {
|
338
|
+
return this.isObject(value) && prototype.isPrototypeOf(value);
|
339
|
+
}
|
340
|
+
|
341
|
+
isFrozen(value: any): boolean {
|
342
|
+
return Object.isFrozen(value);
|
343
|
+
}
|
344
|
+
|
345
|
+
isSealed(value: any): boolean {
|
346
|
+
return Object.isSealed(value);
|
347
|
+
}
|
348
|
+
|
349
|
+
isExtensible(value: any): boolean {
|
350
|
+
return Object.isExtensible(value);
|
351
|
+
}
|
352
|
+
|
353
|
+
// ------------------ Array Checks ------------------
|
354
|
+
|
355
|
+
isEmptyArray(value: any): boolean {
|
356
|
+
return this.isArray(value) && value.length === 0;
|
357
|
+
}
|
358
|
+
|
359
|
+
isNonEmptyArray(value: any): boolean {
|
360
|
+
return this.isArray(value) && value.length > 0;
|
361
|
+
}
|
362
|
+
|
363
|
+
isArrayLike(value: any): boolean {
|
364
|
+
return (
|
365
|
+
value != null &&
|
366
|
+
typeof value === "object" &&
|
367
|
+
"length" in value &&
|
368
|
+
Number.isInteger(value.length)
|
369
|
+
);
|
370
|
+
}
|
371
|
+
|
372
|
+
isArrayOfType(value: any, typeCheck: (item: any) => boolean): boolean {
|
373
|
+
return this.isArray(value) && value.every(typeCheck);
|
374
|
+
}
|
375
|
+
|
376
|
+
isArrayOfStrings(value: any): boolean {
|
377
|
+
return this.isArrayOfType(value, this.isString.bind(this));
|
378
|
+
}
|
379
|
+
|
380
|
+
isArrayOfNumbers(value: any): boolean {
|
381
|
+
return this.isArrayOfType(value, this.isNumber.bind(this));
|
382
|
+
}
|
383
|
+
|
384
|
+
isArrayOfBooleans(value: any): boolean {
|
385
|
+
return this.isArrayOfType(value, this.isBoolean.bind(this));
|
386
|
+
}
|
387
|
+
|
388
|
+
isArrayOfObjects(value: any): boolean {
|
389
|
+
return this.isArrayOfType(value, this.isObject.bind(this));
|
390
|
+
}
|
391
|
+
|
392
|
+
isArrayOfPrimitives(value: any): boolean {
|
393
|
+
const isPrimitive = (item: any) => item !== Object(item);
|
394
|
+
return this.isArray(value) && value.every(isPrimitive);
|
395
|
+
}
|
396
|
+
|
397
|
+
isSubset(subset: any[], superset: any[]): boolean {
|
398
|
+
return (
|
399
|
+
this.isArray(subset) && subset.every((value) => superset.includes(value))
|
400
|
+
);
|
401
|
+
}
|
402
|
+
|
403
|
+
isArrayOfUniqueElements(value: any): boolean {
|
404
|
+
return this.isArray(value) && new Set(value).size === value.length;
|
405
|
+
}
|
406
|
+
|
407
|
+
// ------------------ Comparison Checks ------------------
|
408
|
+
|
409
|
+
isDeepEqual(a: any, b: any): boolean {
|
410
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
411
|
+
}
|
412
|
+
|
413
|
+
isShallowEqual(a: any, b: any): boolean {
|
414
|
+
return a === b;
|
415
|
+
}
|
416
|
+
|
417
|
+
isGreaterThan(a: any, b: any): boolean {
|
418
|
+
return this.isNumber(a) && this.isNumber(b) && a > b;
|
419
|
+
}
|
420
|
+
|
421
|
+
isLessThan(a: any, b: any): boolean {
|
422
|
+
return this.isNumber(a) && this.isNumber(b) && a < b;
|
423
|
+
}
|
424
|
+
|
425
|
+
isBetween(value: number, min: number, max: number): boolean {
|
426
|
+
return this.isNumber(value) && value >= min && value <= max;
|
427
|
+
}
|
428
|
+
|
429
|
+
isInstanceOfAll(value: any, constructors: Function[]): boolean {
|
430
|
+
return constructors.every((constructor) => value instanceof constructor);
|
431
|
+
}
|
432
|
+
|
433
|
+
isInstanceOfAny(value: any, constructors: Function[]): boolean {
|
434
|
+
return constructors.some((constructor) => value instanceof constructor);
|
435
|
+
}
|
436
|
+
|
437
|
+
isSimilarObject(obj1: any, obj2: any): boolean {
|
438
|
+
const keys1 = Object.keys(obj1);
|
439
|
+
const keys2 = Object.keys(obj2);
|
440
|
+
if (keys1.length !== keys2.length) return false;
|
441
|
+
return keys1.every((key) => key in obj2 && obj1[key] === obj2[key]);
|
442
|
+
}
|
443
|
+
|
444
|
+
isApproxEqual(
|
445
|
+
value: number,
|
446
|
+
comparison: number,
|
447
|
+
tolerance: number = 0.00001
|
448
|
+
): boolean {
|
449
|
+
return (
|
450
|
+
this.isNumber(value) &&
|
451
|
+
this.isNumber(comparison) &&
|
452
|
+
Math.abs(value - comparison) <= tolerance
|
453
|
+
);
|
454
|
+
}
|
455
|
+
|
456
|
+
isNonStrictEqual(a: any, b: any): boolean {
|
457
|
+
return a == b;
|
458
|
+
}
|
459
|
+
|
460
|
+
// ------------------ Miscellaneous Checks ------------------
|
461
|
+
|
462
|
+
isRegExp(value: any): boolean {
|
463
|
+
return value instanceof RegExp;
|
464
|
+
}
|
465
|
+
|
466
|
+
isURL(value: any): boolean {
|
467
|
+
try {
|
468
|
+
new URL(value);
|
469
|
+
return true;
|
470
|
+
} catch {
|
471
|
+
return false;
|
472
|
+
}
|
473
|
+
}
|
474
|
+
|
475
|
+
isDomain(value: any): boolean {
|
476
|
+
return (
|
477
|
+
this.isString(value) &&
|
478
|
+
/^(?!:\/\/)([a-zA-Z0-9-_]+\.)+[a-zA-Z]{2,6}$/.test(value)
|
479
|
+
);
|
480
|
+
}
|
481
|
+
|
482
|
+
isRelativeURL(value: any): boolean {
|
483
|
+
return (
|
484
|
+
this.isString(value) && !this.isURL(value) && /^[\/.][^:]*$/.test(value)
|
485
|
+
);
|
486
|
+
}
|
487
|
+
|
488
|
+
isAbsoluteURL(value: any): boolean {
|
489
|
+
return this.isURL(value) && /^https?:\/\//.test(value);
|
490
|
+
}
|
491
|
+
|
492
|
+
isSecureURL(value: any): boolean {
|
493
|
+
return this.isAbsoluteURL(value) && value.startsWith("https://");
|
494
|
+
}
|
495
|
+
|
496
|
+
isTruthy(value: any): boolean {
|
497
|
+
return !!value;
|
498
|
+
}
|
499
|
+
|
500
|
+
isFalsy(value: any): boolean {
|
501
|
+
return !value;
|
502
|
+
}
|
503
|
+
|
504
|
+
isInRange(value: any, min: number, max: number): boolean {
|
505
|
+
return this.isNumber(value) && value >= min && value <= max;
|
506
|
+
}
|
507
|
+
|
508
|
+
isFileSizeInRange(value: File, minBytes: number, maxBytes: number): boolean {
|
509
|
+
return (
|
510
|
+
this.isFile(value) && value.size >= minBytes && value.size <= maxBytes
|
511
|
+
);
|
512
|
+
}
|
513
|
+
|
514
|
+
isBrowser(): boolean {
|
515
|
+
return (
|
516
|
+
typeof window !== "undefined" && typeof window.document !== "undefined"
|
517
|
+
);
|
518
|
+
}
|
519
|
+
|
520
|
+
isNode(): boolean {
|
521
|
+
return (
|
522
|
+
typeof process !== "undefined" &&
|
523
|
+
process.versions != null &&
|
524
|
+
process.versions.node != null
|
525
|
+
);
|
526
|
+
}
|
527
|
+
|
528
|
+
isUserDefinedType(value: any, constructor: Function): boolean {
|
529
|
+
return value instanceof constructor;
|
530
|
+
}
|
531
|
+
|
532
|
+
isWeakRef(value: any): boolean {
|
533
|
+
return value instanceof WeakRef;
|
534
|
+
}
|
535
|
+
|
536
|
+
isNativeFunction(value: any): boolean {
|
537
|
+
return this.isFunction(value) && /\[native code\]/.test(value.toString());
|
538
|
+
}
|
539
|
+
|
540
|
+
isMimeType(value: any): boolean {
|
541
|
+
return this.isString(value) && /^[a-z]+\/[a-z0-9\-\+]+$/i.test(value);
|
542
|
+
}
|
543
|
+
|
544
|
+
isMobileDevice(): boolean {
|
545
|
+
return this.isBrowser() && /Mobi|Android/i.test(navigator.userAgent);
|
546
|
+
}
|
547
|
+
|
548
|
+
isDevEnvironment(): boolean {
|
549
|
+
return process.env.NODE_ENV === "development";
|
550
|
+
}
|
551
|
+
|
552
|
+
isProdEnvironment(): boolean {
|
553
|
+
return process.env.NODE_ENV === "production";
|
554
|
+
}
|
555
|
+
|
556
|
+
// ------------------ Iterable and Collection Checks ------------------
|
557
|
+
|
558
|
+
isIterable(value: any): boolean {
|
559
|
+
return value != null && typeof value[Symbol.iterator] === "function";
|
560
|
+
}
|
561
|
+
|
562
|
+
isEmptyIterable(value: any): boolean {
|
563
|
+
return this.isIterable(value) && Array.from(value).length === 0;
|
564
|
+
}
|
565
|
+
|
566
|
+
isNonEmptyIterable(value: any): boolean {
|
567
|
+
return this.isIterable(value) && Array.from(value).length > 0;
|
568
|
+
}
|
569
|
+
|
570
|
+
isNonEmptyMap(value: any): boolean {
|
571
|
+
return this.isMap(value) && value.size > 0;
|
572
|
+
}
|
573
|
+
|
574
|
+
isNonEmptySet(value: any): boolean {
|
575
|
+
return this.isSet(value) && value.size > 0;
|
576
|
+
}
|
577
|
+
|
578
|
+
// ------------------ Function Checks ------------------
|
579
|
+
|
580
|
+
isAsyncFunction(value: any): boolean {
|
581
|
+
return (
|
582
|
+
value instanceof Function && value.constructor.name === "AsyncFunction"
|
583
|
+
);
|
584
|
+
}
|
585
|
+
|
586
|
+
isPromiseLike(value: any): boolean {
|
587
|
+
return (
|
588
|
+
value && typeof value === "object" && typeof value.then === "function"
|
589
|
+
);
|
590
|
+
}
|
591
|
+
|
592
|
+
isFunctionType(value: any, constructor: Function): boolean {
|
593
|
+
return typeof value === "function" && value.constructor === constructor;
|
594
|
+
}
|
595
|
+
|
596
|
+
isGeneratorFunction(value: any): boolean {
|
597
|
+
return (
|
598
|
+
this.isFunction(value) && value.constructor.name === "GeneratorFunction"
|
599
|
+
);
|
600
|
+
}
|
601
|
+
|
602
|
+
isArrowFunction(value: any): boolean {
|
603
|
+
return this.isFunction(value) && !value.hasOwnProperty("prototype");
|
604
|
+
}
|
605
|
+
|
606
|
+
isAsyncGeneratorFunction(value: any): boolean {
|
607
|
+
return (
|
608
|
+
this.isFunction(value) &&
|
609
|
+
value.constructor.name === "AsyncGeneratorFunction"
|
610
|
+
);
|
611
|
+
}
|
612
|
+
|
613
|
+
// ------------------ Date Checks ------------------
|
614
|
+
|
615
|
+
isDate(value: any): value is Date {
|
616
|
+
return value instanceof Date && !isNaN(value.getTime());
|
617
|
+
}
|
618
|
+
|
619
|
+
isPastDate(value: any): boolean {
|
620
|
+
return this.isDate(value) && value.getTime() < Date.now();
|
621
|
+
}
|
622
|
+
|
623
|
+
isFutureDate(value: any): boolean {
|
624
|
+
return this.isDate(value) && value.getTime() > Date.now();
|
625
|
+
}
|
626
|
+
|
627
|
+
isValidDate(value: any): boolean {
|
628
|
+
return !isNaN(new Date(value).getTime());
|
629
|
+
}
|
630
|
+
|
631
|
+
isLeapYear(year: number): boolean {
|
632
|
+
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
633
|
+
}
|
634
|
+
|
635
|
+
isWeekend(value: any): boolean {
|
636
|
+
const date = new Date(value);
|
637
|
+
return this.isDate(date) && (date.getDay() === 0 || date.getDay() === 6);
|
638
|
+
}
|
639
|
+
|
640
|
+
isToday(value: any): boolean {
|
641
|
+
if (!this.isDate(value)) return false;
|
642
|
+
const today = new Date();
|
643
|
+
const date = new Date(value);
|
644
|
+
return (
|
645
|
+
date.getFullYear() === today.getFullYear() &&
|
646
|
+
date.getMonth() === today.getMonth() &&
|
647
|
+
date.getDate() === today.getDate()
|
648
|
+
);
|
649
|
+
}
|
650
|
+
|
651
|
+
isTomorrow(value: any): boolean {
|
652
|
+
if (!this.isDate(value)) return false;
|
653
|
+
const tomorrow = new Date();
|
654
|
+
tomorrow.setDate(tomorrow.getDate() + 1);
|
655
|
+
const date = new Date(value);
|
656
|
+
return (
|
657
|
+
date.getFullYear() === tomorrow.getFullYear() &&
|
658
|
+
date.getMonth() === tomorrow.getMonth() &&
|
659
|
+
date.getDate() === tomorrow.getDate()
|
660
|
+
);
|
661
|
+
}
|
662
|
+
|
663
|
+
// ------------------ Extended Error Checks ------------------
|
664
|
+
|
665
|
+
isError(value: any): boolean {
|
666
|
+
return value instanceof Error;
|
667
|
+
}
|
668
|
+
|
669
|
+
isTypeError(value: any): boolean {
|
670
|
+
return value instanceof TypeError;
|
671
|
+
}
|
672
|
+
|
673
|
+
isSyntaxError(value: any): boolean {
|
674
|
+
return value instanceof SyntaxError;
|
675
|
+
}
|
676
|
+
|
677
|
+
isRangeError(value: any): boolean {
|
678
|
+
return value instanceof RangeError;
|
679
|
+
}
|
680
|
+
}
|
681
|
+
const isThis = new IsThis();
|
682
|
+
export default isThis;
|