@ix-xs/node-comfort 1.2.0 → 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 (75) hide show
  1. package/CHANGELOG.md +190 -0
  2. package/README.md +136 -549
  3. package/index.js +80 -35
  4. package/package.json +160 -33
  5. package/src/Arr.js +887 -419
  6. package/src/Async.js +585 -0
  7. package/src/Cache.js +329 -0
  8. package/src/Checker.js +971 -539
  9. package/src/Cli.js +1034 -0
  10. package/src/Color.js +415 -0
  11. package/src/Crypto.js +707 -0
  12. package/src/Emitter.js +289 -0
  13. package/src/Env.js +555 -0
  14. package/src/FS.js +1483 -894
  15. package/src/Func.js +670 -353
  16. package/src/Http.js +538 -0
  17. package/src/Id.js +384 -201
  18. package/src/Logger.js +739 -378
  19. package/src/Num.js +844 -361
  20. package/src/Obj.js +855 -372
  21. package/src/SQLite.js +1308 -1005
  22. package/src/Schema.js +1586 -0
  23. package/src/Str.js +983 -436
  24. package/src/Sys.js +511 -0
  25. package/src/Time.js +1260 -422
  26. package/src/Utils.js +321 -325
  27. package/src/errors.js +292 -0
  28. package/types/index.d.ts +693 -526
  29. package/types/src/Arr.d.ts +455 -145
  30. package/types/src/Arr.d.ts.map +1 -1
  31. package/types/src/Async.d.ts +321 -0
  32. package/types/src/Async.d.ts.map +1 -0
  33. package/types/src/Cache.d.ts +195 -0
  34. package/types/src/Cache.d.ts.map +1 -0
  35. package/types/src/Checker.d.ts +501 -235
  36. package/types/src/Checker.d.ts.map +1 -1
  37. package/types/src/Cli.d.ts +582 -0
  38. package/types/src/Cli.d.ts.map +1 -0
  39. package/types/src/Color.d.ts +294 -0
  40. package/types/src/Color.d.ts.map +1 -0
  41. package/types/src/Crypto.d.ts +534 -0
  42. package/types/src/Crypto.d.ts.map +1 -0
  43. package/types/src/Emitter.d.ts +170 -0
  44. package/types/src/Emitter.d.ts.map +1 -0
  45. package/types/src/Env.d.ts +382 -0
  46. package/types/src/Env.d.ts.map +1 -0
  47. package/types/src/FS.d.ts +686 -238
  48. package/types/src/FS.d.ts.map +1 -1
  49. package/types/src/Func.d.ts +380 -135
  50. package/types/src/Func.d.ts.map +1 -1
  51. package/types/src/Http.d.ts +374 -0
  52. package/types/src/Http.d.ts.map +1 -0
  53. package/types/src/Id.d.ts +149 -68
  54. package/types/src/Id.d.ts.map +1 -1
  55. package/types/src/Logger.d.ts +363 -46
  56. package/types/src/Logger.d.ts.map +1 -1
  57. package/types/src/Num.d.ts +473 -137
  58. package/types/src/Num.d.ts.map +1 -1
  59. package/types/src/Obj.d.ts +390 -104
  60. package/types/src/Obj.d.ts.map +1 -1
  61. package/types/src/SQLite.d.ts +646 -309
  62. package/types/src/SQLite.d.ts.map +1 -1
  63. package/types/src/Schema.d.ts +940 -0
  64. package/types/src/Schema.d.ts.map +1 -0
  65. package/types/src/Str.d.ts +569 -166
  66. package/types/src/Str.d.ts.map +1 -1
  67. package/types/src/Sys.d.ts +269 -0
  68. package/types/src/Sys.d.ts.map +1 -0
  69. package/types/src/Time.d.ts +663 -204
  70. package/types/src/Time.d.ts.map +1 -1
  71. package/types/src/Utils.d.ts +154 -102
  72. package/types/src/Utils.d.ts.map +1 -1
  73. package/types/src/errors.d.ts +292 -0
  74. package/types/src/errors.d.ts.map +1 -0
  75. package/types/index.d.ts.map +0 -1
@@ -1,407 +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?
3
66
  *
4
67
  * @example
5
- * nodeComfort.isArray([1, 2, 3]); // true
6
- * nodeComfort.isArray("not array"); // false
68
+ * nc.isArray([1, 2]); // true
69
+ * nc.isArray({ length: 0 }); // false
7
70
  *
8
- * @param {*} value - Value to check.
9
- * @returns {boolean} True if the value is an Array.
71
+ * @param {unknown} value
72
+ * @returns {value is any[]}
10
73
  */
11
- export function isArray(value: any): boolean;
74
+ export function isArray(value: unknown): value is any[];
12
75
  /**
13
- * Checks if a value is a number (excluding NaN).
76
+ * Is it a number? `NaN` is rejected, `Infinity` passes (see `isFinite`).
14
77
  *
15
78
  * @example
16
- * nodeComfort.isNumber(42); // true
17
- * nodeComfort.isNumber(NaN); // false
18
- * nodeComfort.isNumber("42"); // false
79
+ * nc.isNumber(42); // true
80
+ * nc.isNumber(NaN); // false
81
+ * nc.isNumber("42"); // false, use isNumeric for strings
19
82
  *
20
- * @param {*} value - Value to check.
21
- * @returns {boolean} True if the value is a finite number or Infinity/-Infinity.
83
+ * @param {unknown} value
84
+ * @returns {value is number}
22
85
  */
23
- export function isNumber(value: any): boolean;
86
+ export function isNumber(value: unknown): value is number;
24
87
  /**
25
- * Checks if a value is a finite number (excludes NaN, Infinity and -Infinity).
88
+ * Is it a finite number? Unlike the global `isFinite`, strings are rejected.
26
89
  *
27
90
  * @example
28
- * nodeComfort.isFinite(42); // true
29
- * nodeComfort.isFinite(Infinity); // false
91
+ * nc.isFinite(42); // true
92
+ * nc.isFinite(Infinity); // false
93
+ * nc.isFinite("42"); // false
30
94
  *
31
- * @param {*} value - Value to check.
32
- * @returns {boolean} True if the value is a finite number.
95
+ * @param {unknown} value
96
+ * @returns {value is number}
33
97
  */
34
- export function isFinite(value: any): boolean;
98
+ export function isFinite(value: unknown): value is number;
35
99
  /**
36
- * Checks if a value is a safe integer.
100
+ * Is it an integer?
37
101
  *
38
102
  * @example
39
- * nodeComfort.isInteger(42); // true
40
- * nodeComfort.isInteger(4.2); // false
103
+ * nc.isInteger(4); // true
104
+ * nc.isInteger(4.5); // false
41
105
  *
42
- * @param {*} value - Value to check.
43
- * @returns {boolean} True if the value is an integer.
106
+ * @param {unknown} value
107
+ * @returns {value is number}
44
108
  */
45
- export function isInteger(value: any): boolean;
109
+ export function isInteger(value: unknown): value is number;
46
110
  /**
47
- * Checks if a value is a floating-point number (a finite non-integer number).
111
+ * Is it an integer JavaScript can represent exactly (up to 2^53 - 1)?
48
112
  *
49
113
  * @example
50
- * nodeComfort.isFloat(4.2); // true
51
- * nodeComfort.isFloat(4); // false
114
+ * nc.isSafeInteger(2 ** 53 - 1); // true
115
+ * nc.isSafeInteger(2 ** 53); // false
52
116
  *
53
- * @param {*} value - Value to check.
54
- * @returns {boolean} True if the value is a non-integer finite number.
117
+ * @param {unknown} value
118
+ * @returns {value is number}
55
119
  */
56
- export function isFloat(value: any): boolean;
120
+ export function isSafeInteger(value: unknown): value is number;
57
121
  /**
58
- * Checks if a value is a function.
122
+ * Is it a finite number with a decimal part?
59
123
  *
60
124
  * @example
61
- * nodeComfort.isFunction(() => {}); // true
62
- * nodeComfort.isFunction(class {}); // true
63
- * nodeComfort.isFunction("fn"); // false
125
+ * nc.isFloat(4.2); // true
126
+ * nc.isFloat(4); // false
64
127
  *
65
- * @param {*} value - Value to check.
66
- * @returns {boolean} True if the value is a function.
128
+ * @param {unknown} value
129
+ * @returns {value is number}
67
130
  */
68
- export function isFunction(value: any): boolean;
131
+ export function isFloat(value: unknown): value is number;
69
132
  /**
70
- * Checks if a value is an async function (declared with `async`).
133
+ * Is it a number greater than zero?
71
134
  *
72
135
  * @example
73
- * nodeComfort.isAsyncFunction(async () => {}); // true
74
- * nodeComfort.isAsyncFunction(() => {}); // false
136
+ * nc.isPositive(3); // true
137
+ * nc.isPositive(0); // false
75
138
  *
76
- * @param {*} value - Value to check.
77
- * @returns {boolean} True if the value is an async function.
139
+ * @param {unknown} value
140
+ * @returns {value is number}
78
141
  */
79
- export function isAsyncFunction(value: any): boolean;
142
+ export function isPositive(value: unknown): value is number;
80
143
  /**
81
- * Checks if a value is a non-null object.
144
+ * Is it a number lower than zero?
145
+ *
146
+ * @example
147
+ * nc.isNegative(-3); // true
148
+ * nc.isNegative(0); // false
82
149
  *
83
- * Note: Arrays, Maps, Sets, Dates, etc. are also considered objects here.
150
+ * @param {unknown} value
151
+ * @returns {value is number}
152
+ */
153
+ export function isNegative(value: unknown): value is number;
154
+ /**
155
+ * Is it `true` or `false`? Truthy and falsy values don't count.
84
156
  *
85
157
  * @example
86
- * nodeComfort.isObject({}); // true
87
- * nodeComfort.isObject([]); // true
88
- * nodeComfort.isObject(null); // false
89
- * nodeComfort.isObject("str"); // false
158
+ * nc.isBoolean(false); // true
159
+ * nc.isBoolean(0); // false
90
160
  *
91
- * @param {*} value - Value to check.
92
- * @returns {boolean} True if the value is a non-null object.
161
+ * @param {unknown} value
162
+ * @returns {value is boolean}
93
163
  */
94
- export function isObject(value: any): boolean;
164
+ export function isBoolean(value: unknown): value is boolean;
95
165
  /**
96
- * Checks if a value is a plain object (created via `{}` or `new Object()`).
166
+ * Is it a string?
97
167
  *
98
- * Unlike {@link isObject}, this excludes arrays, Maps, Dates, class instances, etc.
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?
99
185
  *
100
186
  * @example
101
- * nodeComfort.isPlainObject({ a: 1 }); // true
102
- * nodeComfort.isPlainObject([]); // false
103
- * nodeComfort.isPlainObject(new Date()); // false
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`?
104
203
  *
105
- * @param {*} value - Value to check.
106
- * @returns {boolean} True if the value is a plain object.
204
+ * @param {unknown} value
205
+ * @returns {value is null}
107
206
  */
108
- export function isPlainObject(value: any): boolean;
207
+ export function isNull(value: unknown): value is null;
109
208
  /**
110
- * Checks if a value is a boolean.
209
+ * Is it `null` or `undefined`?
111
210
  *
112
211
  * @example
113
- * nodeComfort.isBoolean(true); // true
114
- * nodeComfort.isBoolean(false); // true
115
- * nodeComfort.isBoolean(0); // false
212
+ * nc.isNil(undefined); // true
213
+ * nc.isNil(0); // false
116
214
  *
117
- * @param {*} value - Value to check.
118
- * @returns {boolean} True if the value is a boolean.
215
+ * @param {unknown} value
216
+ * @returns {value is null | undefined}
119
217
  */
120
- export function isBoolean(value: any): boolean;
218
+ export function isNil(value: unknown): value is null | undefined;
121
219
  /**
122
- * Checks if a value is a string.
220
+ * Is it anything but `null` or `undefined`? Pass it to `filter()` to drop
221
+ * missing values and keep the right type.
123
222
  *
124
223
  * @example
125
- * nodeComfort.isString("hello"); // true
126
- * nodeComfort.isString(123); // false
224
+ * const ids = [1, null, 2, undefined].filter(nc.isDefined); // number[]
127
225
  *
128
- * @param {*} value - Value to check.
129
- * @returns {boolean} True if the value is a string.
226
+ * @template T
227
+ * @param {T} value
228
+ * @returns {value is NonNullable<T>}
130
229
  */
131
- export function isString(value: any): boolean;
230
+ export function isDefined<T>(value: T): value is NonNullable<T>;
132
231
  /**
133
- * Checks if a value is a symbol.
232
+ * Is it a primitive (string, number, boolean, symbol, bigint, `null` or `undefined`)?
233
+ *
234
+ * @param {unknown} value
235
+ * @returns {value is Primitive}
236
+ */
237
+ export function isPrimitive(value: unknown): value is Primitive;
238
+ /**
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.
134
248
  *
135
249
  * @example
136
- * nodeComfort.isSymbol(Symbol("x")); // true
250
+ * nc.isAsyncFunction(async () => {}); // true
251
+ * nc.isAsyncFunction(() => Promise.resolve()); // false
252
+ *
253
+ * @param {unknown} value
254
+ * @returns {value is (...args: any[]) => Promise<any>}
255
+ */
256
+ export function isAsyncFunction(value: unknown): value is (...args: any[]) => Promise<any>;
257
+ /**
258
+ * Is it a generator function (`function*`)?
259
+ *
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*`?
137
266
  *
138
- * @param {*} value - Value to check.
139
- * @returns {boolean} True if the value is a symbol.
267
+ * @param {unknown} value
268
+ * @returns {value is Generator}
140
269
  */
141
- export function isSymbol(value: any): boolean;
270
+ export function isGenerator(value: unknown): value is Generator;
142
271
  /**
143
- * Checks if a value is a BigInt.
272
+ * Is it a `class` rather than a plain function?
144
273
  *
145
274
  * @example
146
- * nodeComfort.isBigInt(10n); // true
147
- * nodeComfort.isBigInt(10); // false
275
+ * nc.isClass(class User {}); // true
276
+ * nc.isClass(function () {}); // false
148
277
  *
149
- * @param {*} value - Value to check.
150
- * @returns {boolean} True if the value is a BigInt.
278
+ * @param {unknown} value
279
+ * @returns {value is new (...args: any[]) => any}
151
280
  */
152
- export function isBigInt(value: any): boolean;
281
+ export function isClass(value: unknown): value is new (...args: any[]) => any;
153
282
  /**
154
- * Checks if a value is exactly undefined.
283
+ * Is it an object, and not `null`? Arrays, dates and class instances count;
284
+ * use `isPlainObject` for `{}` literals only.
285
+ *
286
+ * @param {unknown} value
287
+ * @returns {value is object}
288
+ */
289
+ export function isObject(value: unknown): value is object;
290
+ /**
291
+ * Is it a plain object, made with `{}`, `new Object()` or
292
+ * `Object.create(null)`? Arrays, dates, maps and class instances are rejected.
155
293
  *
156
294
  * @example
157
- * nodeComfort.isUndefined(undefined); // true
158
- * nodeComfort.isUndefined(null); // 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)?
159
304
  *
160
- * @param {*} value - Value to check.
161
- * @returns {boolean} True if the value is undefined.
305
+ * @param {unknown} value
306
+ * @returns {value is PromiseLike<any>}
162
307
  */
163
- export function isUndefined(value: any): boolean;
308
+ export function isPromise(value: unknown): value is PromiseLike<any>;
164
309
  /**
165
- * Checks if a value is exactly null.
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`.
318
+ *
319
+ * @param {unknown} value
320
+ * @returns {value is Date}
321
+ */
322
+ export function isDate(value: unknown): value is Date;
323
+ /**
324
+ * Is it a `Date` holding a real point in time?
166
325
  *
167
326
  * @example
168
- * nodeComfort.isNull(null); // true
169
- * nodeComfort.isNull(undefined); // 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.
170
364
  *
171
- * @param {*} value - Value to check.
172
- * @returns {boolean} True if the value is null.
365
+ * @param {unknown} value
366
+ * @returns {value is Iterable<unknown>}
173
367
  */
174
- export function isNull(value: any): boolean;
368
+ export function isIterable(value: unknown): value is Iterable<unknown>;
175
369
  /**
176
- * Checks if a value is `null` or `undefined`.
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?
392
+ *
393
+ * @param {unknown} value
394
+ * @returns {value is Error}
395
+ */
396
+ export function isError(value: unknown): value is Error;
397
+ /**
398
+ * Is it empty? `null`, `undefined`, `""`, `[]`, `{}` and empty maps and sets
399
+ * are. Numbers, booleans and functions never are.
177
400
  *
178
401
  * @example
179
- * nodeComfort.isNil(null); // true
180
- * nodeComfort.isNil(undefined); // true
181
- * nodeComfort.isNil(0); // false
402
+ * nc.isEmpty({}); // true
403
+ * nc.isEmpty(0); // false
404
+ * nc.isEmpty(" "); // false, see isBlank
182
405
  *
183
- * @param {*} value - Value to check.
184
- * @returns {boolean} True if the value is null or undefined.
406
+ * @param {unknown} value
407
+ * @returns {boolean}
185
408
  */
186
- export function isNil(value: any): boolean;
409
+ export function isEmpty(value: unknown): boolean;
187
410
  /**
188
- * Checks if a value is a primitive (string, number, boolean, symbol, bigint, null or undefined).
411
+ * Is it `null`, `undefined`, or a string with nothing but whitespace?
189
412
  *
190
413
  * @example
191
- * nodeComfort.isPrimitive(42); // true
192
- * nodeComfort.isPrimitive({}); // false
414
+ * nc.isBlank(" \n"); // true
415
+ * nc.isBlank(" a "); // false
193
416
  *
194
- * @param {*} value - Value to check.
195
- * @returns {boolean} True if the value is a primitive.
417
+ * @param {unknown} value
418
+ * @returns {boolean}
196
419
  */
197
- export function isPrimitive(value: any): boolean;
420
+ export function isBlank(value: unknown): boolean;
198
421
  /**
199
- * Checks if a value is a Promise (or a thenable object).
422
+ * Is it an array where every item passes `guard`?
200
423
  *
201
424
  * @example
202
- * nodeComfort.isPromise(Promise.resolve()); // true
203
- * nodeComfort.isPromise({ then() {} }); // true
204
- * nodeComfort.isPromise(42); // false
425
+ * if (nc.isArrayOf(input, nc.isString)) input.join(", "); // input: string[]
205
426
  *
206
- * @param {*} value - Value to check.
207
- * @returns {boolean} True if the value is a Promise or thenable.
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[]}
208
431
  */
209
- export function isPromise(value: any): boolean;
432
+ export function isArrayOf<T>(value: unknown, guard: (item: unknown) => item is T): value is T[];
210
433
  /**
211
- * Checks if a value is a RegExp instance.
434
+ * Is it one of the allowed values? With a constant list, the value gets the
435
+ * matching literal type.
212
436
  *
213
437
  * @example
214
- * nodeComfort.isRegExp(/a/); // true
215
- * nodeComfort.isRegExp("a"); // false
438
+ * const ROLES = ["admin", "user"] as const;
439
+ * if (nc.isOneOf(input, ROLES)) input; // "admin" | "user"
216
440
  *
217
- * @param {*} value - Value to check.
218
- * @returns {boolean} True if the value is a RegExp.
441
+ * @template {readonly unknown[]} L
442
+ * @param {unknown} value
443
+ * @param {L} allowed
444
+ * @returns {value is L[number]}
219
445
  */
220
- export function isRegExp(value: any): boolean;
446
+ export function isOneOf<L extends readonly unknown[]>(value: unknown, allowed: L): value is L[number];
221
447
  /**
222
- * Checks if a value is a built-in Date instance.
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
223
456
  *
224
- * Uses `util.types.isDate` under the hood to handle cross-realm Dates.
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)?
225
463
  *
226
464
  * @example
227
- * nodeComfort.isDate(new Date()); // true
228
- * nodeComfort.isDate("2024-01-01"); // false
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
229
468
  *
230
- * @param {*} value - Value to check.
231
- * @returns {boolean} True if the value is a Date instance.
469
+ * @param {unknown} value
470
+ * @param {IsURLOptions} [options]
471
+ * @returns {value is string}
232
472
  */
233
- export function isDate(value: any): boolean;
473
+ export function isURL(value: unknown, options?: IsURLOptions): value is string;
234
474
  /**
235
- * Checks if a value is a **valid** Date (a Date instance that is not `Invalid Date`).
475
+ * Is it a UUID?
236
476
  *
237
477
  * @example
238
- * nodeComfort.isValidDate(new Date()); // true
239
- * nodeComfort.isValidDate(new Date("nope")); // false
478
+ * nc.isUUID(nc.id.uuid()); // true
479
+ * nc.isUUID(id, { version: 7 }); // only v7
240
480
  *
241
- * @param {*} value - Value to check.
242
- * @returns {boolean} True if the value is a valid Date.
481
+ * @param {unknown} value
482
+ * @param {IsUUIDOptions} [options]
483
+ * @returns {value is string}
243
484
  */
244
- export function isValidDate(value: any): boolean;
485
+ export function isUUID(value: unknown, options?: IsUUIDOptions): value is string;
245
486
  /**
246
- * Checks if a value is a built-in Map instance.
487
+ * Is it a string of valid JSON?
247
488
  *
248
489
  * @example
249
- * nodeComfort.isMap(new Map()); // true
250
- * nodeComfort.isMap({}); // false
490
+ * nc.isJSON('{"a":1}'); // true
491
+ * nc.isJSON("{a:1}"); // false
251
492
  *
252
- * @param {*} value - Value to check.
253
- * @returns {boolean} True if the value is a Map.
493
+ * @param {unknown} value
494
+ * @returns {value is string}
254
495
  */
255
- export function isMap(value: any): boolean;
496
+ export function isJSON(value: unknown): value is string;
256
497
  /**
257
- * Checks if a value is a built-in Set instance.
498
+ * Is it a finite number, or a string that is exactly one (spaces around are fine)?
258
499
  *
259
500
  * @example
260
- * nodeComfort.isSet(new Set()); // true
261
- * nodeComfort.isSet([]); // false
501
+ * nc.isNumeric("4.2e3"); // true
502
+ * nc.isNumeric(" 12 "); // true
503
+ * nc.isNumeric("12px"); // false
262
504
  *
263
- * @param {*} value - Value to check.
264
- * @returns {boolean} True if the value is a Set.
505
+ * @param {unknown} value
506
+ * @returns {value is number | string}
265
507
  */
266
- export function isSet(value: any): boolean;
508
+ export function isNumeric(value: unknown): value is number | string;
267
509
  /**
268
- * Checks if a value is a WeakMap instance.
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
269
516
  *
270
- * @param {*} value - Value to check.
271
- * @returns {boolean} True if the value is a WeakMap.
517
+ * @param {unknown} value
518
+ * @param {4|6} [version] Only accept IPv4 or IPv6.
519
+ * @returns {value is string}
272
520
  */
273
- export function isWeakMap(value: any): boolean;
521
+ export function isIP(value: unknown, version?: 4 | 6): value is string;
274
522
  /**
275
- * Checks if a value is a WeakSet instance.
523
+ * Is it an IPv4 address, like `192.168.0.1`?
276
524
  *
277
- * @param {*} value - Value to check.
278
- * @returns {boolean} True if the value is a WeakSet.
525
+ * @param {unknown} value
526
+ * @returns {value is string}
279
527
  */
280
- export function isWeakSet(value: any): boolean;
528
+ export function isIPv4(value: unknown): value is string;
281
529
  /**
282
- * Checks if a value is an iterable (has a `Symbol.iterator`).
530
+ * Is it an IPv6 address, like `::1`?
283
531
  *
284
- * Strings, arrays, Maps, Sets and generators are iterable.
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?
285
538
  *
286
539
  * @example
287
- * nodeComfort.isIterable([1, 2]); // true
288
- * nodeComfort.isIterable("hi"); // true
289
- * nodeComfort.isIterable({}); // false
540
+ * nc.isPort("8080"); // true
541
+ * nc.isPort(70000); // false
290
542
  *
291
- * @param {*} value - Value to check.
292
- * @returns {boolean} True if the value is iterable.
543
+ * @param {unknown} value
544
+ * @returns {boolean}
293
545
  */
294
- export function isIterable(value: any): boolean;
546
+ export function isPort(value: unknown): boolean;
295
547
  /**
296
- * Checks if a value is a Node.js Buffer.
548
+ * Is it a hexadecimal string? A `0x` prefix is allowed.
297
549
  *
298
- * @param {*} value - Value to check.
299
- * @returns {boolean} True if the value is a Buffer.
550
+ * @param {unknown} value
551
+ * @returns {value is string}
300
552
  */
301
- export function isBuffer(value: any): boolean;
553
+ export function isHex(value: unknown): value is string;
302
554
  /**
303
- * Checks if a value is any TypedArray (Uint8Array, Float64Array, etc.).
555
+ * Is it a CSS hex color (`#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`)?
304
556
  *
305
- * @param {*} value - Value to check.
306
- * @returns {boolean} True if the value is a TypedArray.
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}
307
563
  */
308
- export function isTypedArray(value: any): boolean;
564
+ export function isHexColor(value: unknown): value is string;
309
565
  /**
310
- * Checks if a value is a native Error instance.
311
- *
312
- * Uses `util.types.isNativeError` to correctly handle errors from other realms.
566
+ * Is it valid Base64?
313
567
  *
314
568
  * @example
315
- * nodeComfort.isError(new Error("x")); // true
316
- * nodeComfort.isError(new TypeError("x")); // true
317
- * nodeComfort.isError({ message: "x" }); // false
569
+ * nc.isBase64("aGVsbG8="); // true
570
+ * nc.isBase64("aGVsbG8", { urlSafe: true }); // true
318
571
  *
319
- * @param {*} value - Value to check.
320
- * @returns {boolean} True if the value is a native Error.
572
+ * @param {unknown} value
573
+ * @param {IsBase64Options} [options]
574
+ * @returns {value is string}
321
575
  */
322
- export function isError(value: any): boolean;
576
+ export function isBase64(value: unknown, options?: IsBase64Options): value is string;
323
577
  /**
324
- * Checks if a value is "empty".
325
- *
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
578
+ * Is it a semantic version? A leading `v` is fine.
332
579
  *
333
580
  * @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
581
+ * nc.isSemver("v2.0.0-rc.1"); // true
582
+ * nc.isSemver("1.2"); // false
340
583
  *
341
- * @param {*} value - Value to check.
342
- * @returns {boolean} True if the value is considered empty.
584
+ * @param {unknown} value
585
+ * @returns {value is string}
343
586
  */
344
- export function isEmpty(value: any): boolean;
587
+ export function isSemver(value: unknown): value is string;
345
588
  /**
346
- * Checks if a string is a valid, well-formed email address.
347
- *
348
- * This is a pragmatic check (not a full RFC 5322 parser) that covers the
349
- * vast majority of real-world addresses.
589
+ * Is it an ISO 8601 date or date-time, and a real calendar date?
350
590
  *
351
591
  * @example
352
- * nodeComfort.isEmail("john@example.com"); // true
353
- * nodeComfort.isEmail("nope"); // false
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.
354
601
  *
355
- * @param {*} value - Value to check.
356
- * @returns {boolean} True if the value looks like an email address.
602
+ * @param {unknown} value
603
+ * @returns {value is string}
357
604
  */
358
- export function isEmail(value: any): boolean;
605
+ export function isSlug(value: unknown): value is string;
359
606
  /**
360
- * Checks if a string is a valid HTTP(S) URL.
607
+ * Is it made only of letters, in any alphabet?
361
608
  *
362
609
  * @example
363
- * nodeComfort.isURL("https://example.com"); // true
364
- * nodeComfort.isURL("ftp://example.com"); // false (not http/https)
610
+ * nc.isAlpha("Élodie"); // true
611
+ * nc.isAlpha("abc1"); // false
365
612
  *
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.
613
+ * @param {unknown} value
614
+ * @returns {value is string}
369
615
  */
370
- export function isURL(value: any, options?: {
371
- protocols?: string[];
372
- }): boolean;
616
+ export function isAlpha(value: unknown): value is string;
373
617
  /**
374
- * Checks if a string is a valid UUID (versions 1-5, any variant).
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.
375
627
  *
376
628
  * @example
377
- * nodeComfort.isUUID("123e4567-e89b-12d3-a456-426614174000"); // true
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.
378
638
  *
379
- * @param {*} value - Value to check.
380
- * @returns {boolean} True if the value is a UUID.
639
+ * @param {unknown} value
640
+ * @returns {value is string}
381
641
  */
382
- export function isUUID(value: any): boolean;
642
+ export function isJWT(value: unknown): value is string;
383
643
  /**
384
- * Checks if a string is valid, parseable JSON.
644
+ * Throws an `AssertionError` if `condition` is falsy. Afterwards, TypeScript
645
+ * knows the condition holds.
385
646
  *
386
647
  * @example
387
- * nodeComfort.isJSON('{"a":1}'); // true
388
- * nodeComfort.isJSON("{a:1}"); // false
648
+ * const user = users.find((u) => u.id === id);
649
+ * nc.assert(user, `User ${id} not found`);
650
+ * user.name; // no "possibly undefined" here
389
651
  *
390
- * @param {*} value - Value to check.
391
- * @returns {boolean} True if the value is a JSON-parseable string.
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}
392
656
  */
393
- export function isJSON(value: any): boolean;
657
+ export function assert(condition: unknown, message?: string | (() => string)): asserts condition;
394
658
  /**
395
- * Checks if a value is numeric • a number, or a string that fully represents one.
659
+ * Throws an `AssertionError` unless `value` passes `guard`. Afterwards,
660
+ * `value` has the guarded type.
396
661
  *
397
662
  * @example
398
- * nodeComfort.isNumeric(42); // true
399
- * nodeComfort.isNumeric("42"); // true
400
- * nodeComfort.isNumeric("4.2e3"); // true
401
- * nodeComfort.isNumeric("4px"); // false
663
+ * nc.assertType(config.port, nc.isInteger, "port must be an integer");
402
664
  *
403
- * @param {*} value - Value to check.
404
- * @returns {boolean} True if the value is numeric.
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}
405
671
  */
406
- export function isNumeric(value: any): boolean;
672
+ export function assertType<T>(value: unknown, guard: (value: unknown) => value is T, message?: string | (() => string)): asserts value is T;
407
673
  //# sourceMappingURL=Checker.d.ts.map