@ix-xs/node-comfort 1.2.0 → 2.1.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.
Files changed (78) hide show
  1. package/CHANGELOG.md +340 -0
  2. package/README.md +137 -548
  3. package/index.js +80 -35
  4. package/package.json +161 -33
  5. package/src/Arr.js +1050 -418
  6. package/src/Async.js +817 -0
  7. package/src/Cache.js +433 -0
  8. package/src/Checker.js +1202 -539
  9. package/src/Cli.js +1293 -0
  10. package/src/Color.js +556 -0
  11. package/src/Crypto.js +1082 -0
  12. package/src/Emitter.js +356 -0
  13. package/src/Env.js +689 -0
  14. package/src/FS.js +1626 -894
  15. package/src/Func.js +823 -356
  16. package/src/Http.js +700 -0
  17. package/src/Id.js +568 -200
  18. package/src/Logger.js +963 -378
  19. package/src/Num.js +965 -360
  20. package/src/Obj.js +1077 -379
  21. package/src/SQLite.js +1629 -1005
  22. package/src/Schema.js +2101 -0
  23. package/src/Str.js +1209 -432
  24. package/src/Sys.js +654 -0
  25. package/src/Time.js +1533 -440
  26. package/src/Utils.js +342 -325
  27. package/src/errors.js +335 -0
  28. package/src/internal/shared.js +415 -0
  29. package/types/index.d.ts +735 -526
  30. package/types/src/Arr.d.ts +533 -145
  31. package/types/src/Arr.d.ts.map +1 -1
  32. package/types/src/Async.d.ts +372 -0
  33. package/types/src/Async.d.ts.map +1 -0
  34. package/types/src/Cache.d.ts +221 -0
  35. package/types/src/Cache.d.ts.map +1 -0
  36. package/types/src/Checker.d.ts +651 -232
  37. package/types/src/Checker.d.ts.map +1 -1
  38. package/types/src/Cli.d.ts +664 -0
  39. package/types/src/Cli.d.ts.map +1 -0
  40. package/types/src/Color.d.ts +300 -0
  41. package/types/src/Color.d.ts.map +1 -0
  42. package/types/src/Crypto.d.ts +620 -0
  43. package/types/src/Crypto.d.ts.map +1 -0
  44. package/types/src/Emitter.d.ts +189 -0
  45. package/types/src/Emitter.d.ts.map +1 -0
  46. package/types/src/Env.d.ts +412 -0
  47. package/types/src/Env.d.ts.map +1 -0
  48. package/types/src/FS.d.ts +717 -238
  49. package/types/src/FS.d.ts.map +1 -1
  50. package/types/src/Func.d.ts +508 -135
  51. package/types/src/Func.d.ts.map +1 -1
  52. package/types/src/Http.d.ts +444 -0
  53. package/types/src/Http.d.ts.map +1 -0
  54. package/types/src/Id.d.ts +199 -66
  55. package/types/src/Id.d.ts.map +1 -1
  56. package/types/src/Logger.d.ts +414 -46
  57. package/types/src/Logger.d.ts.map +1 -1
  58. package/types/src/Num.d.ts +514 -137
  59. package/types/src/Num.d.ts.map +1 -1
  60. package/types/src/Obj.d.ts +453 -104
  61. package/types/src/Obj.d.ts.map +1 -1
  62. package/types/src/SQLite.d.ts +754 -308
  63. package/types/src/SQLite.d.ts.map +1 -1
  64. package/types/src/Schema.d.ts +1189 -0
  65. package/types/src/Schema.d.ts.map +1 -0
  66. package/types/src/Str.d.ts +617 -166
  67. package/types/src/Str.d.ts.map +1 -1
  68. package/types/src/Sys.d.ts +314 -0
  69. package/types/src/Sys.d.ts.map +1 -0
  70. package/types/src/Time.d.ts +759 -204
  71. package/types/src/Time.d.ts.map +1 -1
  72. package/types/src/Utils.d.ts +158 -102
  73. package/types/src/Utils.d.ts.map +1 -1
  74. package/types/src/errors.d.ts +316 -0
  75. package/types/src/errors.d.ts.map +1 -0
  76. package/types/src/internal/shared.d.ts +123 -0
  77. package/types/src/internal/shared.d.ts.map +1 -0
  78. package/types/index.d.ts.map +0 -1
@@ -1,407 +1,826 @@
1
1
  /**
2
- * Checks if a value is a real Array.
2
+ * Any built-in typed array.
3
+ */
4
+ export type AnyTypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
5
+ /**
6
+ * Any primitive value.
7
+ */
8
+ export type Primitive = string | number | boolean | symbol | bigint | null | undefined;
9
+ /**
10
+ * Options for `isURL()`.
11
+ */
12
+ export type IsURLOptions = {
13
+ /**
14
+ * Accepted protocols, colon included. Defaults to `["http:", "https:"]`.
15
+ */
16
+ protocols?: string[] | undefined;
17
+ /**
18
+ * Require a domain like `example.com`, rejecting `localhost` and bare IPs.
19
+ */
20
+ requireTld?: boolean | undefined;
21
+ };
22
+ /**
23
+ * Options for `isUUID()`.
24
+ */
25
+ export type IsUUIDOptions = {
26
+ /**
27
+ * Only accept this version. Otherwise versions 1 to 8 pass, plus the nil and max UUIDs.
28
+ */
29
+ version?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | undefined;
30
+ };
31
+ /**
32
+ * Options for `isHostname()`.
33
+ */
34
+ export type IsHostnameOptions = {
35
+ /**
36
+ * Require a domain like `example.com`, rejecting `localhost`.
37
+ */
38
+ requireTld?: boolean | undefined;
39
+ };
40
+ /**
41
+ * Options for `isBase64()`.
42
+ */
43
+ export type IsBase64Options = {
44
+ /**
45
+ * Expect the URL-safe alphabet (`-` and `_`), padding optional.
46
+ */
47
+ urlSafe?: boolean | undefined;
48
+ };
49
+ /**
50
+ * Any built-in typed array.
51
+ * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} AnyTypedArray
52
+ */
53
+ /**
54
+ * Any primitive value.
55
+ * @typedef {string | number | boolean | symbol | bigint | null | undefined} Primitive
56
+ */
57
+ /**
58
+ * Options for `isURL()`.
59
+ * @typedef {object} IsURLOptions
60
+ * @property {string[]} [protocols] Accepted protocols, colon included. Defaults to `["http:", "https:"]`.
61
+ * @property {boolean} [requireTld] Require a domain like `example.com`, rejecting `localhost` and bare IPs.
62
+ */
63
+ /**
64
+ * Options for `isUUID()`.
65
+ * @typedef {object} IsUUIDOptions
66
+ * @property {1|2|3|4|5|6|7|8} [version] Only accept this version. Otherwise versions 1 to 8 pass, plus the nil and max UUIDs.
67
+ */
68
+ /**
69
+ * Options for `isHostname()`.
70
+ * @typedef {object} IsHostnameOptions
71
+ * @property {boolean} [requireTld] Require a domain like `example.com`, rejecting `localhost`.
72
+ */
73
+ /**
74
+ * Options for `isBase64()`.
75
+ * @typedef {object} IsBase64Options
76
+ * @property {boolean} [urlSafe] Expect the URL-safe alphabet (`-` and `_`), padding optional.
77
+ */
78
+ /**
79
+ * Is it an array?
80
+ *
81
+ * @example
82
+ * nc.isArray([1, 2]); // true
83
+ * nc.isArray({ length: 0 }); // false
84
+ *
85
+ * @param {unknown} value
86
+ * @returns {value is any[]}
87
+ */
88
+ export function isArray(value: unknown): value is any[];
89
+ /**
90
+ * Is it a number? `NaN` is rejected, `Infinity` passes (see `isFinite`).
91
+ *
92
+ * @example
93
+ * nc.isNumber(42); // true
94
+ * nc.isNumber(NaN); // false
95
+ * nc.isNumber("42"); // false, use isNumeric for strings
96
+ *
97
+ * @param {unknown} value
98
+ * @returns {value is number}
99
+ */
100
+ export function isNumber(value: unknown): value is number;
101
+ /**
102
+ * Is it a finite number? Unlike the global `isFinite`, strings are rejected.
103
+ *
104
+ * @example
105
+ * nc.isFinite(42); // true
106
+ * nc.isFinite(Infinity); // false
107
+ * nc.isFinite("42"); // false
108
+ *
109
+ * @param {unknown} value
110
+ * @returns {value is number}
111
+ */
112
+ export function isFinite(value: unknown): value is number;
113
+ /**
114
+ * Is it an integer?
115
+ *
116
+ * @example
117
+ * nc.isInteger(4); // true
118
+ * nc.isInteger(4.5); // false
119
+ *
120
+ * @param {unknown} value
121
+ * @returns {value is number}
122
+ */
123
+ export function isInteger(value: unknown): value is number;
124
+ /**
125
+ * Is it an integer JavaScript can represent exactly (up to 2^53 - 1)?
126
+ *
127
+ * @example
128
+ * nc.isSafeInteger(2 ** 53 - 1); // true
129
+ * nc.isSafeInteger(2 ** 53); // false
130
+ *
131
+ * @param {unknown} value
132
+ * @returns {value is number}
133
+ */
134
+ export function isSafeInteger(value: unknown): value is number;
135
+ /**
136
+ * Is it a finite number with a decimal part?
137
+ *
138
+ * @example
139
+ * nc.isFloat(4.2); // true
140
+ * nc.isFloat(4); // false
141
+ *
142
+ * @param {unknown} value
143
+ * @returns {value is number}
144
+ */
145
+ export function isFloat(value: unknown): value is number;
146
+ /**
147
+ * Is it a number greater than zero?
148
+ *
149
+ * @example
150
+ * nc.isPositive(3); // true
151
+ * nc.isPositive(0); // false
152
+ *
153
+ * @param {unknown} value
154
+ * @returns {value is number}
155
+ */
156
+ export function isPositive(value: unknown): value is number;
157
+ /**
158
+ * Is it a number lower than zero?
159
+ *
160
+ * @example
161
+ * nc.isNegative(-3); // true
162
+ * nc.isNegative(0); // false
163
+ *
164
+ * @param {unknown} value
165
+ * @returns {value is number}
166
+ */
167
+ export function isNegative(value: unknown): value is number;
168
+ /**
169
+ * Is it `true` or `false`? Truthy and falsy values don't count.
3
170
  *
4
171
  * @example
5
- * nodeComfort.isArray([1, 2, 3]); // true
6
- * nodeComfort.isArray("not array"); // false
172
+ * nc.isBoolean(false); // true
173
+ * nc.isBoolean(0); // false
7
174
  *
8
- * @param {*} value - Value to check.
9
- * @returns {boolean} True if the value is an Array.
175
+ * @param {unknown} value
176
+ * @returns {value is boolean}
10
177
  */
11
- export function isArray(value: any): boolean;
178
+ export function isBoolean(value: unknown): value is boolean;
12
179
  /**
13
- * Checks if a value is a number (excluding NaN).
180
+ * Is it a string?
14
181
  *
15
182
  * @example
16
- * nodeComfort.isNumber(42); // true
17
- * nodeComfort.isNumber(NaN); // false
18
- * nodeComfort.isNumber("42"); // false
183
+ * nc.isString("hello"); // true
184
+ * nc.isString(new String("hello")); // false
185
+ *
186
+ * @param {unknown} value
187
+ * @returns {value is string}
188
+ */
189
+ export function isString(value: unknown): value is string;
190
+ /**
191
+ * Is it a symbol?
19
192
  *
20
- * @param {*} value - Value to check.
21
- * @returns {boolean} True if the value is a finite number or Infinity/-Infinity.
193
+ * @param {unknown} value
194
+ * @returns {value is symbol}
22
195
  */
23
- export function isNumber(value: any): boolean;
196
+ export function isSymbol(value: unknown): value is symbol;
24
197
  /**
25
- * Checks if a value is a finite number (excludes NaN, Infinity and -Infinity).
198
+ * Is it a bigint?
26
199
  *
27
200
  * @example
28
- * nodeComfort.isFinite(42); // true
29
- * nodeComfort.isFinite(Infinity); // false
201
+ * nc.isBigInt(10n); // true
202
+ * nc.isBigInt(10); // false
30
203
  *
31
- * @param {*} value - Value to check.
32
- * @returns {boolean} True if the value is a finite number.
204
+ * @param {unknown} value
205
+ * @returns {value is bigint}
33
206
  */
34
- export function isFinite(value: any): boolean;
207
+ export function isBigInt(value: unknown): value is bigint;
35
208
  /**
36
- * Checks if a value is a safe integer.
209
+ * Is it `undefined`?
210
+ *
211
+ * @param {unknown} value
212
+ * @returns {value is undefined}
213
+ */
214
+ export function isUndefined(value: unknown): value is undefined;
215
+ /**
216
+ * Is it `null`?
217
+ *
218
+ * @param {unknown} value
219
+ * @returns {value is null}
220
+ */
221
+ export function isNull(value: unknown): value is null;
222
+ /**
223
+ * Is it `null` or `undefined`?
37
224
  *
38
225
  * @example
39
- * nodeComfort.isInteger(42); // true
40
- * nodeComfort.isInteger(4.2); // false
226
+ * nc.isNil(undefined); // true
227
+ * nc.isNil(0); // false
228
+ *
229
+ * @param {unknown} value
230
+ * @returns {value is null | undefined}
231
+ */
232
+ export function isNil(value: unknown): value is null | undefined;
233
+ /**
234
+ * Is it anything but `null` or `undefined`? Pass it to `filter()` to drop
235
+ * missing values and keep the right type.
236
+ *
237
+ * @example
238
+ * const ids = [1, null, 2, undefined].filter(nc.isDefined); // number[]
239
+ *
240
+ * @template T
241
+ * @param {T} value
242
+ * @returns {value is NonNullable<T>}
243
+ */
244
+ export function isDefined<T>(value: T): value is NonNullable<T>;
245
+ /**
246
+ * Is it a primitive (string, number, boolean, symbol, bigint, `null` or `undefined`)?
247
+ *
248
+ * @param {unknown} value
249
+ * @returns {value is Primitive}
250
+ */
251
+ export function isPrimitive(value: unknown): value is Primitive;
252
+ /**
253
+ * Is it callable? Classes, async and generator functions count.
41
254
  *
42
- * @param {*} value - Value to check.
43
- * @returns {boolean} True if the value is an integer.
255
+ * @param {unknown} value
256
+ * @returns {value is (...args: any[]) => any}
44
257
  */
45
- export function isInteger(value: any): boolean;
258
+ export function isFunction(value: unknown): value is (...args: any[]) => any;
46
259
  /**
47
- * Checks if a value is a floating-point number (a finite non-integer number).
260
+ * Was it declared with `async`? A function that merely returns a promise
261
+ * doesn't count.
48
262
  *
49
263
  * @example
50
- * nodeComfort.isFloat(4.2); // true
51
- * nodeComfort.isFloat(4); // false
264
+ * nc.isAsyncFunction(async () => {}); // true
265
+ * nc.isAsyncFunction(() => Promise.resolve()); // false
52
266
  *
53
- * @param {*} value - Value to check.
54
- * @returns {boolean} True if the value is a non-integer finite number.
267
+ * @param {unknown} value
268
+ * @returns {value is (...args: any[]) => Promise<any>}
55
269
  */
56
- export function isFloat(value: any): boolean;
270
+ export function isAsyncFunction(value: unknown): value is (...args: any[]) => Promise<any>;
57
271
  /**
58
- * Checks if a value is a function.
272
+ * Is it a generator function (`function*`)?
273
+ *
274
+ * @param {unknown} value
275
+ * @returns {value is (...args: any[]) => Generator}
276
+ */
277
+ export function isGeneratorFunction(value: unknown): value is (...args: any[]) => Generator;
278
+ /**
279
+ * Is it a generator object, the result of calling a `function*`?
280
+ *
281
+ * @param {unknown} value
282
+ * @returns {value is Generator}
283
+ */
284
+ export function isGenerator(value: unknown): value is Generator;
285
+ /**
286
+ * Is it a `class` rather than a plain function?
59
287
  *
60
288
  * @example
61
- * nodeComfort.isFunction(() => {}); // true
62
- * nodeComfort.isFunction(class {}); // true
63
- * nodeComfort.isFunction("fn"); // false
289
+ * nc.isClass(class User {}); // true
290
+ * nc.isClass(function () {}); // false
64
291
  *
65
- * @param {*} value - Value to check.
66
- * @returns {boolean} True if the value is a function.
292
+ * @param {unknown} value
293
+ * @returns {value is new (...args: any[]) => any}
67
294
  */
68
- export function isFunction(value: any): boolean;
295
+ export function isClass(value: unknown): value is new (...args: any[]) => any;
69
296
  /**
70
- * Checks if a value is an async function (declared with `async`).
297
+ * Is it an object, and not `null`? Arrays, dates and class instances count;
298
+ * use `isPlainObject` for `{}` literals only.
299
+ *
300
+ * @param {unknown} value
301
+ * @returns {value is object}
302
+ */
303
+ export function isObject(value: unknown): value is object;
304
+ /**
305
+ * Is it a plain object, made with `{}`, `new Object()` or
306
+ * `Object.create(null)`? Arrays, dates, maps and class instances are rejected.
71
307
  *
72
308
  * @example
73
- * nodeComfort.isAsyncFunction(async () => {}); // true
74
- * nodeComfort.isAsyncFunction(() => {}); // false
309
+ * nc.isPlainObject({ a: 1 }); // true
310
+ * nc.isPlainObject(new Date()); // false
75
311
  *
76
- * @param {*} value - Value to check.
77
- * @returns {boolean} True if the value is an async function.
312
+ * @param {unknown} value
313
+ * @returns {value is Record<string, unknown>}
78
314
  */
79
- export function isAsyncFunction(value: any): boolean;
315
+ export function isPlainObject(value: unknown): value is Record<string, unknown>;
80
316
  /**
81
- * Checks if a value is a non-null object.
317
+ * Can it be awaited like a promise (anything with a `then` method)?
82
318
  *
83
- * Note: Arrays, Maps, Sets, Dates, etc. are also considered objects here.
319
+ * @param {unknown} value
320
+ * @returns {value is PromiseLike<any>}
321
+ */
322
+ export function isPromise(value: unknown): value is PromiseLike<any>;
323
+ /**
324
+ * Is it a regular expression?
325
+ *
326
+ * @param {unknown} value
327
+ * @returns {value is RegExp}
328
+ */
329
+ export function isRegExp(value: unknown): value is RegExp;
330
+ /**
331
+ * Is it a `Date`, valid or not? Use `isValidDate` to reject `Invalid Date`.
332
+ *
333
+ * @param {unknown} value
334
+ * @returns {value is Date}
335
+ */
336
+ export function isDate(value: unknown): value is Date;
337
+ /**
338
+ * Is it a `Date` holding a real point in time?
84
339
  *
85
340
  * @example
86
- * nodeComfort.isObject({}); // true
87
- * nodeComfort.isObject([]); // true
88
- * nodeComfort.isObject(null); // false
89
- * nodeComfort.isObject("str"); // false
341
+ * nc.isValidDate(new Date()); // true
342
+ * nc.isValidDate(new Date("nope")); // false
343
+ *
344
+ * @param {unknown} value
345
+ * @returns {value is Date}
346
+ */
347
+ export function isValidDate(value: unknown): value is Date;
348
+ /**
349
+ * Is it a `Map`?
350
+ *
351
+ * @param {unknown} value
352
+ * @returns {value is Map<unknown, unknown>}
353
+ */
354
+ export function isMap(value: unknown): value is Map<unknown, unknown>;
355
+ /**
356
+ * Is it a `Set`?
357
+ *
358
+ * @param {unknown} value
359
+ * @returns {value is Set<unknown>}
360
+ */
361
+ export function isSet(value: unknown): value is Set<unknown>;
362
+ /**
363
+ * Is it a `WeakMap`?
364
+ *
365
+ * @param {unknown} value
366
+ * @returns {value is WeakMap<object, unknown>}
367
+ */
368
+ export function isWeakMap(value: unknown): value is WeakMap<object, unknown>;
369
+ /**
370
+ * Is it a `WeakSet`?
371
+ *
372
+ * @param {unknown} value
373
+ * @returns {value is WeakSet<object>}
374
+ */
375
+ export function isWeakSet(value: unknown): value is WeakSet<object>;
376
+ /**
377
+ * Does it work with `for...of`? Strings, arrays, maps, sets and generators do.
90
378
  *
91
- * @param {*} value - Value to check.
92
- * @returns {boolean} True if the value is a non-null object.
379
+ * @param {unknown} value
380
+ * @returns {value is Iterable<unknown>}
93
381
  */
94
- export function isObject(value: any): boolean;
382
+ export function isIterable(value: unknown): value is Iterable<unknown>;
95
383
  /**
96
- * Checks if a value is a plain object (created via `{}` or `new Object()`).
384
+ * Does it work with `for await...of`, like streams and async generators?
97
385
  *
98
- * Unlike {@link isObject}, this excludes arrays, Maps, Dates, class instances, etc.
386
+ * @param {unknown} value
387
+ * @returns {value is AsyncIterable<unknown>}
388
+ */
389
+ export function isAsyncIterable(value: unknown): value is AsyncIterable<unknown>;
390
+ /**
391
+ * Is it a Node.js `Buffer`?
392
+ *
393
+ * @param {unknown} value
394
+ * @returns {value is Buffer}
395
+ */
396
+ export function isBuffer(value: unknown): value is Buffer;
397
+ /**
398
+ * Is it a typed array (`Uint8Array`, `Float64Array`...)? Buffers count.
399
+ *
400
+ * @param {unknown} value
401
+ * @returns {value is AnyTypedArray}
402
+ */
403
+ export function isTypedArray(value: unknown): value is AnyTypedArray;
404
+ /**
405
+ * Is it an `Error`, including subclasses and errors from other realms?
406
+ *
407
+ * @param {unknown} value
408
+ * @returns {value is Error}
409
+ */
410
+ export function isError(value: unknown): value is Error;
411
+ /**
412
+ * Is it empty? `null`, `undefined`, `""`, `[]`, `{}` and empty maps and sets
413
+ * are. Numbers, booleans, functions, dates, regular expressions and errors
414
+ * never are.
99
415
  *
100
416
  * @example
101
- * nodeComfort.isPlainObject({ a: 1 }); // true
102
- * nodeComfort.isPlainObject([]); // false
103
- * nodeComfort.isPlainObject(new Date()); // false
417
+ * nc.isEmpty({}); // true
418
+ * nc.isEmpty(0); // false
419
+ * nc.isEmpty(" "); // false, see isBlank
104
420
  *
105
- * @param {*} value - Value to check.
106
- * @returns {boolean} True if the value is a plain object.
421
+ * @param {unknown} value
422
+ * @returns {boolean}
107
423
  */
108
- export function isPlainObject(value: any): boolean;
424
+ export function isEmpty(value: unknown): boolean;
109
425
  /**
110
- * Checks if a value is a boolean.
426
+ * Is it `null`, `undefined`, or a string with nothing but whitespace?
111
427
  *
112
428
  * @example
113
- * nodeComfort.isBoolean(true); // true
114
- * nodeComfort.isBoolean(false); // true
115
- * nodeComfort.isBoolean(0); // false
429
+ * nc.isBlank(" \n"); // true
430
+ * nc.isBlank(" a "); // false
116
431
  *
117
- * @param {*} value - Value to check.
118
- * @returns {boolean} True if the value is a boolean.
432
+ * @param {unknown} value
433
+ * @returns {boolean}
119
434
  */
120
- export function isBoolean(value: any): boolean;
435
+ export function isBlank(value: unknown): boolean;
121
436
  /**
122
- * Checks if a value is a string.
437
+ * Is it an array where every item passes `guard`?
123
438
  *
124
439
  * @example
125
- * nodeComfort.isString("hello"); // true
126
- * nodeComfort.isString(123); // false
440
+ * if (nc.isArrayOf(input, nc.isString)) input.join(", "); // input: string[]
127
441
  *
128
- * @param {*} value - Value to check.
129
- * @returns {boolean} True if the value is a string.
442
+ * @template T
443
+ * @param {unknown} value
444
+ * @param {(item: unknown) => item is T} guard Checked against every item, like `nc.isString`.
445
+ * @returns {value is T[]}
130
446
  */
131
- export function isString(value: any): boolean;
447
+ export function isArrayOf<T>(value: unknown, guard: (item: unknown) => item is T): value is T[];
132
448
  /**
133
- * Checks if a value is a symbol.
449
+ * Is it one of the allowed values? With a constant list, the value gets the
450
+ * matching literal type.
134
451
  *
135
452
  * @example
136
- * nodeComfort.isSymbol(Symbol("x")); // true
453
+ * const ROLES = ["admin", "user"] as const;
454
+ * if (nc.isOneOf(input, ROLES)) input; // "admin" | "user"
137
455
  *
138
- * @param {*} value - Value to check.
139
- * @returns {boolean} True if the value is a symbol.
456
+ * @template {readonly unknown[]} L
457
+ * @param {unknown} value
458
+ * @param {L} allowed
459
+ * @returns {value is L[number]}
140
460
  */
141
- export function isSymbol(value: any): boolean;
461
+ export function isOneOf<L extends readonly unknown[]>(value: unknown, allowed: L): value is L[number];
142
462
  /**
143
- * Checks if a value is a BigInt.
463
+ * Does it look like an email address? The rules are practical rather than
464
+ * the full RFC: one `@`, no stray dots, a real top-level domain.
465
+ * International addresses are accepted.
144
466
  *
145
467
  * @example
146
- * nodeComfort.isBigInt(10n); // true
147
- * nodeComfort.isBigInt(10); // false
468
+ * nc.isEmail("jose@exemple.fr"); // true
469
+ * nc.isEmail("john..doe@example.com"); // false
470
+ * nc.isEmail("john@localhost"); // false
148
471
  *
149
- * @param {*} value - Value to check.
150
- * @returns {boolean} True if the value is a BigInt.
472
+ * @param {unknown} value
473
+ * @returns {value is string}
151
474
  */
152
- export function isBigInt(value: any): boolean;
475
+ export function isEmail(value: unknown): value is string;
153
476
  /**
154
- * Checks if a value is exactly undefined.
477
+ * Is it an absolute URL with an allowed protocol (http and https by default)?
155
478
  *
156
479
  * @example
157
- * nodeComfort.isUndefined(undefined); // true
158
- * nodeComfort.isUndefined(null); // false
480
+ * nc.isURL("https://example.com/a?b=1"); // true
481
+ * nc.isURL("ftp://example.com", { protocols: ["ftp:"] }); // true
482
+ * nc.isURL("http://localhost:3000", { requireTld: true }); // false
159
483
  *
160
- * @param {*} value - Value to check.
161
- * @returns {boolean} True if the value is undefined.
484
+ * @param {unknown} value
485
+ * @param {IsURLOptions} [options]
486
+ * @returns {value is string}
162
487
  */
163
- export function isUndefined(value: any): boolean;
488
+ export function isURL(value: unknown, options?: IsURLOptions): value is string;
164
489
  /**
165
- * Checks if a value is exactly null.
490
+ * Is it a UUID?
166
491
  *
167
492
  * @example
168
- * nodeComfort.isNull(null); // true
169
- * nodeComfort.isNull(undefined); // false
493
+ * nc.isUUID(nc.id.uuid()); // true
494
+ * nc.isUUID(id, { version: 7 }); // only v7
170
495
  *
171
- * @param {*} value - Value to check.
172
- * @returns {boolean} True if the value is null.
496
+ * @param {unknown} value
497
+ * @param {IsUUIDOptions} [options]
498
+ * @returns {value is string}
173
499
  */
174
- export function isNull(value: any): boolean;
500
+ export function isUUID(value: unknown, options?: IsUUIDOptions): value is string;
175
501
  /**
176
- * Checks if a value is `null` or `undefined`.
502
+ * Is it a string of valid JSON?
177
503
  *
178
504
  * @example
179
- * nodeComfort.isNil(null); // true
180
- * nodeComfort.isNil(undefined); // true
181
- * nodeComfort.isNil(0); // false
505
+ * nc.isJSON('{"a":1}'); // true
506
+ * nc.isJSON("{a:1}"); // false
182
507
  *
183
- * @param {*} value - Value to check.
184
- * @returns {boolean} True if the value is null or undefined.
508
+ * @param {unknown} value
509
+ * @returns {value is string}
185
510
  */
186
- export function isNil(value: any): boolean;
511
+ export function isJSON(value: unknown): value is string;
187
512
  /**
188
- * Checks if a value is a primitive (string, number, boolean, symbol, bigint, null or undefined).
513
+ * Is it a finite number, or a string that is exactly one (spaces around are fine)?
189
514
  *
190
515
  * @example
191
- * nodeComfort.isPrimitive(42); // true
192
- * nodeComfort.isPrimitive({}); // false
516
+ * nc.isNumeric("4.2e3"); // true
517
+ * nc.isNumeric(" 12 "); // true
518
+ * nc.isNumeric("12px"); // false
193
519
  *
194
- * @param {*} value - Value to check.
195
- * @returns {boolean} True if the value is a primitive.
520
+ * @param {unknown} value
521
+ * @returns {value is number | string}
196
522
  */
197
- export function isPrimitive(value: any): boolean;
523
+ export function isNumeric(value: unknown): value is number | string;
198
524
  /**
199
- * Checks if a value is a Promise (or a thenable object).
525
+ * Is it an IP address?
200
526
  *
201
527
  * @example
202
- * nodeComfort.isPromise(Promise.resolve()); // true
203
- * nodeComfort.isPromise({ then() {} }); // true
204
- * nodeComfort.isPromise(42); // false
528
+ * nc.isIP("192.168.0.1"); // true
529
+ * nc.isIP("::1"); // true
530
+ * nc.isIP("::1", 4); // false
531
+ *
532
+ * @param {unknown} value
533
+ * @param {4|6} [version] Only accept IPv4 or IPv6.
534
+ * @returns {value is string}
535
+ */
536
+ export function isIP(value: unknown, version?: 4 | 6): value is string;
537
+ /**
538
+ * Is it an IPv4 address, like `192.168.0.1`?
205
539
  *
206
- * @param {*} value - Value to check.
207
- * @returns {boolean} True if the value is a Promise or thenable.
540
+ * @param {unknown} value
541
+ * @returns {value is string}
208
542
  */
209
- export function isPromise(value: any): boolean;
543
+ export function isIPv4(value: unknown): value is string;
210
544
  /**
211
- * Checks if a value is a RegExp instance.
545
+ * Is it an IPv6 address, like `::1`?
546
+ *
547
+ * @param {unknown} value
548
+ * @returns {value is string}
549
+ */
550
+ export function isIPv6(value: unknown): value is string;
551
+ /**
552
+ * Is it a valid port (0 to 65535), as a number or a numeric string?
212
553
  *
213
554
  * @example
214
- * nodeComfort.isRegExp(/a/); // true
215
- * nodeComfort.isRegExp("a"); // false
555
+ * nc.isPort("8080"); // true
556
+ * nc.isPort(70000); // false
216
557
  *
217
- * @param {*} value - Value to check.
218
- * @returns {boolean} True if the value is a RegExp.
558
+ * @param {unknown} value
559
+ * @returns {boolean}
219
560
  */
220
- export function isRegExp(value: any): boolean;
561
+ export function isPort(value: unknown): boolean;
221
562
  /**
222
- * Checks if a value is a built-in Date instance.
563
+ * Is it a hexadecimal string? A `0x` prefix is allowed.
223
564
  *
224
- * Uses `util.types.isDate` under the hood to handle cross-realm Dates.
565
+ * @param {unknown} value
566
+ * @returns {value is string}
567
+ */
568
+ export function isHex(value: unknown): value is string;
569
+ /**
570
+ * Is it a CSS hex color (`#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`)?
225
571
  *
226
572
  * @example
227
- * nodeComfort.isDate(new Date()); // true
228
- * nodeComfort.isDate("2024-01-01"); // false
573
+ * nc.isHexColor("#f80"); // true
574
+ * nc.isHexColor("f80"); // false, the # is required
229
575
  *
230
- * @param {*} value - Value to check.
231
- * @returns {boolean} True if the value is a Date instance.
576
+ * @param {unknown} value
577
+ * @returns {value is string}
232
578
  */
233
- export function isDate(value: any): boolean;
579
+ export function isHexColor(value: unknown): value is string;
234
580
  /**
235
- * Checks if a value is a **valid** Date (a Date instance that is not `Invalid Date`).
581
+ * Is it valid Base64?
236
582
  *
237
583
  * @example
238
- * nodeComfort.isValidDate(new Date()); // true
239
- * nodeComfort.isValidDate(new Date("nope")); // false
584
+ * nc.isBase64("aGVsbG8="); // true
585
+ * nc.isBase64("aGVsbG8", { urlSafe: true }); // true
240
586
  *
241
- * @param {*} value - Value to check.
242
- * @returns {boolean} True if the value is a valid Date.
587
+ * @param {unknown} value
588
+ * @param {IsBase64Options} [options]
589
+ * @returns {value is string}
243
590
  */
244
- export function isValidDate(value: any): boolean;
591
+ export function isBase64(value: unknown, options?: IsBase64Options): value is string;
245
592
  /**
246
- * Checks if a value is a built-in Map instance.
593
+ * Is it a semantic version? A leading `v` is fine.
247
594
  *
248
595
  * @example
249
- * nodeComfort.isMap(new Map()); // true
250
- * nodeComfort.isMap({}); // false
596
+ * nc.isSemver("v2.0.0-rc.1"); // true
597
+ * nc.isSemver("1.2"); // false
251
598
  *
252
- * @param {*} value - Value to check.
253
- * @returns {boolean} True if the value is a Map.
599
+ * @param {unknown} value
600
+ * @returns {value is string}
254
601
  */
255
- export function isMap(value: any): boolean;
602
+ export function isSemver(value: unknown): value is string;
256
603
  /**
257
- * Checks if a value is a built-in Set instance.
604
+ * Is it an ISO 8601 date or date-time, and a real calendar date?
258
605
  *
259
606
  * @example
260
- * nodeComfort.isSet(new Set()); // true
261
- * nodeComfort.isSet([]); // false
607
+ * nc.isISODate("2024-02-29T10:00:00Z"); // true
608
+ * nc.isISODate("2023-02-29"); // false, not a leap year
262
609
  *
263
- * @param {*} value - Value to check.
264
- * @returns {boolean} True if the value is a Set.
610
+ * @param {unknown} value
611
+ * @returns {value is string}
265
612
  */
266
- export function isSet(value: any): boolean;
613
+ export function isISODate(value: unknown): value is string;
267
614
  /**
268
- * Checks if a value is a WeakMap instance.
615
+ * Is it a URL slug like `my-first-post`? See `nc.str.slugify()` to make one.
616
+ *
617
+ * @param {unknown} value
618
+ * @returns {value is string}
619
+ */
620
+ export function isSlug(value: unknown): value is string;
621
+ /**
622
+ * Is it made only of letters, in any alphabet?
623
+ *
624
+ * @example
625
+ * nc.isAlpha("Élodie"); // true
626
+ * nc.isAlpha("abc1"); // false
627
+ *
628
+ * @param {unknown} value
629
+ * @returns {value is string}
630
+ */
631
+ export function isAlpha(value: unknown): value is string;
632
+ /**
633
+ * Is it made only of letters and digits, in any alphabet?
634
+ *
635
+ * @param {unknown} value
636
+ * @returns {value is string}
637
+ */
638
+ export function isAlphanumeric(value: unknown): value is string;
639
+ /**
640
+ * Could it be a card number? Checks the length and the Luhn checksum;
641
+ * spaces and dashes are ignored.
642
+ *
643
+ * @example
644
+ * nc.isCreditCard("4242 4242 4242 4242"); // true
269
645
  *
270
- * @param {*} value - Value to check.
271
- * @returns {boolean} True if the value is a WeakMap.
646
+ * @param {unknown} value
647
+ * @returns {value is string}
272
648
  */
273
- export function isWeakMap(value: any): boolean;
649
+ export function isCreditCard(value: unknown): value is string;
274
650
  /**
275
- * Checks if a value is a WeakSet instance.
651
+ * Does it look like a JSON Web Token? The signature is not verified; use
652
+ * `nc.crypto.verifyJWT()` for that.
276
653
  *
277
- * @param {*} value - Value to check.
278
- * @returns {boolean} True if the value is a WeakSet.
654
+ * @param {unknown} value
655
+ * @returns {value is string}
279
656
  */
280
- export function isWeakSet(value: any): boolean;
657
+ export function isJWT(value: unknown): value is string;
281
658
  /**
282
- * Checks if a value is an iterable (has a `Symbol.iterator`).
659
+ * Is it a host name, like `example.com`, `api.internal` or `localhost`?
660
+ * International names are accepted. IP addresses aren't: see `isIP`.
661
+ *
662
+ * @example
663
+ * nc.isHostname("api.example.com"); // true
664
+ * nc.isHostname("localhost"); // true
665
+ * nc.isHostname("localhost", { requireTld: true }); // false
666
+ * nc.isHostname("-bad-.com"); // false
283
667
  *
284
- * Strings, arrays, Maps, Sets and generators are iterable.
668
+ * @param {unknown} value
669
+ * @param {IsHostnameOptions} [options]
670
+ * @returns {value is string}
671
+ */
672
+ export function isHostname(value: unknown, options?: IsHostnameOptions): value is string;
673
+ /**
674
+ * Is it a MAC address? Accepts `aa:bb:cc:dd:ee:ff`, `aa-bb-cc-dd-ee-ff` and
675
+ * `aabb.ccdd.eeff`.
285
676
  *
286
677
  * @example
287
- * nodeComfort.isIterable([1, 2]); // true
288
- * nodeComfort.isIterable("hi"); // true
289
- * nodeComfort.isIterable({}); // false
678
+ * nc.isMACAddress("00:1B:44:11:3A:B7"); // true
679
+ * nc.isMACAddress("00:1B:44:11:3A"); // false
290
680
  *
291
- * @param {*} value - Value to check.
292
- * @returns {boolean} True if the value is iterable.
681
+ * @param {unknown} value
682
+ * @returns {value is string}
293
683
  */
294
- export function isIterable(value: any): boolean;
684
+ export function isMACAddress(value: unknown): value is string;
295
685
  /**
296
- * Checks if a value is a Node.js Buffer.
686
+ * Is it a valid IBAN? Checks the format and the checksum, spaces allowed.
297
687
  *
298
- * @param {*} value - Value to check.
299
- * @returns {boolean} True if the value is a Buffer.
688
+ * @example
689
+ * nc.isIBAN("FR76 3000 6000 0112 3456 7890 189"); // true
690
+ * nc.isIBAN("FR76 3000 6000 0112 3456 7890 180"); // false, wrong checksum
691
+ *
692
+ * @param {unknown} value
693
+ * @returns {value is string}
300
694
  */
301
- export function isBuffer(value: any): boolean;
695
+ export function isIBAN(value: unknown): value is string;
302
696
  /**
303
- * Checks if a value is any TypedArray (Uint8Array, Float64Array, etc.).
697
+ * Is it a phone number in the international E.164 format: a `+`, then 7 to
698
+ * 15 digits, no spaces? That's what SMS and telephony APIs expect.
699
+ *
700
+ * @example
701
+ * nc.isE164("+33612345678"); // true
702
+ * nc.isE164("06 12 34 56 78"); // false
304
703
  *
305
- * @param {*} value - Value to check.
306
- * @returns {boolean} True if the value is a TypedArray.
704
+ * @param {unknown} value
705
+ * @returns {value is string}
307
706
  */
308
- export function isTypedArray(value: any): boolean;
707
+ export function isE164(value: unknown): value is string;
309
708
  /**
310
- * Checks if a value is a native Error instance.
709
+ * Is it a MongoDB ObjectId, 24 hexadecimal characters?
311
710
  *
312
- * Uses `util.types.isNativeError` to correctly handle errors from other realms.
711
+ * @example
712
+ * nc.isObjectId("507f1f77bcf86cd799439011"); // true
713
+ *
714
+ * @param {unknown} value
715
+ * @returns {value is string}
716
+ */
717
+ export function isObjectId(value: unknown): value is string;
718
+ /**
719
+ * Is it a ULID, like the ones `nc.id.ulid()` makes?
313
720
  *
314
721
  * @example
315
- * nodeComfort.isError(new Error("x")); // true
316
- * nodeComfort.isError(new TypeError("x")); // true
317
- * nodeComfort.isError({ message: "x" }); // false
722
+ * nc.isULID(nc.id.ulid()); // true
318
723
  *
319
- * @param {*} value - Value to check.
320
- * @returns {boolean} True if the value is a native Error.
724
+ * @param {unknown} value
725
+ * @returns {value is string}
321
726
  */
322
- export function isError(value: any): boolean;
727
+ export function isULID(value: unknown): value is string;
323
728
  /**
324
- * Checks if a value is "empty".
729
+ * Is it a `data:` URL, like an inline image?
325
730
  *
326
- * Emptiness rules:
327
- * - `null` / `undefined` -> empty
328
- * - string / array -> empty when `length === 0`
329
- * - Map / Set -> empty when `size === 0`
330
- * - plain object -> empty when it has no own enumerable keys
331
- * - anything else (numbers, booleans, functions, ...) -> never empty
731
+ * @example
732
+ * nc.isDataURL("data:image/png;base64,iVBORw0KGgo="); // true
733
+ * nc.isDataURL("data:,Hello"); // true
734
+ *
735
+ * @param {unknown} value
736
+ * @returns {value is string}
737
+ */
738
+ export function isDataURL(value: unknown): value is string;
739
+ /**
740
+ * Is it a media type, like `application/json` or
741
+ * `text/html; charset=utf-8`?
332
742
  *
333
743
  * @example
334
- * nodeComfort.isEmpty(""); // true
335
- * nodeComfort.isEmpty([]); // true
336
- * nodeComfort.isEmpty({}); // true
337
- * nodeComfort.isEmpty(new Map()); // true
338
- * nodeComfort.isEmpty("x"); // false
339
- * nodeComfort.isEmpty(0); // false
744
+ * nc.isMimeType("image/svg+xml"); // true
745
+ * nc.isMimeType("json"); // false
340
746
  *
341
- * @param {*} value - Value to check.
342
- * @returns {boolean} True if the value is considered empty.
747
+ * @param {unknown} value
748
+ * @returns {value is string}
343
749
  */
344
- export function isEmpty(value: any): boolean;
750
+ export function isMimeType(value: unknown): value is string;
345
751
  /**
346
- * Checks if a string is a valid, well-formed email address.
752
+ * Is it a time zone Node.js knows, like `"Europe/Paris"` or `"UTC"`?
753
+ *
754
+ * @example
755
+ * nc.isTimeZone("America/New_York"); // true
756
+ * nc.isTimeZone("Mars/Olympus"); // false
347
757
  *
348
- * This is a pragmatic check (not a full RFC 5322 parser) that covers the
349
- * vast majority of real-world addresses.
758
+ * @param {unknown} value
759
+ * @returns {value is string}
760
+ */
761
+ export function isTimeZone(value: unknown): value is string;
762
+ /**
763
+ * Is it a well-formed locale, like `"fr"`, `"en-GB"` or `"zh-Hant-TW"`?
350
764
  *
351
765
  * @example
352
- * nodeComfort.isEmail("john@example.com"); // true
353
- * nodeComfort.isEmail("nope"); // false
766
+ * nc.isLocale("pt-BR"); // true
767
+ * nc.isLocale("en_US"); // false, use a dash
354
768
  *
355
- * @param {*} value - Value to check.
356
- * @returns {boolean} True if the value looks like an email address.
769
+ * @param {unknown} value
770
+ * @returns {value is string}
357
771
  */
358
- export function isEmail(value: any): boolean;
772
+ export function isLocale(value: unknown): value is string;
359
773
  /**
360
- * Checks if a string is a valid HTTP(S) URL.
774
+ * Is it a cron expression `nc.time.cron()` understands?
361
775
  *
362
776
  * @example
363
- * nodeComfort.isURL("https://example.com"); // true
364
- * nodeComfort.isURL("ftp://example.com"); // false (not http/https)
777
+ * nc.isCron("0 9 * * 1-5"); // true
778
+ * nc.isCron("@daily"); // true
779
+ * nc.isCron("61 * * * *"); // false
365
780
  *
366
- * @param {*} value - Value to check.
367
- * @param {{ protocols?: string[] }} [options] - Allowed protocols (default: http, https).
368
- * @returns {boolean} True if the value is a valid URL with an allowed protocol.
781
+ * @param {unknown} value
782
+ * @returns {value is string}
369
783
  */
370
- export function isURL(value: any, options?: {
371
- protocols?: string[];
372
- }): boolean;
784
+ export function isCron(value: unknown): value is string;
373
785
  /**
374
- * Checks if a string is a valid UUID (versions 1-5, any variant).
786
+ * Is it made only of ASCII characters?
375
787
  *
376
788
  * @example
377
- * nodeComfort.isUUID("123e4567-e89b-12d3-a456-426614174000"); // true
789
+ * nc.isASCII("hello"); // true
790
+ * nc.isASCII("héllo"); // false
378
791
  *
379
- * @param {*} value - Value to check.
380
- * @returns {boolean} True if the value is a UUID.
792
+ * @param {unknown} value
793
+ * @returns {value is string}
381
794
  */
382
- export function isUUID(value: any): boolean;
795
+ export function isASCII(value: unknown): value is string;
383
796
  /**
384
- * Checks if a string is valid, parseable JSON.
797
+ * Throws an `AssertionError` if `condition` is falsy. Afterwards, TypeScript
798
+ * knows the condition holds.
385
799
  *
386
800
  * @example
387
- * nodeComfort.isJSON('{"a":1}'); // true
388
- * nodeComfort.isJSON("{a:1}"); // false
801
+ * const user = users.find((u) => u.id === id);
802
+ * nc.assert(user, `User ${id} not found`);
803
+ * user.name; // no "possibly undefined" here
389
804
  *
390
- * @param {*} value - Value to check.
391
- * @returns {boolean} True if the value is a JSON-parseable string.
805
+ * @param {unknown} condition
806
+ * @param {string | (() => string)} [message="Assertion failed"] A message, or a function that builds it.
807
+ * @returns {asserts condition}
808
+ * @throws {AssertionError}
392
809
  */
393
- export function isJSON(value: any): boolean;
810
+ export function assert(condition: unknown, message?: string | (() => string)): asserts condition;
394
811
  /**
395
- * Checks if a value is numeric • a number, or a string that fully represents one.
812
+ * Throws an `AssertionError` unless `value` passes `guard`. Afterwards,
813
+ * `value` has the guarded type.
396
814
  *
397
815
  * @example
398
- * nodeComfort.isNumeric(42); // true
399
- * nodeComfort.isNumeric("42"); // true
400
- * nodeComfort.isNumeric("4.2e3"); // true
401
- * nodeComfort.isNumeric("4px"); // false
816
+ * nc.assertType(config.port, nc.isInteger, "port must be an integer");
402
817
  *
403
- * @param {*} value - Value to check.
404
- * @returns {boolean} True if the value is numeric.
818
+ * @template T
819
+ * @param {unknown} value
820
+ * @param {(value: unknown) => value is T} guard A check like `nc.isString`.
821
+ * @param {string | (() => string)} [message]
822
+ * @returns {asserts value is T}
823
+ * @throws {AssertionError}
405
824
  */
406
- export function isNumeric(value: any): boolean;
825
+ export function assertType<T>(value: unknown, guard: (value: unknown) => value is T, message?: string | (() => string)): asserts value is T;
407
826
  //# sourceMappingURL=Checker.d.ts.map