@ix-xs/node-comfort 1.1.31 → 2.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.
Files changed (77) hide show
  1. package/CHANGELOG.md +190 -0
  2. package/LICENSE +21 -0
  3. package/README.md +210 -748
  4. package/index.js +84 -13
  5. package/package.json +201 -21
  6. package/src/Arr.js +901 -0
  7. package/src/Async.js +585 -0
  8. package/src/Cache.js +329 -0
  9. package/src/Checker.js +979 -188
  10. package/src/Cli.js +1034 -0
  11. package/src/Color.js +415 -0
  12. package/src/Crypto.js +707 -0
  13. package/src/Emitter.js +289 -0
  14. package/src/Env.js +555 -0
  15. package/src/FS.js +1483 -768
  16. package/src/Func.js +692 -0
  17. package/src/Http.js +538 -0
  18. package/src/Id.js +418 -0
  19. package/src/Logger.js +739 -307
  20. package/src/Num.js +849 -0
  21. package/src/Obj.js +904 -0
  22. package/src/SQLite.js +1308 -994
  23. package/src/Schema.js +1586 -0
  24. package/src/Str.js +999 -0
  25. package/src/Sys.js +511 -0
  26. package/src/Time.js +1323 -0
  27. package/src/Utils.js +321 -325
  28. package/src/errors.js +292 -0
  29. package/types/index.d.ts +693 -120
  30. package/types/src/Arr.d.ts +562 -0
  31. package/types/src/Arr.d.ts.map +1 -0
  32. package/types/src/Async.d.ts +321 -0
  33. package/types/src/Async.d.ts.map +1 -0
  34. package/types/src/Cache.d.ts +195 -0
  35. package/types/src/Cache.d.ts.map +1 -0
  36. package/types/src/Checker.d.ts +609 -81
  37. package/types/src/Checker.d.ts.map +1 -1
  38. package/types/src/Cli.d.ts +582 -0
  39. package/types/src/Cli.d.ts.map +1 -0
  40. package/types/src/Color.d.ts +294 -0
  41. package/types/src/Color.d.ts.map +1 -0
  42. package/types/src/Crypto.d.ts +534 -0
  43. package/types/src/Crypto.d.ts.map +1 -0
  44. package/types/src/Emitter.d.ts +170 -0
  45. package/types/src/Emitter.d.ts.map +1 -0
  46. package/types/src/Env.d.ts +382 -0
  47. package/types/src/Env.d.ts.map +1 -0
  48. package/types/src/FS.d.ts +709 -201
  49. package/types/src/FS.d.ts.map +1 -1
  50. package/types/src/Func.d.ts +455 -0
  51. package/types/src/Func.d.ts.map +1 -0
  52. package/types/src/Http.d.ts +374 -0
  53. package/types/src/Http.d.ts.map +1 -0
  54. package/types/src/Id.d.ts +200 -0
  55. package/types/src/Id.d.ts.map +1 -0
  56. package/types/src/Logger.d.ts +374 -24
  57. package/types/src/Logger.d.ts.map +1 -1
  58. package/types/src/Num.d.ts +578 -0
  59. package/types/src/Num.d.ts.map +1 -0
  60. package/types/src/Obj.d.ts +475 -0
  61. package/types/src/Obj.d.ts.map +1 -0
  62. package/types/src/SQLite.d.ts +647 -305
  63. package/types/src/SQLite.d.ts.map +1 -1
  64. package/types/src/Schema.d.ts +940 -0
  65. package/types/src/Schema.d.ts.map +1 -0
  66. package/types/src/Str.d.ts +696 -0
  67. package/types/src/Str.d.ts.map +1 -0
  68. package/types/src/Sys.d.ts +269 -0
  69. package/types/src/Sys.d.ts.map +1 -0
  70. package/types/src/Time.d.ts +664 -0
  71. package/types/src/Time.d.ts.map +1 -0
  72. package/types/src/Utils.d.ts +154 -102
  73. package/types/src/Utils.d.ts.map +1 -1
  74. package/types/src/errors.d.ts +292 -0
  75. package/types/src/errors.d.ts.map +1 -0
  76. package/tsconfig.json +0 -18
  77. package/types/index.d.ts.map +0 -1
@@ -1,145 +1,673 @@
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?: 2 | 1 | 3 | 4 | 5 | 6 | 7 | 8 | undefined;
30
+ };
31
+ /**
32
+ * Options for `isBase64()`.
33
+ */
34
+ export type IsBase64Options = {
35
+ /**
36
+ * Expect the URL-safe alphabet (`-` and `_`), padding optional.
37
+ */
38
+ urlSafe?: boolean | undefined;
39
+ };
40
+ /**
41
+ * Any built-in typed array.
42
+ * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} AnyTypedArray
43
+ */
44
+ /**
45
+ * Any primitive value.
46
+ * @typedef {string | number | boolean | symbol | bigint | null | undefined} Primitive
47
+ */
48
+ /**
49
+ * Options for `isURL()`.
50
+ * @typedef {object} IsURLOptions
51
+ * @property {string[]} [protocols] Accepted protocols, colon included. Defaults to `["http:", "https:"]`.
52
+ * @property {boolean} [requireTld] Require a domain like `example.com`, rejecting `localhost` and bare IPs.
53
+ */
54
+ /**
55
+ * Options for `isUUID()`.
56
+ * @typedef {object} IsUUIDOptions
57
+ * @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.
58
+ */
59
+ /**
60
+ * Options for `isBase64()`.
61
+ * @typedef {object} IsBase64Options
62
+ * @property {boolean} [urlSafe] Expect the URL-safe alphabet (`-` and `_`), padding optional.
63
+ */
64
+ /**
65
+ * Is it an array?
66
+ *
67
+ * @example
68
+ * nc.isArray([1, 2]); // true
69
+ * nc.isArray({ length: 0 }); // false
70
+ *
71
+ * @param {unknown} value
72
+ * @returns {value is any[]}
73
+ */
74
+ export function isArray(value: unknown): value is any[];
75
+ /**
76
+ * Is it a number? `NaN` is rejected, `Infinity` passes (see `isFinite`).
77
+ *
78
+ * @example
79
+ * nc.isNumber(42); // true
80
+ * nc.isNumber(NaN); // false
81
+ * nc.isNumber("42"); // false, use isNumeric for strings
82
+ *
83
+ * @param {unknown} value
84
+ * @returns {value is number}
85
+ */
86
+ export function isNumber(value: unknown): value is number;
87
+ /**
88
+ * Is it a finite number? Unlike the global `isFinite`, strings are rejected.
89
+ *
90
+ * @example
91
+ * nc.isFinite(42); // true
92
+ * nc.isFinite(Infinity); // false
93
+ * nc.isFinite("42"); // false
94
+ *
95
+ * @param {unknown} value
96
+ * @returns {value is number}
97
+ */
98
+ export function isFinite(value: unknown): value is number;
99
+ /**
100
+ * Is it an integer?
3
101
  *
4
102
  * @example
5
- * nodeComfort.Checker.isArray([1, 2, 3]); // true
6
- * nodeComfort.Checker.isArray("not array"); // false
103
+ * nc.isInteger(4); // true
104
+ * nc.isInteger(4.5); // false
7
105
  *
8
- * @param {*} value - Value to check.
9
- * @returns {boolean} True if the value is an Array.
106
+ * @param {unknown} value
107
+ * @returns {value is number}
10
108
  */
11
- export function isArray(value: any): boolean;
109
+ export function isInteger(value: unknown): value is number;
12
110
  /**
13
- * Checks if a value is a number (excluding NaN).
111
+ * Is it an integer JavaScript can represent exactly (up to 2^53 - 1)?
14
112
  *
15
113
  * @example
16
- * nodeComfort.Checker.isNumber(42); // true
17
- * nodeComfort.Checker.isNumber(NaN); // false
18
- * nodeComfort.Checker.isNumber("42"); // false
114
+ * nc.isSafeInteger(2 ** 53 - 1); // true
115
+ * nc.isSafeInteger(2 ** 53); // false
19
116
  *
20
- * @param {*} value - Value to check.
21
- * @returns {boolean} True if the value is a finite number or Infinity/-Infinity.
117
+ * @param {unknown} value
118
+ * @returns {value is number}
22
119
  */
23
- export function isNumber(value: any): boolean;
120
+ export function isSafeInteger(value: unknown): value is number;
24
121
  /**
25
- * Checks if a value is a function.
122
+ * Is it a finite number with a decimal part?
26
123
  *
27
124
  * @example
28
- * nodeComfort.Checker.isFunction(() => {}); // true
29
- * nodeComfort.Checker.isFunction(class {}); // true
30
- * nodeComfort.Checker.isFunction("fn"); // false
125
+ * nc.isFloat(4.2); // true
126
+ * nc.isFloat(4); // false
31
127
  *
32
- * @param {*} value - Value to check.
33
- * @returns {boolean} True if the value is a function.
128
+ * @param {unknown} value
129
+ * @returns {value is number}
34
130
  */
35
- export function isFunction(value: any): boolean;
131
+ export function isFloat(value: unknown): value is number;
36
132
  /**
37
- * Checks if a value is a non-null object.
133
+ * Is it a number greater than zero?
38
134
  *
39
- * Note: Arrays, Maps, Sets, Dates, etc. are also considered objects here.
135
+ * @example
136
+ * nc.isPositive(3); // true
137
+ * nc.isPositive(0); // false
138
+ *
139
+ * @param {unknown} value
140
+ * @returns {value is number}
141
+ */
142
+ export function isPositive(value: unknown): value is number;
143
+ /**
144
+ * Is it a number lower than zero?
40
145
  *
41
146
  * @example
42
- * nodeComfort.Checker.isObject({}); // true
43
- * nodeComfort.Checker.isObject([]); // true
44
- * nodeComfort.Checker.isObject(null); // false
45
- * nodeComfort.Checker.isObject("str"); // false
147
+ * nc.isNegative(-3); // true
148
+ * nc.isNegative(0); // false
46
149
  *
47
- * @param {*} value - Value to check.
48
- * @returns {boolean} True if the value is a non-null object.
150
+ * @param {unknown} value
151
+ * @returns {value is number}
49
152
  */
50
- export function isObject(value: any): boolean;
153
+ export function isNegative(value: unknown): value is number;
51
154
  /**
52
- * Checks if a value is a boolean.
155
+ * Is it `true` or `false`? Truthy and falsy values don't count.
53
156
  *
54
157
  * @example
55
- * nodeComfort.Checker.isBoolean(true); // true
56
- * nodeComfort.Checker.isBoolean(false); // true
57
- * nodeComfort.Checker.isBoolean(0); // false
158
+ * nc.isBoolean(false); // true
159
+ * nc.isBoolean(0); // false
160
+ *
161
+ * @param {unknown} value
162
+ * @returns {value is boolean}
163
+ */
164
+ export function isBoolean(value: unknown): value is boolean;
165
+ /**
166
+ * Is it a string?
167
+ *
168
+ * @example
169
+ * nc.isString("hello"); // true
170
+ * nc.isString(new String("hello")); // false
171
+ *
172
+ * @param {unknown} value
173
+ * @returns {value is string}
174
+ */
175
+ export function isString(value: unknown): value is string;
176
+ /**
177
+ * Is it a symbol?
178
+ *
179
+ * @param {unknown} value
180
+ * @returns {value is symbol}
181
+ */
182
+ export function isSymbol(value: unknown): value is symbol;
183
+ /**
184
+ * Is it a bigint?
185
+ *
186
+ * @example
187
+ * nc.isBigInt(10n); // true
188
+ * nc.isBigInt(10); // false
189
+ *
190
+ * @param {unknown} value
191
+ * @returns {value is bigint}
192
+ */
193
+ export function isBigInt(value: unknown): value is bigint;
194
+ /**
195
+ * Is it `undefined`?
196
+ *
197
+ * @param {unknown} value
198
+ * @returns {value is undefined}
199
+ */
200
+ export function isUndefined(value: unknown): value is undefined;
201
+ /**
202
+ * Is it `null`?
58
203
  *
59
- * @param {*} value - Value to check.
60
- * @returns {boolean} True if the value is a boolean.
204
+ * @param {unknown} value
205
+ * @returns {value is null}
61
206
  */
62
- export function isBoolean(value: any): boolean;
207
+ export function isNull(value: unknown): value is null;
63
208
  /**
64
- * Checks if a value is a string.
209
+ * Is it `null` or `undefined`?
65
210
  *
66
211
  * @example
67
- * nodeComfort.Checker.isString("hello"); // true
68
- * nodeComfort.Checker.isString(123); // false
212
+ * nc.isNil(undefined); // true
213
+ * nc.isNil(0); // false
69
214
  *
70
- * @param {*} value - Value to check.
71
- * @returns {boolean} True if the value is a string.
215
+ * @param {unknown} value
216
+ * @returns {value is null | undefined}
72
217
  */
73
- export function isString(value: any): boolean;
218
+ export function isNil(value: unknown): value is null | undefined;
74
219
  /**
75
- * Checks if a value is exactly undefined.
220
+ * Is it anything but `null` or `undefined`? Pass it to `filter()` to drop
221
+ * missing values and keep the right type.
76
222
  *
77
223
  * @example
78
- * nodeComfort.Checker.isUndefined(undefined); // true
79
- * nodeComfort.Checker.isUndefined(null); // false
224
+ * const ids = [1, null, 2, undefined].filter(nc.isDefined); // number[]
225
+ *
226
+ * @template T
227
+ * @param {T} value
228
+ * @returns {value is NonNullable<T>}
229
+ */
230
+ export function isDefined<T>(value: T): value is NonNullable<T>;
231
+ /**
232
+ * Is it a primitive (string, number, boolean, symbol, bigint, `null` or `undefined`)?
80
233
  *
81
- * @param {*} value - Value to check.
82
- * @returns {boolean} True if the value is undefined.
234
+ * @param {unknown} value
235
+ * @returns {value is Primitive}
83
236
  */
84
- export function isUndefined(value: any): boolean;
237
+ export function isPrimitive(value: unknown): value is Primitive;
85
238
  /**
86
- * Checks if a value is exactly null.
239
+ * Is it callable? Classes, async and generator functions count.
240
+ *
241
+ * @param {unknown} value
242
+ * @returns {value is (...args: any[]) => any}
243
+ */
244
+ export function isFunction(value: unknown): value is (...args: any[]) => any;
245
+ /**
246
+ * Was it declared with `async`? A function that merely returns a promise
247
+ * doesn't count.
87
248
  *
88
249
  * @example
89
- * nodeComfort.Checker.isNull(null); // true
90
- * nodeComfort.Checker.isNull(undefined); // false
250
+ * nc.isAsyncFunction(async () => {}); // true
251
+ * nc.isAsyncFunction(() => Promise.resolve()); // false
91
252
  *
92
- * @param {*} value - Value to check.
93
- * @returns {boolean} True if the value is null.
253
+ * @param {unknown} value
254
+ * @returns {value is (...args: any[]) => Promise<any>}
94
255
  */
95
- export function isNull(value: any): boolean;
256
+ export function isAsyncFunction(value: unknown): value is (...args: any[]) => Promise<any>;
96
257
  /**
97
- * Checks if a value is a built-in Date instance.
258
+ * Is it a generator function (`function*`)?
98
259
  *
99
- * Uses `util.types.isDate` under the hood to handle cross-realm Dates.
260
+ * @param {unknown} value
261
+ * @returns {value is (...args: any[]) => Generator}
262
+ */
263
+ export function isGeneratorFunction(value: unknown): value is (...args: any[]) => Generator;
264
+ /**
265
+ * Is it a generator object, the result of calling a `function*`?
266
+ *
267
+ * @param {unknown} value
268
+ * @returns {value is Generator}
269
+ */
270
+ export function isGenerator(value: unknown): value is Generator;
271
+ /**
272
+ * Is it a `class` rather than a plain function?
100
273
  *
101
274
  * @example
102
- * nodeComfort.Checker.isDate(new Date()); // true
103
- * nodeComfort.Checker.isDate("2024-01-01"); // false
275
+ * nc.isClass(class User {}); // true
276
+ * nc.isClass(function () {}); // false
277
+ *
278
+ * @param {unknown} value
279
+ * @returns {value is new (...args: any[]) => any}
280
+ */
281
+ export function isClass(value: unknown): value is new (...args: any[]) => any;
282
+ /**
283
+ * Is it an object, and not `null`? Arrays, dates and class instances count;
284
+ * use `isPlainObject` for `{}` literals only.
104
285
  *
105
- * @param {*} value - Value to check.
106
- * @returns {boolean} True if the value is a Date instance.
286
+ * @param {unknown} value
287
+ * @returns {value is object}
107
288
  */
108
- export function isDate(value: any): boolean;
289
+ export function isObject(value: unknown): value is object;
109
290
  /**
110
- * Checks if a value is a built-in Map instance.
291
+ * Is it a plain object, made with `{}`, `new Object()` or
292
+ * `Object.create(null)`? Arrays, dates, maps and class instances are rejected.
111
293
  *
112
294
  * @example
113
- * nodeComfort.Checker.isMap(new Map()); // true
114
- * nodeComfort.Checker.isMap({}); // false
295
+ * nc.isPlainObject({ a: 1 }); // true
296
+ * nc.isPlainObject(new Date()); // false
297
+ *
298
+ * @param {unknown} value
299
+ * @returns {value is Record<string, unknown>}
300
+ */
301
+ export function isPlainObject(value: unknown): value is Record<string, unknown>;
302
+ /**
303
+ * Can it be awaited like a promise (anything with a `then` method)?
304
+ *
305
+ * @param {unknown} value
306
+ * @returns {value is PromiseLike<any>}
307
+ */
308
+ export function isPromise(value: unknown): value is PromiseLike<any>;
309
+ /**
310
+ * Is it a regular expression?
311
+ *
312
+ * @param {unknown} value
313
+ * @returns {value is RegExp}
314
+ */
315
+ export function isRegExp(value: unknown): value is RegExp;
316
+ /**
317
+ * Is it a `Date`, valid or not? Use `isValidDate` to reject `Invalid Date`.
115
318
  *
116
- * @param {*} value - Value to check.
117
- * @returns {boolean} True if the value is a Map.
319
+ * @param {unknown} value
320
+ * @returns {value is Date}
118
321
  */
119
- export function isMap(value: any): boolean;
322
+ export function isDate(value: unknown): value is Date;
120
323
  /**
121
- * Checks if a value is a built-in Set instance.
324
+ * Is it a `Date` holding a real point in time?
122
325
  *
123
326
  * @example
124
- * nodeComfort.Checker.isSet(new Set()); // true
125
- * nodeComfort.Checker.isSet([]); // false
327
+ * nc.isValidDate(new Date()); // true
328
+ * nc.isValidDate(new Date("nope")); // false
329
+ *
330
+ * @param {unknown} value
331
+ * @returns {value is Date}
332
+ */
333
+ export function isValidDate(value: unknown): value is Date;
334
+ /**
335
+ * Is it a `Map`?
336
+ *
337
+ * @param {unknown} value
338
+ * @returns {value is Map<unknown, unknown>}
339
+ */
340
+ export function isMap(value: unknown): value is Map<unknown, unknown>;
341
+ /**
342
+ * Is it a `Set`?
343
+ *
344
+ * @param {unknown} value
345
+ * @returns {value is Set<unknown>}
346
+ */
347
+ export function isSet(value: unknown): value is Set<unknown>;
348
+ /**
349
+ * Is it a `WeakMap`?
350
+ *
351
+ * @param {unknown} value
352
+ * @returns {value is WeakMap<object, unknown>}
353
+ */
354
+ export function isWeakMap(value: unknown): value is WeakMap<object, unknown>;
355
+ /**
356
+ * Is it a `WeakSet`?
357
+ *
358
+ * @param {unknown} value
359
+ * @returns {value is WeakSet<object>}
360
+ */
361
+ export function isWeakSet(value: unknown): value is WeakSet<object>;
362
+ /**
363
+ * Does it work with `for...of`? Strings, arrays, maps, sets and generators do.
364
+ *
365
+ * @param {unknown} value
366
+ * @returns {value is Iterable<unknown>}
367
+ */
368
+ export function isIterable(value: unknown): value is Iterable<unknown>;
369
+ /**
370
+ * Does it work with `for await...of`, like streams and async generators?
371
+ *
372
+ * @param {unknown} value
373
+ * @returns {value is AsyncIterable<unknown>}
374
+ */
375
+ export function isAsyncIterable(value: unknown): value is AsyncIterable<unknown>;
376
+ /**
377
+ * Is it a Node.js `Buffer`?
378
+ *
379
+ * @param {unknown} value
380
+ * @returns {value is Buffer}
381
+ */
382
+ export function isBuffer(value: unknown): value is Buffer;
383
+ /**
384
+ * Is it a typed array (`Uint8Array`, `Float64Array`...)? Buffers count.
385
+ *
386
+ * @param {unknown} value
387
+ * @returns {value is AnyTypedArray}
388
+ */
389
+ export function isTypedArray(value: unknown): value is AnyTypedArray;
390
+ /**
391
+ * Is it an `Error`, including subclasses and errors from other realms?
126
392
  *
127
- * @param {*} value - Value to check.
128
- * @returns {boolean} True if the value is a Set.
393
+ * @param {unknown} value
394
+ * @returns {value is Error}
129
395
  */
130
- export function isSet(value: any): boolean;
396
+ export function isError(value: unknown): value is Error;
131
397
  /**
132
- * Checks if a value is a native Error instance.
398
+ * Is it empty? `null`, `undefined`, `""`, `[]`, `{}` and empty maps and sets
399
+ * are. Numbers, booleans and functions never are.
133
400
  *
134
- * Uses `util.types.isNativeError` to correctly handle errors from other realms.
401
+ * @example
402
+ * nc.isEmpty({}); // true
403
+ * nc.isEmpty(0); // false
404
+ * nc.isEmpty(" "); // false, see isBlank
405
+ *
406
+ * @param {unknown} value
407
+ * @returns {boolean}
408
+ */
409
+ export function isEmpty(value: unknown): boolean;
410
+ /**
411
+ * Is it `null`, `undefined`, or a string with nothing but whitespace?
412
+ *
413
+ * @example
414
+ * nc.isBlank(" \n"); // true
415
+ * nc.isBlank(" a "); // false
416
+ *
417
+ * @param {unknown} value
418
+ * @returns {boolean}
419
+ */
420
+ export function isBlank(value: unknown): boolean;
421
+ /**
422
+ * Is it an array where every item passes `guard`?
423
+ *
424
+ * @example
425
+ * if (nc.isArrayOf(input, nc.isString)) input.join(", "); // input: string[]
426
+ *
427
+ * @template T
428
+ * @param {unknown} value
429
+ * @param {(item: unknown) => item is T} guard Checked against every item, like `nc.isString`.
430
+ * @returns {value is T[]}
431
+ */
432
+ export function isArrayOf<T>(value: unknown, guard: (item: unknown) => item is T): value is T[];
433
+ /**
434
+ * Is it one of the allowed values? With a constant list, the value gets the
435
+ * matching literal type.
436
+ *
437
+ * @example
438
+ * const ROLES = ["admin", "user"] as const;
439
+ * if (nc.isOneOf(input, ROLES)) input; // "admin" | "user"
440
+ *
441
+ * @template {readonly unknown[]} L
442
+ * @param {unknown} value
443
+ * @param {L} allowed
444
+ * @returns {value is L[number]}
445
+ */
446
+ export function isOneOf<L extends readonly unknown[]>(value: unknown, allowed: L): value is L[number];
447
+ /**
448
+ * Does it look like an email address? The rules are practical rather than
449
+ * the full RFC: one `@`, no stray dots, a real top-level domain.
450
+ * International addresses are accepted.
451
+ *
452
+ * @example
453
+ * nc.isEmail("jose@exemple.fr"); // true
454
+ * nc.isEmail("john..doe@example.com"); // false
455
+ * nc.isEmail("john@localhost"); // false
456
+ *
457
+ * @param {unknown} value
458
+ * @returns {value is string}
459
+ */
460
+ export function isEmail(value: unknown): value is string;
461
+ /**
462
+ * Is it an absolute URL with an allowed protocol (http and https by default)?
463
+ *
464
+ * @example
465
+ * nc.isURL("https://example.com/a?b=1"); // true
466
+ * nc.isURL("ftp://example.com", { protocols: ["ftp:"] }); // true
467
+ * nc.isURL("http://localhost:3000", { requireTld: true }); // false
468
+ *
469
+ * @param {unknown} value
470
+ * @param {IsURLOptions} [options]
471
+ * @returns {value is string}
472
+ */
473
+ export function isURL(value: unknown, options?: IsURLOptions): value is string;
474
+ /**
475
+ * Is it a UUID?
476
+ *
477
+ * @example
478
+ * nc.isUUID(nc.id.uuid()); // true
479
+ * nc.isUUID(id, { version: 7 }); // only v7
480
+ *
481
+ * @param {unknown} value
482
+ * @param {IsUUIDOptions} [options]
483
+ * @returns {value is string}
484
+ */
485
+ export function isUUID(value: unknown, options?: IsUUIDOptions): value is string;
486
+ /**
487
+ * Is it a string of valid JSON?
488
+ *
489
+ * @example
490
+ * nc.isJSON('{"a":1}'); // true
491
+ * nc.isJSON("{a:1}"); // false
492
+ *
493
+ * @param {unknown} value
494
+ * @returns {value is string}
495
+ */
496
+ export function isJSON(value: unknown): value is string;
497
+ /**
498
+ * Is it a finite number, or a string that is exactly one (spaces around are fine)?
499
+ *
500
+ * @example
501
+ * nc.isNumeric("4.2e3"); // true
502
+ * nc.isNumeric(" 12 "); // true
503
+ * nc.isNumeric("12px"); // false
504
+ *
505
+ * @param {unknown} value
506
+ * @returns {value is number | string}
507
+ */
508
+ export function isNumeric(value: unknown): value is number | string;
509
+ /**
510
+ * Is it an IP address?
511
+ *
512
+ * @example
513
+ * nc.isIP("192.168.0.1"); // true
514
+ * nc.isIP("::1"); // true
515
+ * nc.isIP("::1", 4); // false
516
+ *
517
+ * @param {unknown} value
518
+ * @param {4|6} [version] Only accept IPv4 or IPv6.
519
+ * @returns {value is string}
520
+ */
521
+ export function isIP(value: unknown, version?: 4 | 6): value is string;
522
+ /**
523
+ * Is it an IPv4 address, like `192.168.0.1`?
524
+ *
525
+ * @param {unknown} value
526
+ * @returns {value is string}
527
+ */
528
+ export function isIPv4(value: unknown): value is string;
529
+ /**
530
+ * Is it an IPv6 address, like `::1`?
531
+ *
532
+ * @param {unknown} value
533
+ * @returns {value is string}
534
+ */
535
+ export function isIPv6(value: unknown): value is string;
536
+ /**
537
+ * Is it a valid port (0 to 65535), as a number or a numeric string?
538
+ *
539
+ * @example
540
+ * nc.isPort("8080"); // true
541
+ * nc.isPort(70000); // false
542
+ *
543
+ * @param {unknown} value
544
+ * @returns {boolean}
545
+ */
546
+ export function isPort(value: unknown): boolean;
547
+ /**
548
+ * Is it a hexadecimal string? A `0x` prefix is allowed.
549
+ *
550
+ * @param {unknown} value
551
+ * @returns {value is string}
552
+ */
553
+ export function isHex(value: unknown): value is string;
554
+ /**
555
+ * Is it a CSS hex color (`#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`)?
556
+ *
557
+ * @example
558
+ * nc.isHexColor("#f80"); // true
559
+ * nc.isHexColor("f80"); // false, the # is required
560
+ *
561
+ * @param {unknown} value
562
+ * @returns {value is string}
563
+ */
564
+ export function isHexColor(value: unknown): value is string;
565
+ /**
566
+ * Is it valid Base64?
567
+ *
568
+ * @example
569
+ * nc.isBase64("aGVsbG8="); // true
570
+ * nc.isBase64("aGVsbG8", { urlSafe: true }); // true
571
+ *
572
+ * @param {unknown} value
573
+ * @param {IsBase64Options} [options]
574
+ * @returns {value is string}
575
+ */
576
+ export function isBase64(value: unknown, options?: IsBase64Options): value is string;
577
+ /**
578
+ * Is it a semantic version? A leading `v` is fine.
579
+ *
580
+ * @example
581
+ * nc.isSemver("v2.0.0-rc.1"); // true
582
+ * nc.isSemver("1.2"); // false
583
+ *
584
+ * @param {unknown} value
585
+ * @returns {value is string}
586
+ */
587
+ export function isSemver(value: unknown): value is string;
588
+ /**
589
+ * Is it an ISO 8601 date or date-time, and a real calendar date?
590
+ *
591
+ * @example
592
+ * nc.isISODate("2024-02-29T10:00:00Z"); // true
593
+ * nc.isISODate("2023-02-29"); // false, not a leap year
594
+ *
595
+ * @param {unknown} value
596
+ * @returns {value is string}
597
+ */
598
+ export function isISODate(value: unknown): value is string;
599
+ /**
600
+ * Is it a URL slug like `my-first-post`? See `nc.str.slugify()` to make one.
601
+ *
602
+ * @param {unknown} value
603
+ * @returns {value is string}
604
+ */
605
+ export function isSlug(value: unknown): value is string;
606
+ /**
607
+ * Is it made only of letters, in any alphabet?
608
+ *
609
+ * @example
610
+ * nc.isAlpha("Élodie"); // true
611
+ * nc.isAlpha("abc1"); // false
612
+ *
613
+ * @param {unknown} value
614
+ * @returns {value is string}
615
+ */
616
+ export function isAlpha(value: unknown): value is string;
617
+ /**
618
+ * Is it made only of letters and digits, in any alphabet?
619
+ *
620
+ * @param {unknown} value
621
+ * @returns {value is string}
622
+ */
623
+ export function isAlphanumeric(value: unknown): value is string;
624
+ /**
625
+ * Could it be a card number? Checks the length and the Luhn checksum;
626
+ * spaces and dashes are ignored.
627
+ *
628
+ * @example
629
+ * nc.isCreditCard("4242 4242 4242 4242"); // true
630
+ *
631
+ * @param {unknown} value
632
+ * @returns {value is string}
633
+ */
634
+ export function isCreditCard(value: unknown): value is string;
635
+ /**
636
+ * Does it look like a JSON Web Token? The signature is not verified; use
637
+ * `nc.crypto.verifyJWT()` for that.
638
+ *
639
+ * @param {unknown} value
640
+ * @returns {value is string}
641
+ */
642
+ export function isJWT(value: unknown): value is string;
643
+ /**
644
+ * Throws an `AssertionError` if `condition` is falsy. Afterwards, TypeScript
645
+ * knows the condition holds.
646
+ *
647
+ * @example
648
+ * const user = users.find((u) => u.id === id);
649
+ * nc.assert(user, `User ${id} not found`);
650
+ * user.name; // no "possibly undefined" here
651
+ *
652
+ * @param {unknown} condition
653
+ * @param {string | (() => string)} [message="Assertion failed"] A message, or a function that builds it.
654
+ * @returns {asserts condition}
655
+ * @throws {AssertionError}
656
+ */
657
+ export function assert(condition: unknown, message?: string | (() => string)): asserts condition;
658
+ /**
659
+ * Throws an `AssertionError` unless `value` passes `guard`. Afterwards,
660
+ * `value` has the guarded type.
135
661
  *
136
662
  * @example
137
- * nodeComfort.Checker.isError(new Error("x")); // true
138
- * nodeComfort.Checker.isError(new TypeError("x")); // true
139
- * nodeComfort.Checker.isError({ message: "x" }); // false
663
+ * nc.assertType(config.port, nc.isInteger, "port must be an integer");
140
664
  *
141
- * @param {*} value - Value to check.
142
- * @returns {boolean} True if the value is a native Error.
665
+ * @template T
666
+ * @param {unknown} value
667
+ * @param {(value: unknown) => value is T} guard A check like `nc.isString`.
668
+ * @param {string | (() => string)} [message]
669
+ * @returns {asserts value is T}
670
+ * @throws {AssertionError}
143
671
  */
144
- export function isError(value: any): boolean;
672
+ export function assertType<T>(value: unknown, guard: (value: unknown) => value is T, message?: string | (() => string)): asserts value is T;
145
673
  //# sourceMappingURL=Checker.d.ts.map