@gera2ld/common 0.1.1 → 0.1.3
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/dist/base64.d.ts +6 -0
- package/dist/buffer.d.ts +14 -0
- package/dist/concurrency.d.ts +13 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +116 -41
- package/dist/logger.d.ts +63 -0
- package/dist/logger.test.d.ts +1 -0
- package/dist/number.d.ts +41 -0
- package/dist/number.test.d.ts +1 -0
- package/dist/rate-limiter.d.ts +17 -0
- package/dist/request.d.ts +25 -0
- package/dist/request.test.d.ts +1 -0
- package/dist/time.d.ts +17 -0
- package/dist/time.test.d.ts +1 -0
- package/dist/util.d.ts +21 -0
- package/package.json +2 -2
package/dist/base64.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function b64encode(bytes: Uint8Array): string;
|
|
2
|
+
export declare function b64decode(base64: string): Uint8Array<ArrayBuffer>;
|
|
3
|
+
export declare function b64urlEncode(bytes: Uint8Array): string;
|
|
4
|
+
export declare function b64urlDecode(base64url: string): Uint8Array<ArrayBuffer>;
|
|
5
|
+
export declare function encodeText(str: string): Uint8Array<ArrayBuffer>;
|
|
6
|
+
export declare function decodeText(bytes: Uint8Array): string;
|
package/dist/buffer.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const CrLf: Uint8Array<ArrayBuffer>;
|
|
2
|
+
export declare class BufferReader {
|
|
3
|
+
private buffer;
|
|
4
|
+
private requests;
|
|
5
|
+
isClosed: boolean;
|
|
6
|
+
read(size?: number): Promise<Uint8Array>;
|
|
7
|
+
readExact(size: number): Promise<Uint8Array>;
|
|
8
|
+
readUntil(delimiter?: string | Uint8Array): Promise<Uint8Array>;
|
|
9
|
+
feed(chunk: Uint8Array): void;
|
|
10
|
+
close(): void;
|
|
11
|
+
private processBuffer;
|
|
12
|
+
}
|
|
13
|
+
export declare function concatBuffer(a: Uint8Array, b: Uint8Array): Uint8Array;
|
|
14
|
+
export declare function searchBuffer(buffer: Uint8Array, slice: Uint8Array): number;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare function limitConcurrency<T extends unknown[], U>(fn: (...args: T) => Promise<U>, concurrency: number): (...args: T) => Promise<U>;
|
|
2
|
+
export declare class Queue<T> {
|
|
3
|
+
maxSize: number;
|
|
4
|
+
private data;
|
|
5
|
+
private getQueue;
|
|
6
|
+
private putQueue;
|
|
7
|
+
constructor(maxSize?: number);
|
|
8
|
+
get size(): number;
|
|
9
|
+
private defer;
|
|
10
|
+
private resolve;
|
|
11
|
+
get(maxWait?: number): Promise<T | undefined>;
|
|
12
|
+
put(item: T, maxWait?: number): Promise<void>;
|
|
13
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
CHANGED
|
@@ -256,9 +256,29 @@ var P = class {
|
|
|
256
256
|
async put(e, t = 0) {
|
|
257
257
|
this.maxSize && this.data.length >= this.maxSize && await this.defer(this.putQueue, t), this.data.push(e), this.resolve(this.getQueue);
|
|
258
258
|
}
|
|
259
|
-
}, F =
|
|
259
|
+
}, F = {
|
|
260
|
+
debug: 10,
|
|
261
|
+
info: 20,
|
|
262
|
+
warn: 30,
|
|
263
|
+
error: 40
|
|
264
|
+
}, I = {
|
|
265
|
+
debug: "debug",
|
|
266
|
+
info: "info",
|
|
267
|
+
warn: "warn",
|
|
268
|
+
error: "error"
|
|
269
|
+
};
|
|
270
|
+
function L(e) {
|
|
271
|
+
return typeof e == "function" ? e() || "" : e || "";
|
|
272
|
+
}
|
|
273
|
+
function R(e, t) {
|
|
274
|
+
return e && t ? `${e}:${t}` : t || e;
|
|
275
|
+
}
|
|
276
|
+
function z(e, t) {
|
|
277
|
+
return typeof e == "string" ? w(e, ...t) : w("%o", e, ...t);
|
|
278
|
+
}
|
|
279
|
+
var B = class e {
|
|
260
280
|
static defaultOptions = {};
|
|
261
|
-
|
|
281
|
+
collectors = [];
|
|
262
282
|
options;
|
|
263
283
|
constructor(t) {
|
|
264
284
|
this.options = {
|
|
@@ -266,24 +286,79 @@ var P = class {
|
|
|
266
286
|
...typeof t == "string" ? { prefix: t } : t
|
|
267
287
|
};
|
|
268
288
|
}
|
|
289
|
+
setOptions(e) {
|
|
290
|
+
return Object.assign(this.options, e), this;
|
|
291
|
+
}
|
|
292
|
+
clone(t) {
|
|
293
|
+
return new e({
|
|
294
|
+
...this.options,
|
|
295
|
+
...t
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
child(t) {
|
|
299
|
+
let n = this.options.prefix, r = new e({
|
|
300
|
+
...this.options,
|
|
301
|
+
prefix: () => R(L(n), L(t))
|
|
302
|
+
});
|
|
303
|
+
return r.collectors = this.collectors, r;
|
|
304
|
+
}
|
|
269
305
|
getPrefix() {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
306
|
+
return L(this.options.prefix);
|
|
307
|
+
}
|
|
308
|
+
getLevel() {
|
|
309
|
+
return this.options.level || "debug";
|
|
310
|
+
}
|
|
311
|
+
setLevel(e) {
|
|
312
|
+
return this.setOptions({ level: e });
|
|
313
|
+
}
|
|
314
|
+
isEnabledFor(e) {
|
|
315
|
+
return this.options.enabled !== !1 && F[e] >= F[this.getLevel()];
|
|
316
|
+
}
|
|
317
|
+
resolveTimestamp() {
|
|
318
|
+
let { timestamp: e } = this.options;
|
|
319
|
+
if (e) return typeof e == "function" ? e() : (/* @__PURE__ */ new Date()).toISOString();
|
|
320
|
+
}
|
|
321
|
+
defaultSink(e) {
|
|
322
|
+
let t = I[e.level];
|
|
323
|
+
(console[t] || console.log)(...[
|
|
324
|
+
e.timestamp,
|
|
325
|
+
e.prefix,
|
|
326
|
+
e.message
|
|
327
|
+
].filter(Boolean));
|
|
328
|
+
}
|
|
329
|
+
collect(e) {
|
|
330
|
+
let t = {
|
|
331
|
+
entries: [],
|
|
332
|
+
silent: e?.silent
|
|
333
|
+
};
|
|
334
|
+
return this.collectors.push(t), () => {
|
|
335
|
+
let e = this.collectors.indexOf(t);
|
|
336
|
+
if (e < 0) throw Error("Logs already collected");
|
|
337
|
+
return this.collectors.splice(e, 1), t.entries;
|
|
278
338
|
};
|
|
279
339
|
}
|
|
280
340
|
log(e, t, ...n) {
|
|
281
|
-
|
|
282
|
-
|
|
341
|
+
if (!this.isEnabledFor(e)) return;
|
|
342
|
+
let r = {
|
|
343
|
+
level: e,
|
|
344
|
+
prefix: this.getPrefix(),
|
|
345
|
+
message: z(t, n),
|
|
346
|
+
timestamp: this.resolveTimestamp(),
|
|
347
|
+
args: n
|
|
348
|
+
};
|
|
349
|
+
for (let e of this.collectors) e.entries.push(r);
|
|
350
|
+
let i = this.collectors[this.collectors.length - 1];
|
|
351
|
+
this.options.silent || i?.silent || (this.options.sink ? this.options.sink(r) : this.defaultSink(r));
|
|
352
|
+
}
|
|
353
|
+
debug(e, ...t) {
|
|
354
|
+
return this.log("debug", e, ...t);
|
|
283
355
|
}
|
|
284
356
|
info(e, ...t) {
|
|
285
357
|
return this.log("info", e, ...t);
|
|
286
358
|
}
|
|
359
|
+
warn(e, ...t) {
|
|
360
|
+
return this.log("warn", e, ...t);
|
|
361
|
+
}
|
|
287
362
|
error(e, ...t) {
|
|
288
363
|
return this.log("error", e, ...t);
|
|
289
364
|
}
|
|
@@ -296,13 +371,13 @@ var P = class {
|
|
|
296
371
|
};
|
|
297
372
|
//#endregion
|
|
298
373
|
//#region src/number.ts
|
|
299
|
-
function
|
|
374
|
+
function V(t, n) {
|
|
300
375
|
return BigInt(e(t).multipliedBy(e(10).exponentiatedBy(n)).toFixed(0));
|
|
301
376
|
}
|
|
302
|
-
function
|
|
377
|
+
function H(t, n) {
|
|
303
378
|
return e(t).dividedBy(e(10).exponentiatedBy(n)).toNumber();
|
|
304
379
|
}
|
|
305
|
-
var
|
|
380
|
+
var U = {
|
|
306
381
|
maximumFractionDigits: 4,
|
|
307
382
|
exponentialThresholdLarge: 1e6,
|
|
308
383
|
exponentialThresholdSmall: 1e-6,
|
|
@@ -311,12 +386,12 @@ var R = {
|
|
|
311
386
|
quantizationStep: 0,
|
|
312
387
|
keepTrailingZeros: !1
|
|
313
388
|
};
|
|
314
|
-
function
|
|
389
|
+
function W(e, t) {
|
|
315
390
|
if (e == null || e === "") return "";
|
|
316
391
|
let n = +e;
|
|
317
392
|
if (Number.isNaN(n)) return "";
|
|
318
393
|
let r = {
|
|
319
|
-
...
|
|
394
|
+
...U,
|
|
320
395
|
...t
|
|
321
396
|
};
|
|
322
397
|
r.quantizationStep > 0 && (n = Math.round(n / r.quantizationStep) * r.quantizationStep);
|
|
@@ -333,19 +408,19 @@ function z(e, t) {
|
|
|
333
408
|
let s = new Intl.NumberFormat("en-US", o).format(n);
|
|
334
409
|
return !r.keepTrailingZeros && s.includes(".") && (s = s.replace(/\.?0+$/, "")), s;
|
|
335
410
|
}
|
|
336
|
-
function
|
|
337
|
-
return
|
|
411
|
+
function G(e, t) {
|
|
412
|
+
return W(e * 100, t) + "%";
|
|
338
413
|
}
|
|
339
414
|
//#endregion
|
|
340
415
|
//#region src/rate-limiter.ts
|
|
341
|
-
var
|
|
416
|
+
var K = {
|
|
342
417
|
interval: 2e3,
|
|
343
418
|
maxRetry: 6,
|
|
344
419
|
maxInterval: 0
|
|
345
420
|
};
|
|
346
|
-
function
|
|
421
|
+
function q(e) {
|
|
347
422
|
let n = {
|
|
348
|
-
...
|
|
423
|
+
...K,
|
|
349
424
|
...e
|
|
350
425
|
}, { interval: r, maxInterval: i } = n;
|
|
351
426
|
i && i < r && (i = r);
|
|
@@ -365,9 +440,9 @@ function H(e) {
|
|
|
365
440
|
return a = o.catch(() => {}).then(() => t(r)), o;
|
|
366
441
|
};
|
|
367
442
|
}
|
|
368
|
-
async function
|
|
443
|
+
async function J(e, n) {
|
|
369
444
|
let r = {
|
|
370
|
-
...
|
|
445
|
+
...K,
|
|
371
446
|
...n
|
|
372
447
|
}, { interval: i, maxInterval: a } = r;
|
|
373
448
|
a && a < i && (a = i);
|
|
@@ -377,14 +452,14 @@ async function U(e, n) {
|
|
|
377
452
|
}
|
|
378
453
|
//#endregion
|
|
379
454
|
//#region src/request.ts
|
|
380
|
-
var
|
|
455
|
+
var Y = class extends Error {
|
|
381
456
|
request;
|
|
382
457
|
response;
|
|
383
458
|
constructor(e, t, n, r) {
|
|
384
459
|
super(e, { cause: r }), this.request = t, this.response = n;
|
|
385
460
|
}
|
|
386
461
|
};
|
|
387
|
-
function
|
|
462
|
+
function X(e) {
|
|
388
463
|
let t = new URLSearchParams();
|
|
389
464
|
return Array.isArray(e) || e instanceof URLSearchParams ? new URLSearchParams(e).forEach((e, n) => {
|
|
390
465
|
t.append(n, e);
|
|
@@ -397,11 +472,11 @@ function G(e) {
|
|
|
397
472
|
} else t.append(e, n);
|
|
398
473
|
}), t;
|
|
399
474
|
}
|
|
400
|
-
function
|
|
475
|
+
function Z(e, t) {
|
|
401
476
|
let { json: n, timeout: r = 1e4, searchParams: i, signal: a = AbortSignal.timeout(r), ...o } = t || {}, s = new Headers(o.headers);
|
|
402
477
|
n != null && (o.body = JSON.stringify(n), s.set("content-type", "application/json"));
|
|
403
478
|
let c = new URL(e);
|
|
404
|
-
i && (c.search =
|
|
479
|
+
i && (c.search = X(i).toString());
|
|
405
480
|
async function l(e) {
|
|
406
481
|
let n;
|
|
407
482
|
try {
|
|
@@ -411,14 +486,14 @@ function K(e, t) {
|
|
|
411
486
|
signal: a
|
|
412
487
|
});
|
|
413
488
|
let r = await e(n);
|
|
414
|
-
if (!n.ok) throw new
|
|
489
|
+
if (!n.ok) throw new Y(n.statusText, {
|
|
415
490
|
url: c.toString(),
|
|
416
491
|
method: o.method || "GET",
|
|
417
492
|
options: t
|
|
418
493
|
}, n, r);
|
|
419
494
|
return r;
|
|
420
495
|
} catch (e) {
|
|
421
|
-
throw e instanceof
|
|
496
|
+
throw e instanceof Y ? e : new Y(e?.message || `${e}`, {
|
|
422
497
|
url: c.toString(),
|
|
423
498
|
method: o.method || "GET",
|
|
424
499
|
options: t
|
|
@@ -442,7 +517,7 @@ function K(e, t) {
|
|
|
442
517
|
}
|
|
443
518
|
//#endregion
|
|
444
519
|
//#region src/time.ts
|
|
445
|
-
var
|
|
520
|
+
var Q = [
|
|
446
521
|
["ms", 1],
|
|
447
522
|
["s", 1e3],
|
|
448
523
|
["m", 60],
|
|
@@ -450,7 +525,7 @@ var q = [
|
|
|
450
525
|
["d", 24],
|
|
451
526
|
["mo", 30],
|
|
452
527
|
["y", 365 / 30]
|
|
453
|
-
],
|
|
528
|
+
], $ = [
|
|
454
529
|
["year", 365 * 24 * 60 * 60 * 1e3],
|
|
455
530
|
["month", 720 * 60 * 60 * 1e3],
|
|
456
531
|
["week", 10080 * 60 * 1e3],
|
|
@@ -459,10 +534,10 @@ var q = [
|
|
|
459
534
|
["minute", 60 * 1e3],
|
|
460
535
|
["second", 1e3]
|
|
461
536
|
];
|
|
462
|
-
function
|
|
537
|
+
function ee(e) {
|
|
463
538
|
let t = `${e}`.match(/[a-zA-Z]+$/)?.[0] || "", n = t ? +`${e}`.slice(0, -t.length) : +e;
|
|
464
539
|
if (t) {
|
|
465
|
-
for (let [e, r] of
|
|
540
|
+
for (let [e, r] of Q) if (n *= r, e === t) {
|
|
466
541
|
t = "";
|
|
467
542
|
break;
|
|
468
543
|
}
|
|
@@ -470,9 +545,9 @@ function Y(e) {
|
|
|
470
545
|
if (t || isNaN(n)) throw Error(`Invalid interval: ${e}`);
|
|
471
546
|
return n;
|
|
472
547
|
}
|
|
473
|
-
function
|
|
548
|
+
function te(e, t) {
|
|
474
549
|
let { locale: n, style: r = "long" } = t ?? {}, i = Math.abs(e);
|
|
475
|
-
for (let [e, t] of
|
|
550
|
+
for (let [e, t] of $) if (i >= t) return new Intl.NumberFormat(n, {
|
|
476
551
|
style: "unit",
|
|
477
552
|
unit: e,
|
|
478
553
|
unitDisplay: r
|
|
@@ -483,22 +558,22 @@ function X(e, t) {
|
|
|
483
558
|
unitDisplay: r
|
|
484
559
|
}).format(Math.round(i / 1e3));
|
|
485
560
|
}
|
|
486
|
-
function
|
|
561
|
+
function ne(e) {
|
|
487
562
|
return [
|
|
488
563
|
e.getFullYear(),
|
|
489
564
|
e.getMonth() + 1,
|
|
490
565
|
e.getDate()
|
|
491
566
|
].map((e) => `${e}`.padStart(2, "0")).join("-");
|
|
492
567
|
}
|
|
493
|
-
function
|
|
568
|
+
function re(e) {
|
|
494
569
|
return new Intl.DateTimeFormat("en-GB", {
|
|
495
570
|
dateStyle: "short",
|
|
496
571
|
timeStyle: "medium"
|
|
497
572
|
}).format(new Date(e));
|
|
498
573
|
}
|
|
499
|
-
function
|
|
574
|
+
function ie(e, t) {
|
|
500
575
|
let n = new Date(e).getTime() - Date.now(), r = Math.abs(n), { locale: i, numeric: a = "auto", style: o = "long" } = t ?? {};
|
|
501
|
-
for (let [e, t] of
|
|
576
|
+
for (let [e, t] of $) if (r >= t) return new Intl.RelativeTimeFormat(i, {
|
|
502
577
|
numeric: a,
|
|
503
578
|
style: o
|
|
504
579
|
}).format(Math.round(n / t), e);
|
|
@@ -508,4 +583,4 @@ function $(e, t) {
|
|
|
508
583
|
}).format(Math.round(n / 1e3), "second");
|
|
509
584
|
}
|
|
510
585
|
//#endregion
|
|
511
|
-
export { A as BufferReader, k as CrLf,
|
|
586
|
+
export { A as BufferReader, k as CrLf, B as Logger, P as Queue, Y as SimpleRequestError, h as b64decode, m as b64encode, _ as b64urlDecode, g as b64urlEncode, X as buildSearchParams, D as bytesToHex, j as concatBuffer, y as decodeText, T as defer, H as deflateNumber, v as encodeText, te as formatDuration, ne as formatISODate, W as formatNumber, G as formatPercentage, w as formatString, V as inflateNumber, N as limitConcurrency, O as loadJS, ee as normalizeInterval, q as rateLimiter, b as replacerBigInt, re as reprDate, C as reprJson, M as searchBuffer, Z as simpleRequest, ie as timeAgo, S as truncateString, J as waitUntil, E as wrapFunction };
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
|
+
export interface LogEntry {
|
|
3
|
+
level: LogLevel;
|
|
4
|
+
prefix: string;
|
|
5
|
+
message: string;
|
|
6
|
+
timestamp?: string;
|
|
7
|
+
/** Raw args passed to the log call, useful for custom sinks. */
|
|
8
|
+
args: unknown[];
|
|
9
|
+
}
|
|
10
|
+
export interface LoggerOptions {
|
|
11
|
+
prefix?: string | (() => string);
|
|
12
|
+
/** Minimum level to emit. Defaults to 'debug'. */
|
|
13
|
+
level?: LogLevel;
|
|
14
|
+
/** When false, nothing is emitted or collected. Defaults to true. */
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
/** When true, console/sink output is skipped but collectors still capture. */
|
|
17
|
+
silent?: boolean;
|
|
18
|
+
/** Include timestamp in entries. `true` uses `toISOString()`. */
|
|
19
|
+
timestamp?: boolean | (() => string);
|
|
20
|
+
/**
|
|
21
|
+
* Custom output. Replaces the default console output.
|
|
22
|
+
* Useful for file/memory/remote outputs or adapters like
|
|
23
|
+
* `entry => pino[entry.level](entry.message)`.
|
|
24
|
+
*/
|
|
25
|
+
sink?: (entry: LogEntry) => void;
|
|
26
|
+
}
|
|
27
|
+
/** @deprecated Use `LoggerOptions` instead. */
|
|
28
|
+
export type ILoggerOptions = LoggerOptions;
|
|
29
|
+
export declare class Logger {
|
|
30
|
+
static defaultOptions: LoggerOptions;
|
|
31
|
+
private collectors;
|
|
32
|
+
options: LoggerOptions;
|
|
33
|
+
constructor(opts?: string | Partial<LoggerOptions>);
|
|
34
|
+
/** Update options in place. Returns `this` for chaining. */
|
|
35
|
+
setOptions(patch: Partial<LoggerOptions>): this;
|
|
36
|
+
/** Copy this logger, optionally overriding options. Collectors are not shared. */
|
|
37
|
+
clone(extra?: Partial<LoggerOptions>): Logger;
|
|
38
|
+
/**
|
|
39
|
+
* Create a sub-logger with an extended prefix.
|
|
40
|
+
* Collectors are shared so `parent.collect()` also captures child logs.
|
|
41
|
+
*/
|
|
42
|
+
child(prefix: string | (() => string)): Logger;
|
|
43
|
+
getPrefix(): string;
|
|
44
|
+
getLevel(): LogLevel;
|
|
45
|
+
setLevel(level: LogLevel): this;
|
|
46
|
+
isEnabledFor(level: LogLevel): boolean;
|
|
47
|
+
private resolveTimestamp;
|
|
48
|
+
private defaultSink;
|
|
49
|
+
/**
|
|
50
|
+
* Capture subsequent `LogEntry` objects.
|
|
51
|
+
* Supports nesting; each call gets its own buffer.
|
|
52
|
+
* With `{ silent: true }`, console/sink output is suppressed while capturing.
|
|
53
|
+
*/
|
|
54
|
+
collect(opts?: {
|
|
55
|
+
silent?: boolean;
|
|
56
|
+
}): () => LogEntry[];
|
|
57
|
+
log(level: LogLevel, pattern: unknown, ...args: unknown[]): void;
|
|
58
|
+
debug(pattern: unknown, ...args: unknown[]): void;
|
|
59
|
+
info(pattern: unknown, ...args: unknown[]): void;
|
|
60
|
+
warn(pattern: unknown, ...args: unknown[]): void;
|
|
61
|
+
error(pattern: unknown, ...args: unknown[]): void;
|
|
62
|
+
wrap<U extends unknown[], V, T>(fn: (this: T, ...args: U) => V, wrapper: (logger: Logger, fn: (this: T, ...args: U) => V, ...args: U) => V): (this: T, ...args: U) => V;
|
|
63
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/number.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export declare function inflateNumber(input: number | string, decimals: number): bigint;
|
|
2
|
+
export declare function deflateNumber(input: number | string | bigint, decimals: number): number;
|
|
3
|
+
export interface INumberOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Maximum digits after the decimal point in fixed-point notation.
|
|
6
|
+
* `1.2345` with `2` becomes `1.23`.
|
|
7
|
+
*/
|
|
8
|
+
maximumFractionDigits: number;
|
|
9
|
+
/**
|
|
10
|
+
* Absolute value threshold for switching to exponential notation for large numbers.
|
|
11
|
+
* `12345678`>\`1e6` becomes `1.23456e+7`.
|
|
12
|
+
*/
|
|
13
|
+
exponentialThresholdLarge: number;
|
|
14
|
+
/**
|
|
15
|
+
* Absolute value threshold for switching to exponential notation for small non-zero numbers.
|
|
16
|
+
* `0.000000123`<\`1e-6` becomes `1.23e-7`.
|
|
17
|
+
*/
|
|
18
|
+
exponentialThresholdSmall: number;
|
|
19
|
+
/**
|
|
20
|
+
* Minimum number of significant digits to display.
|
|
21
|
+
* `12` with `3` becomes `12.00`.
|
|
22
|
+
*/
|
|
23
|
+
minimumSignificantDigits: number;
|
|
24
|
+
/**
|
|
25
|
+
* Maximum number of significant digits to display.
|
|
26
|
+
* `123456789` with `5` might become `1.2346e+8`.
|
|
27
|
+
*/
|
|
28
|
+
maximumSignificantDigits: number;
|
|
29
|
+
/**
|
|
30
|
+
* Rounds the numerical value to the nearest multiple of this step *before* formatting.
|
|
31
|
+
* `123456` with `100` becomes `123400` then formatted.
|
|
32
|
+
*/
|
|
33
|
+
quantizationStep: number;
|
|
34
|
+
/**
|
|
35
|
+
* Whether to keep trailing zeros in the fractional part of a number.
|
|
36
|
+
* e.g. `1.200` becomes `1.2` if false.
|
|
37
|
+
*/
|
|
38
|
+
keepTrailingZeros?: boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare function formatNumber(num?: number | string, options?: Partial<INumberOptions>): string;
|
|
41
|
+
export declare function formatPercentage(value: number, options?: Partial<INumberOptions>): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface IRetryOptions {
|
|
2
|
+
interval: number;
|
|
3
|
+
maxRetry: number;
|
|
4
|
+
maxInterval: number;
|
|
5
|
+
}
|
|
6
|
+
export interface IRateLimitOptions extends IRetryOptions {
|
|
7
|
+
isTooFrequent?: (error: any) => boolean;
|
|
8
|
+
onScheduleRetry?: (interval: number) => void;
|
|
9
|
+
}
|
|
10
|
+
export declare function rateLimiter(options?: Partial<IRateLimitOptions>): <U>(fn: () => Promise<U>) => Promise<U>;
|
|
11
|
+
export interface IWaitResult<T = unknown> {
|
|
12
|
+
interval?: number;
|
|
13
|
+
error?: unknown;
|
|
14
|
+
ok: boolean;
|
|
15
|
+
data: T;
|
|
16
|
+
}
|
|
17
|
+
export declare function waitUntil<T = unknown>(fn: () => Promise<IWaitResult<T>>, options?: IRetryOptions): Promise<IWaitResult<T>>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type IRequestOptions = RequestInit & {
|
|
2
|
+
json?: unknown;
|
|
3
|
+
timeout?: number;
|
|
4
|
+
searchParams?: string[][] | Record<string, any> | URLSearchParams;
|
|
5
|
+
};
|
|
6
|
+
export declare class SimpleRequestError extends Error {
|
|
7
|
+
request: {
|
|
8
|
+
url: string;
|
|
9
|
+
method: string;
|
|
10
|
+
options?: IRequestOptions;
|
|
11
|
+
};
|
|
12
|
+
response?: Response | undefined;
|
|
13
|
+
constructor(message: string, request: {
|
|
14
|
+
url: string;
|
|
15
|
+
method: string;
|
|
16
|
+
options?: IRequestOptions;
|
|
17
|
+
}, response?: Response | undefined, cause?: unknown);
|
|
18
|
+
}
|
|
19
|
+
export declare function buildSearchParams(init?: IRequestOptions["searchParams"]): URLSearchParams;
|
|
20
|
+
export declare function simpleRequest<T = unknown>(url: string | URL, options?: IRequestOptions): {
|
|
21
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
22
|
+
blob(): Promise<Blob>;
|
|
23
|
+
json<U = T>(): Promise<U>;
|
|
24
|
+
text(): Promise<string>;
|
|
25
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/time.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Converts a string like "1h" or a number to milliseconds. */
|
|
2
|
+
export declare function normalizeInterval(interval: string | number): number;
|
|
3
|
+
/** Formats a signed ms duration as a locale-aware string. */
|
|
4
|
+
export declare function formatDuration(ms: number, options?: {
|
|
5
|
+
locale?: string;
|
|
6
|
+
style?: "long" | "short" | "narrow";
|
|
7
|
+
}): string;
|
|
8
|
+
/** Formats a Date as "YYYY-MM-DD". */
|
|
9
|
+
export declare function formatISODate(date: Date): string;
|
|
10
|
+
/** Formats a date using Intl.DateTimeFormat with en-GB short date and medium time. */
|
|
11
|
+
export declare function reprDate(date: number | string | Date): string;
|
|
12
|
+
/** Formats a date as a relative time string like "2 hours ago". */
|
|
13
|
+
export declare function timeAgo(date: number | string | Date, options?: {
|
|
14
|
+
locale?: string;
|
|
15
|
+
numeric?: "auto" | "always";
|
|
16
|
+
style?: "long" | "short" | "narrow";
|
|
17
|
+
}): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare function replacerBigInt(_key: string, value: any): any;
|
|
2
|
+
interface ReprJsonOptions {
|
|
3
|
+
truncateThreshold: number;
|
|
4
|
+
headLength: number;
|
|
5
|
+
tailLength: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function truncateString(input: string, options?: Partial<ReprJsonOptions>): string;
|
|
8
|
+
export declare function reprJson(obj: unknown, options?: Partial<ReprJsonOptions>): string;
|
|
9
|
+
export declare function formatString(fmt: string, ...args: unknown[]): string;
|
|
10
|
+
export interface IDeferred<T> {
|
|
11
|
+
promise: Promise<T>;
|
|
12
|
+
resolve: (result: T | Promise<T>) => void;
|
|
13
|
+
reject: (reason?: any) => void;
|
|
14
|
+
status: 'pending' | 'resolved' | 'rejected';
|
|
15
|
+
}
|
|
16
|
+
export declare function defer<T = void>(): IDeferred<T>;
|
|
17
|
+
type IFunction<U extends any[], V, T> = (this: T, ...args: U) => V;
|
|
18
|
+
export declare function wrapFunction<U extends any[], V, T>(fn: IFunction<U, V, T>, wrapper: (this: T, originalFn: IFunction<U, V, T>, ...args: U) => V): (this: T, ...args: U) => V;
|
|
19
|
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
20
|
+
export declare function loadJS(url: string): Promise<unknown>;
|
|
21
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gera2ld/common",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"exports": {
|
|
@@ -24,6 +24,6 @@
|
|
|
24
24
|
"clean": "del-cli dist tsconfig.tsbuildinfo",
|
|
25
25
|
"build:types": "tsc",
|
|
26
26
|
"build:js": "vite build",
|
|
27
|
-
"build": "pnpm
|
|
27
|
+
"build": "pnpm clean && pnpm build:js && pnpm build:types"
|
|
28
28
|
}
|
|
29
29
|
}
|