@ix-xs/node-comfort 1.1.31 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +623 -748
- package/index.js +39 -13
- package/package.json +74 -21
- package/src/Arr.js +433 -0
- package/src/Checker.js +547 -188
- package/src/FS.js +127 -1
- package/src/Func.js +375 -0
- package/src/Id.js +235 -0
- package/src/Logger.js +71 -0
- package/src/Num.js +366 -0
- package/src/Obj.js +421 -0
- package/src/SQLite.js +20 -9
- package/src/Str.js +452 -0
- package/src/Time.js +485 -0
- package/types/index.d.ts +406 -0
- package/types/index.d.ts.map +1 -1
- package/types/src/Arr.d.ts +252 -0
- package/types/src/Arr.d.ts.map +1 -0
- package/types/src/Checker.d.ts +292 -30
- package/types/src/Checker.d.ts.map +1 -1
- package/types/src/FS.d.ts +60 -0
- package/types/src/FS.d.ts.map +1 -1
- package/types/src/Func.d.ts +210 -0
- package/types/src/Func.d.ts.map +1 -0
- package/types/src/Id.d.ts +119 -0
- package/types/src/Id.d.ts.map +1 -0
- package/types/src/Logger.d.ts +33 -0
- package/types/src/Logger.d.ts.map +1 -1
- package/types/src/Num.d.ts +242 -0
- package/types/src/Num.d.ts.map +1 -0
- package/types/src/Obj.d.ts +189 -0
- package/types/src/Obj.d.ts.map +1 -0
- package/types/src/SQLite.d.ts +6 -1
- package/types/src/SQLite.d.ts.map +1 -1
- package/types/src/Str.d.ts +293 -0
- package/types/src/Str.d.ts.map +1 -0
- package/types/src/Time.d.ts +205 -0
- package/types/src/Time.d.ts.map +1 -0
- package/tsconfig.json +0 -18
package/types/src/FS.d.ts
CHANGED
|
@@ -217,6 +217,66 @@ export function moveFile(options: {
|
|
|
217
217
|
* @returns {string|undefined} File content, or undefined if the file cannot be read.
|
|
218
218
|
*/
|
|
219
219
|
export function readFile(path: string): string | undefined;
|
|
220
|
+
/**
|
|
221
|
+
* Reads and parses a JSON file.
|
|
222
|
+
* @template T
|
|
223
|
+
* @param {string} path - The JSON file path.
|
|
224
|
+
* @param {T} [fallback] - Value returned when the file is missing or invalid.
|
|
225
|
+
* @returns {T|any|undefined} The parsed value, or the fallback.
|
|
226
|
+
*/
|
|
227
|
+
export function readJSON<T>(path: string, fallback?: T): T | any | undefined;
|
|
228
|
+
/**
|
|
229
|
+
* Serializes a value to JSON and writes it to a file (creating folders as needed).
|
|
230
|
+
* @param {string} path - The destination file path.
|
|
231
|
+
* @param {*} data - The value to serialize.
|
|
232
|
+
* @param {{ spaces?: number, force?: boolean }} [options] - Formatting options (`spaces` default 2).
|
|
233
|
+
* @returns {boolean} True on success, false on failure.
|
|
234
|
+
*/
|
|
235
|
+
export function writeJSON(path: string, data: any, options?: {
|
|
236
|
+
spaces?: number;
|
|
237
|
+
force?: boolean;
|
|
238
|
+
}): boolean;
|
|
239
|
+
/**
|
|
240
|
+
* Appends text to a file, creating it (and its folders) if necessary.
|
|
241
|
+
* @param {string} path - The file path.
|
|
242
|
+
* @param {string} data - The text to append.
|
|
243
|
+
* @returns {boolean} True on success, false on failure.
|
|
244
|
+
*/
|
|
245
|
+
export function appendFile(path: string, data: string): boolean;
|
|
246
|
+
/**
|
|
247
|
+
* Checks whether a path (file or folder) exists.
|
|
248
|
+
* @param {string} path - The path to check.
|
|
249
|
+
* @returns {boolean} True if the path exists.
|
|
250
|
+
*/
|
|
251
|
+
export function exists(path: string): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Returns filesystem stats for a path, or undefined if it does not exist.
|
|
254
|
+
* @param {string} path - The path to stat.
|
|
255
|
+
* @returns {import("node:fs").Stats|undefined} The stats object, or undefined.
|
|
256
|
+
*/
|
|
257
|
+
export function stat(path: string): any | undefined;
|
|
258
|
+
/**
|
|
259
|
+
* Returns the size of a file in bytes.
|
|
260
|
+
* @param {string} path - The file path.
|
|
261
|
+
* @returns {number|undefined} Size in bytes, or undefined if not found.
|
|
262
|
+
*/
|
|
263
|
+
export function fileSize(path: string): number | undefined;
|
|
264
|
+
/**
|
|
265
|
+
* Computes a hash digest of a file's contents.
|
|
266
|
+
* @param {string} path - The file path.
|
|
267
|
+
* @param {{ algorithm?: string, encoding?: "hex"|"base64"|"base64url" }} [options] - Hash options.
|
|
268
|
+
* @returns {string|undefined} The digest, or undefined if the file cannot be read.
|
|
269
|
+
*/
|
|
270
|
+
export function hashFile(path: string, options?: {
|
|
271
|
+
algorithm?: string;
|
|
272
|
+
encoding?: "hex" | "base64" | "base64url";
|
|
273
|
+
}): string | undefined;
|
|
274
|
+
/**
|
|
275
|
+
* Empties a folder's contents while keeping the folder itself.
|
|
276
|
+
* @param {string} path - The folder to empty.
|
|
277
|
+
* @returns {boolean|undefined} True on success, false on failure, undefined if not found.
|
|
278
|
+
*/
|
|
279
|
+
export function emptyFolder(path: string): boolean | undefined;
|
|
220
280
|
/**
|
|
221
281
|
* Watches a path and exposes event listeners.
|
|
222
282
|
* @param {Object} [options] - Watch options.
|
package/types/src/FS.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FS.d.ts","sourceRoot":"","sources":["../../src/FS.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"FS.d.ts","sourceRoot":"","sources":["../../src/FS.js"],"names":[],"mappings":"AAoKE;;;;;GAKG;AACH,8BAJW,MAAM,gBACN,MAAM,GACJ,MAAM,GAAC,SAAS,CAI5B;AAED;;;;GAIG;AACH,kCAHW,MAAM,GACJ,MAAM,CAWlB;AAED;;;;;GAKG;AACH,oCAJW,MAAM,cACN,OAAO,GACL,MAAM,EAAE,GAAC,SAAS,CAK9B;AAED;;;;GAIG;AACH,iCAHW,MAAM,GACJ,MAAM,GAAC,SAAS,CAI5B;AAED;;;;;GAKG;AACH,kCAJW,MAAM,cACN,OAAO,GACL,MAAM,EAAE,GAAC,SAAS,CAK9B;AAED;;;;GAIG;AACH,8BAHW,MAAM,GACJ,MAAM,GAAC,SAAS,CAI5B;AAED;;;;;GAKG;AACH,mCAJW,MAAM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W5B;AAED;;;;GAIG;AACH,kCAHW,MAAM,GACJ,OAAO,GAAC,SAAS,CAa7B;AAED;;;;;;;;;;;;GAYG;AACH,gCAVG;IAAyB,IAAI;IACH,SAAS;IACmC,MAAM,YAA5D,QAAQ,GAAC,QAAQ,QAAQ,MAAM,KAAK,OAAO;CAC3D,GAAU;IACR,EAAE,EAAE,CAAC,CAAC,SAAS,QAAQ,GAAC,QAAQ,GAAC,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,SAAS,KAAK,GAAG,CAAC,KAAK,EAAE,QAAQ,GAAC,QAAQ,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,GAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,GAAC,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC;IAC5L,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,KAAK,EAAE,MAAM,GAAG,CAAC;IACjB,MAAM,EAAE,MAAM,GAAG,CAAC;CACnB,GAAC,SAAS,CAkGb"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a debounced version of a function that delays invocation until
|
|
3
|
+
* `wait` ms have elapsed since the last call.
|
|
4
|
+
*
|
|
5
|
+
* The returned function exposes `.cancel()` and `.flush()`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const onResize = nodeComfort.func.debounce(() => render(), 200);
|
|
9
|
+
* window.on("resize", onResize);
|
|
10
|
+
* onResize.cancel(); // drop any pending call
|
|
11
|
+
*
|
|
12
|
+
* @template {(...args: any[]) => any} F
|
|
13
|
+
* @param {F} fn - Function to debounce.
|
|
14
|
+
* @param {number} wait - Delay in milliseconds.
|
|
15
|
+
* @param {{ leading?: boolean }} [options] - Set `leading` to fire on the first call.
|
|
16
|
+
* @returns {F & { cancel: () => void, flush: () => void }} The debounced function.
|
|
17
|
+
*/
|
|
18
|
+
export function debounce<F extends (...args: any[]) => any>(fn: F, wait: number, options?: {
|
|
19
|
+
leading?: boolean;
|
|
20
|
+
}): F & {
|
|
21
|
+
cancel: () => void;
|
|
22
|
+
flush: () => void;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Creates a throttled version of a function that runs at most once per
|
|
26
|
+
* `wait` ms.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const onScroll = nodeComfort.func.throttle(() => update(), 100);
|
|
30
|
+
*
|
|
31
|
+
* @template {(...args: any[]) => any} F
|
|
32
|
+
* @param {F} fn - Function to throttle.
|
|
33
|
+
* @param {number} wait - Minimum interval in milliseconds.
|
|
34
|
+
* @param {{ trailing?: boolean }} [options] - Set `trailing` (default true) to run a final call.
|
|
35
|
+
* @returns {F & { cancel: () => void }} The throttled function.
|
|
36
|
+
*/
|
|
37
|
+
export function throttle<F extends (...args: any[]) => any>(fn: F, wait: number, options?: {
|
|
38
|
+
trailing?: boolean;
|
|
39
|
+
}): F & {
|
|
40
|
+
cancel: () => void;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Wraps a function so it can only ever run once; subsequent calls return
|
|
44
|
+
* the first result.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const init = nodeComfort.func.once(() => expensiveSetup());
|
|
48
|
+
*
|
|
49
|
+
* @template {(...args: any[]) => any} F
|
|
50
|
+
* @param {F} fn - Function to wrap.
|
|
51
|
+
* @returns {F} The single-call function.
|
|
52
|
+
*/
|
|
53
|
+
export function once<F extends (...args: any[]) => any>(fn: F): F;
|
|
54
|
+
/**
|
|
55
|
+
* Memoizes a function, caching results by argument key.
|
|
56
|
+
*
|
|
57
|
+
* By default the cache key is a JSON serialization of the arguments; pass a
|
|
58
|
+
* `resolver` for custom keys.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const fib = nodeComfort.func.memoize((n) => (n < 2 ? n : fib(n - 1) + fib(n - 2)));
|
|
62
|
+
*
|
|
63
|
+
* @template {(...args: any[]) => any} F
|
|
64
|
+
* @param {F} fn - Function to memoize.
|
|
65
|
+
* @param {(...args: any[]) => any} [resolver] - Cache-key resolver.
|
|
66
|
+
* @returns {F & { cache: Map<any, any> }} The memoized function.
|
|
67
|
+
*/
|
|
68
|
+
export function memoize<F extends (...args: any[]) => any>(fn: F, resolver?: (...args: any[]) => any): F & {
|
|
69
|
+
cache: Map<any, any>;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Retries an async function until it succeeds or the attempt budget runs out.
|
|
73
|
+
*
|
|
74
|
+
* Supports fixed or exponential backoff and a `shouldRetry` guard.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* const data = await nodeComfort.func.retry(() => fetch(url), {
|
|
78
|
+
* attempts: 5,
|
|
79
|
+
* delay: 200,
|
|
80
|
+
* backoff: 2, // 200, 400, 800, ...
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* @template T
|
|
84
|
+
* @param {() => T|Promise<T>} fn - Function to run.
|
|
85
|
+
* @param {{
|
|
86
|
+
* attempts?: number,
|
|
87
|
+
* delay?: number,
|
|
88
|
+
* backoff?: number,
|
|
89
|
+
* maxDelay?: number,
|
|
90
|
+
* shouldRetry?: (error: unknown, attempt: number) => boolean,
|
|
91
|
+
* onRetry?: (error: unknown, attempt: number) => void
|
|
92
|
+
* }} [options] - Retry options.
|
|
93
|
+
* @returns {Promise<T>} The resolved value.
|
|
94
|
+
* @throws Re-throws the last error if all attempts fail.
|
|
95
|
+
*/
|
|
96
|
+
export function retry<T>(fn: () => T | Promise<T>, options?: {
|
|
97
|
+
attempts?: number;
|
|
98
|
+
delay?: number;
|
|
99
|
+
backoff?: number;
|
|
100
|
+
maxDelay?: number;
|
|
101
|
+
shouldRetry?: (error: unknown, attempt: number) => boolean;
|
|
102
|
+
onRetry?: (error: unknown, attempt: number) => void;
|
|
103
|
+
}): Promise<T>;
|
|
104
|
+
/**
|
|
105
|
+
* Runs a promise (or async function) with a timeout, rejecting if it takes
|
|
106
|
+
* too long.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* await nodeComfort.func.timeout(fetch(url), 5000, "fetch too slow");
|
|
110
|
+
*
|
|
111
|
+
* @template T
|
|
112
|
+
* @param {Promise<T>|(() => Promise<T>)} promise - Promise or async function.
|
|
113
|
+
* @param {number} ms - Timeout in milliseconds.
|
|
114
|
+
* @param {string} [message="Operation timed out"] - Error message on timeout.
|
|
115
|
+
* @returns {Promise<T>} The resolved value.
|
|
116
|
+
* @throws {Error} If the timeout elapses first.
|
|
117
|
+
*/
|
|
118
|
+
export function timeout<T>(promise: Promise<T> | (() => Promise<T>), ms: number, message?: string): Promise<T>;
|
|
119
|
+
/**
|
|
120
|
+
* Composes functions left-to-right: `pipe(f, g)(x)` is `g(f(x))`.
|
|
121
|
+
*
|
|
122
|
+
* The first function may take multiple arguments; the rest take one.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* const process = nodeComfort.func.pipe(
|
|
126
|
+
* (s) => s.trim(),
|
|
127
|
+
* (s) => s.toUpperCase(),
|
|
128
|
+
* );
|
|
129
|
+
* process(" hi "); // "HI"
|
|
130
|
+
*
|
|
131
|
+
* @param {...((arg: any) => any)} fns - Functions to compose.
|
|
132
|
+
* @returns {(...args: any[]) => any} The composed function.
|
|
133
|
+
*/
|
|
134
|
+
export function pipe(...fns: ((arg: any) => any)[]): (...args: any[]) => any;
|
|
135
|
+
/**
|
|
136
|
+
* Composes functions right-to-left: `compose(f, g)(x)` is `f(g(x))`.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* const process = nodeComfort.func.compose(
|
|
140
|
+
* (s) => s.toUpperCase(),
|
|
141
|
+
* (s) => s.trim(),
|
|
142
|
+
* );
|
|
143
|
+
* process(" hi "); // "HI"
|
|
144
|
+
*
|
|
145
|
+
* @param {...((arg: any) => any)} fns - Functions to compose.
|
|
146
|
+
* @returns {(...args: any[]) => any} The composed function.
|
|
147
|
+
*/
|
|
148
|
+
export function compose(...fns: ((arg: any) => any)[]): (...args: any[]) => any;
|
|
149
|
+
/**
|
|
150
|
+
* Curries a function so it can be called with arguments one (or a few) at a time.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* const add = nodeComfort.func.curry((a, b, c) => a + b + c);
|
|
154
|
+
* add(1)(2)(3); // 6
|
|
155
|
+
* add(1, 2)(3); // 6
|
|
156
|
+
*
|
|
157
|
+
* @param {(...args: any[]) => any} fn - Function to curry.
|
|
158
|
+
* @param {number} [arity=fn.length] - Number of arguments to wait for.
|
|
159
|
+
* @returns {(...args: any[]) => any} The curried function.
|
|
160
|
+
*/
|
|
161
|
+
export function curry(fn: (...args: any[]) => any, arity?: number): (...args: any[]) => any;
|
|
162
|
+
/**
|
|
163
|
+
* Returns a function that negates the result of `predicate`.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* const isOdd = nodeComfort.func.negate((n) => n % 2 === 0);
|
|
167
|
+
*
|
|
168
|
+
* @param {(...args: any[]) => boolean} predicate - Predicate to negate.
|
|
169
|
+
* @returns {(...args: any[]) => boolean} The negated predicate.
|
|
170
|
+
*/
|
|
171
|
+
export function negate(predicate: (...args: any[]) => boolean): (...args: any[]) => boolean;
|
|
172
|
+
/**
|
|
173
|
+
* Wraps a function so that it is only actually invoked from the `n`-th call on.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* const save = nodeComfort.func.after(2, () => persist());
|
|
177
|
+
* save(); save(); // persists on the 2nd call
|
|
178
|
+
*
|
|
179
|
+
* @template {(...args: any[]) => any} F
|
|
180
|
+
* @param {number} n - Number of calls before `fn` fires.
|
|
181
|
+
* @param {F} fn - Function to wrap.
|
|
182
|
+
* @returns {F} The wrapped function.
|
|
183
|
+
*/
|
|
184
|
+
export function after<F extends (...args: any[]) => any>(n: number, fn: F): F;
|
|
185
|
+
/**
|
|
186
|
+
* Turns a Node-style callback function (`(err, result) => ...`) into one that
|
|
187
|
+
* returns a Promise.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* const readFile = nodeComfort.func.promisify(fs.readFile);
|
|
191
|
+
* const data = await readFile("a.txt", "utf8");
|
|
192
|
+
*
|
|
193
|
+
* @param {(...args: any[]) => void} fn - Callback-style function.
|
|
194
|
+
* @returns {(...args: any[]) => Promise<any>} A promise-returning function.
|
|
195
|
+
*/
|
|
196
|
+
export function promisify(fn: (...args: any[]) => void): (...args: any[]) => Promise<any>;
|
|
197
|
+
/**
|
|
198
|
+
* Wraps an async function so it resolves to a `[error, result]` tuple instead
|
|
199
|
+
* of throwing • Go-style error handling.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* const [err, user] = await nodeComfort.func.attempt(() => getUser(id))();
|
|
203
|
+
* if (err) return handle(err);
|
|
204
|
+
*
|
|
205
|
+
* @template T
|
|
206
|
+
* @param {(...args: any[]) => Promise<T>|T} fn - Function to wrap.
|
|
207
|
+
* @returns {(...args: any[]) => Promise<[unknown, T|undefined]>} The safe function.
|
|
208
|
+
*/
|
|
209
|
+
export function attempt<T>(fn: (...args: any[]) => Promise<T> | T): (...args: any[]) => Promise<[unknown, T | undefined]>;
|
|
210
|
+
//# sourceMappingURL=Func.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Func.d.ts","sourceRoot":"","sources":["../../src/Func.js"],"names":[],"mappings":"AAiBE;;;;;;;;;;;;;;;;GAgBG;AACH,yBANuC,CAAC,SAA3B,CAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAI,MAC3B,CAAC,QACD,MAAM,YACN;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,GACnB,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,IAAI,CAAA;CAAE,CAoCzD;AAED;;;;;;;;;;;;GAYG;AACH,yBANuC,CAAC,SAA3B,CAAE,GAAG,IA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|
5
|
+
* nodeComfort.id.uuid(); // "3f2504e0-4f89-41d3-9a0c-0305e82c3301"
|
|
6
|
+
*
|
|
7
|
+
* @returns {string} A UUID v4 string.
|
|
8
|
+
*/
|
|
9
|
+
export function uuid(): string;
|
|
10
|
+
/**
|
|
11
|
+
* Generates a URL-safe, collision-resistant ID (nanoid-compatible alphabet).
|
|
12
|
+
*
|
|
13
|
+
* Uses secure randomness with unbiased rejection sampling.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* nodeComfort.id.nano(); // "V1StGXR8_Z5jdHi6B-myT"
|
|
17
|
+
* nodeComfort.id.nano(10); // "IRFa-VaY2b"
|
|
18
|
+
*
|
|
19
|
+
* @param {number} [size=21] - Desired length.
|
|
20
|
+
* @param {string} [alphabet] - Custom alphabet.
|
|
21
|
+
* @returns {string} The generated ID.
|
|
22
|
+
*/
|
|
23
|
+
export function nano(size?: number, alphabet?: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Generates a ULID • a lexicographically-sortable, timestamp-prefixed ID.
|
|
26
|
+
*
|
|
27
|
+
* ULIDs sort by creation time and are a great primary-key alternative to UUIDs.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* nodeComfort.id.ulid(); // "01ARZ3NDEKTSV4RRFFQ69G5FAV"
|
|
31
|
+
*
|
|
32
|
+
* @param {number} [time=Date.now()] - Timestamp in ms to encode.
|
|
33
|
+
* @returns {string} A 26-character ULID.
|
|
34
|
+
*/
|
|
35
|
+
export function ulid(time?: number): string;
|
|
36
|
+
/**
|
|
37
|
+
* Generates a cryptographically-secure random token.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* nodeComfort.id.token(); // 32-byte hex string
|
|
41
|
+
* nodeComfort.id.token(16, "base64url"); // 16 bytes, URL-safe base64
|
|
42
|
+
*
|
|
43
|
+
* @param {number} [bytes=32] - Number of random bytes.
|
|
44
|
+
* @param {"hex"|"base64"|"base64url"|"base58"} [encoding="hex"] - Output encoding.
|
|
45
|
+
* @returns {string} The token.
|
|
46
|
+
*/
|
|
47
|
+
export function token(bytes?: number, encoding?: "hex" | "base64" | "base64url" | "base58"): string;
|
|
48
|
+
/**
|
|
49
|
+
* Generates a short, human-friendly random code (uppercase + digits by default).
|
|
50
|
+
*
|
|
51
|
+
* Ambiguous characters (`0/O`, `1/I`) are excluded, making it suitable for
|
|
52
|
+
* coupons, OTP-like codes and invite links.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* nodeComfort.id.code(); // "K7QF9X"
|
|
56
|
+
* nodeComfort.id.code(4, "1234"); // "3142"
|
|
57
|
+
*
|
|
58
|
+
* @param {number} [length=6] - Number of characters.
|
|
59
|
+
* @param {string} [alphabet="ABCDEFGHJKLMNPQRSTUVWXYZ23456789"] - Characters to pick from.
|
|
60
|
+
* @returns {string} The random code.
|
|
61
|
+
*/
|
|
62
|
+
export function code(length?: number, alphabet?: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Hashes a value with a chosen algorithm.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* nodeComfort.id.hash("hello"); // sha256 hex
|
|
68
|
+
* nodeComfort.id.hash("hello", { algorithm: "md5" });
|
|
69
|
+
*
|
|
70
|
+
* @param {string|Buffer} value - Data to hash.
|
|
71
|
+
* @param {{ algorithm?: string, encoding?: "hex"|"base64"|"base64url" }} [options] - Hash options.
|
|
72
|
+
* @returns {string} The digest.
|
|
73
|
+
*/
|
|
74
|
+
export function hash(value: string | Buffer, options?: {
|
|
75
|
+
algorithm?: string;
|
|
76
|
+
encoding?: "hex" | "base64" | "base64url";
|
|
77
|
+
}): string;
|
|
78
|
+
/**
|
|
79
|
+
* Computes an HMAC signature for a value using a secret key.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* nodeComfort.id.hmac("payload", "secret"); // sha256 HMAC hex
|
|
83
|
+
*
|
|
84
|
+
* @param {string|Buffer} value - Data to sign.
|
|
85
|
+
* @param {string|Buffer} secret - Secret key.
|
|
86
|
+
* @param {{ algorithm?: string, encoding?: "hex"|"base64"|"base64url" }} [options] - HMAC options.
|
|
87
|
+
* @returns {string} The signature.
|
|
88
|
+
*/
|
|
89
|
+
export function hmac(value: string | Buffer, secret: string | Buffer, options?: {
|
|
90
|
+
algorithm?: string;
|
|
91
|
+
encoding?: "hex" | "base64" | "base64url";
|
|
92
|
+
}): string;
|
|
93
|
+
/**
|
|
94
|
+
* Compares two strings/buffers in constant time to prevent timing attacks.
|
|
95
|
+
*
|
|
96
|
+
* Use this when comparing secrets, tokens, or signatures.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* if (nodeComfort.id.safeEqual(provided, expected)) { ... }
|
|
100
|
+
*
|
|
101
|
+
* @param {string|Buffer} a - First value.
|
|
102
|
+
* @param {string|Buffer} b - Second value.
|
|
103
|
+
* @returns {boolean} True if equal.
|
|
104
|
+
*/
|
|
105
|
+
export function safeEqual(a: string | Buffer, b: string | Buffer): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Returns a monotonically-increasing counter ID prefixed with an optional label.
|
|
108
|
+
*
|
|
109
|
+
* Useful for readable, unique keys within a single process run.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* nodeComfort.id.seq("user"); // "user-1"
|
|
113
|
+
* nodeComfort.id.seq("user"); // "user-2"
|
|
114
|
+
*
|
|
115
|
+
* @param {string} [prefix=""] - Optional prefix.
|
|
116
|
+
* @returns {string} The sequential ID.
|
|
117
|
+
*/
|
|
118
|
+
export function seq(prefix?: string): string;
|
|
119
|
+
//# sourceMappingURL=Id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Id.d.ts","sourceRoot":"","sources":["../../src/Id.js"],"names":[],"mappings":"AAyDE;;;;;;;GAOG;AACH,wBAFa,MAAM,CAIlB;AAED;;;;;;;;;;;;GAYG;AACH,4BAJW,MAAM,aACN,MAAM,GACJ,MAAM,CAgBlB;AAED;;;;;;;;;;GAUG;AACH,4BAHW,MAAM,GACJ,MAAM,CAgBlB;AAED;;;;;;;;;;GAUG;AACH,8BAJW,MAAM,aACN,KAAK,GAAC,QAAQ,GAAC,WAAW,GAAC,QAAQ,GACjC,MAAM,CAMlB;AAED;;;;;;;;;;;;;GAaG;AACH,8BAJW,MAAM,aACN,MAAM,GACJ,MAAM,CASlB;AAED;;;;;;;;;;GAUG;AACH,4BAJW,MAAM,GAAC,MAAM,YACb;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,KAAK,GAAC,QAAQ,GAAC,WAAW,CAAA;CAAE,GAC3D,MAAM,CAMlB;AAED;;;;;;;;;;GAUG;AACH,4BALW,MAAM,GAAC,MAAM,UACb,MAAM,GAAC,MAAM,YACb;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,KAAK,GAAC,QAAQ,GAAC,WAAW,CAAA;CAAE,GAC3D,MAAM,CAMlB;AAED;;;;;;;;;;;GAWG;AACH,6BAJW,MAAM,GAAC,MAAM,KACb,MAAM,GAAC,MAAM,GACX,OAAO,CAOnB;AAED;;;;;;;;;;;GAWG;AACH,6BAHW,MAAM,GACJ,MAAM,CAKlB"}
|
package/types/src/Logger.d.ts
CHANGED
|
@@ -21,6 +21,39 @@ export function setDelimiter(options?: {
|
|
|
21
21
|
* @returns {Logger} The same Logger instance (for chaining)
|
|
22
22
|
*/
|
|
23
23
|
export function log(content: any): Logger;
|
|
24
|
+
/**
|
|
25
|
+
* Logs an informational message, prefixed with a cyan `ℹ INFO` label.
|
|
26
|
+
* @param {*} content - Content to print.
|
|
27
|
+
* @returns {Logger} The same Logger instance (for chaining).
|
|
28
|
+
*/
|
|
29
|
+
export function info(content: any): Logger;
|
|
30
|
+
/**
|
|
31
|
+
* Logs a success message, prefixed with a green `✔ OK` label.
|
|
32
|
+
* @param {*} content - Content to print.
|
|
33
|
+
* @returns {Logger} The same Logger instance (for chaining).
|
|
34
|
+
*/
|
|
35
|
+
export function success(content: any): Logger;
|
|
36
|
+
/**
|
|
37
|
+
* Logs a warning, prefixed with a yellow `⚠ WARN` label (writes to stderr).
|
|
38
|
+
* @param {*} content - Content to print.
|
|
39
|
+
* @returns {Logger} The same Logger instance (for chaining).
|
|
40
|
+
*/
|
|
41
|
+
export function warn(content: any): Logger;
|
|
42
|
+
/**
|
|
43
|
+
* Logs an error, prefixed with a red `✖ ERROR` label.
|
|
44
|
+
*
|
|
45
|
+
* Accepts an `Error` and prints its stack when available.
|
|
46
|
+
* @param {*} content - Content or Error to print.
|
|
47
|
+
* @returns {Logger} The same Logger instance (for chaining).
|
|
48
|
+
*/
|
|
49
|
+
export function error(content: any): Logger;
|
|
50
|
+
/**
|
|
51
|
+
* Logs a debug message (only when `DEBUG`/`NODE_DEBUG` env is set), prefixed
|
|
52
|
+
* with a gray `● DEBUG` label.
|
|
53
|
+
* @param {*} content - Content to print.
|
|
54
|
+
* @returns {Logger} The same Logger instance (for chaining).
|
|
55
|
+
*/
|
|
56
|
+
export function debug(content: any): Logger;
|
|
24
57
|
/**
|
|
25
58
|
* Start a log group (displays a label and adds indentation)
|
|
26
59
|
* @param {string} label - The group name to display
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Logger.d.ts","sourceRoot":"","sources":["../../src/Logger.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Logger.d.ts","sourceRoot":"","sources":["../../src/Logger.js"],"names":[],"mappings":"AAiPE;;;;GAIG;AACH,oCAHW,OAAO,GACL,MAAM,CAKlB;AAED;;;;;;GAMG;AACH,uCAJG;IAAyB,IAAI;IACJ,KAAK;CAC9B,GAAU,MAAM,CAQlB;AAED;;;;GAIG;AACH,6BAHW,GAAC,GACC,MAAM,CAiClB;AAED;;;;GAIG;AACH,8BAHW,GAAC,GACC,MAAM,CAIlB;AAED;;;;GAIG;AACH,iCAHW,GAAC,GACC,MAAM,CAIlB;AAED;;;;GAIG;AACH,8BAHW,GAAC,GACC,MAAM,CAIlB;AAED;;;;;;GAMG;AACH,+BAHW,GAAC,GACC,MAAM,CAMlB;AAED;;;;;GAKG;AACH,+BAHW,GAAC,GACC,MAAM,CAOlB;AAED;;;;GAIG;AACH,6BAHW,MAAM,GACJ,MAAM,CAOlB;AAED;;;GAGG;AACH,4BAFa,MAAM,CAKlB"}
|