@gera2ld/common 0.1.0 → 0.1.2
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/index.js +59 -42
- package/dist/request.d.ts +1 -2
- package/dist/time.d.ts +16 -9
- package/dist/time.test.d.ts +1 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -398,50 +398,45 @@ function G(e) {
|
|
|
398
398
|
}), t;
|
|
399
399
|
}
|
|
400
400
|
function K(e, t) {
|
|
401
|
-
let { json: n, timeout: r = 1e4, searchParams: i,
|
|
402
|
-
n != null && (
|
|
403
|
-
let
|
|
404
|
-
i && (
|
|
405
|
-
async function
|
|
401
|
+
let { json: n, timeout: r = 1e4, searchParams: i, signal: a = AbortSignal.timeout(r), ...o } = t || {}, s = new Headers(o.headers);
|
|
402
|
+
n != null && (o.body = JSON.stringify(n), s.set("content-type", "application/json"));
|
|
403
|
+
let c = new URL(e);
|
|
404
|
+
i && (c.search = G(i).toString());
|
|
405
|
+
async function l(e) {
|
|
406
406
|
let n;
|
|
407
407
|
try {
|
|
408
|
-
n = await fetch(
|
|
409
|
-
...
|
|
410
|
-
headers:
|
|
411
|
-
signal:
|
|
408
|
+
n = await fetch(c, {
|
|
409
|
+
...o,
|
|
410
|
+
headers: s,
|
|
411
|
+
signal: a
|
|
412
412
|
});
|
|
413
413
|
let r = await e(n);
|
|
414
414
|
if (!n.ok) throw new W(n.statusText, {
|
|
415
|
-
url:
|
|
416
|
-
method:
|
|
415
|
+
url: c.toString(),
|
|
416
|
+
method: o.method || "GET",
|
|
417
417
|
options: t
|
|
418
418
|
}, n, r);
|
|
419
419
|
return r;
|
|
420
420
|
} catch (e) {
|
|
421
421
|
throw e instanceof W ? e : new W(e?.message || `${e}`, {
|
|
422
|
-
url:
|
|
423
|
-
method:
|
|
422
|
+
url: c.toString(),
|
|
423
|
+
method: o.method || "GET",
|
|
424
424
|
options: t
|
|
425
425
|
}, n, e);
|
|
426
|
-
} finally {
|
|
427
|
-
s && clearTimeout(s);
|
|
428
426
|
}
|
|
429
427
|
}
|
|
430
428
|
return {
|
|
431
429
|
async arrayBuffer() {
|
|
432
|
-
return
|
|
430
|
+
return l((e) => e.arrayBuffer());
|
|
433
431
|
},
|
|
434
432
|
async blob() {
|
|
435
|
-
return
|
|
433
|
+
return l((e) => e.blob());
|
|
436
434
|
},
|
|
437
435
|
async json() {
|
|
438
|
-
return
|
|
436
|
+
return s.has("accept") || s.set("accept", "application/json"), l((e) => e.json());
|
|
439
437
|
},
|
|
440
438
|
async text() {
|
|
441
|
-
return
|
|
442
|
-
},
|
|
443
|
-
abort() {
|
|
444
|
-
o.abort();
|
|
439
|
+
return l((e) => e.text());
|
|
445
440
|
}
|
|
446
441
|
};
|
|
447
442
|
}
|
|
@@ -455,40 +450,62 @@ var q = [
|
|
|
455
450
|
["d", 24],
|
|
456
451
|
["mo", 30],
|
|
457
452
|
["y", 365 / 30]
|
|
453
|
+
], J = [
|
|
454
|
+
["year", 365 * 24 * 60 * 60 * 1e3],
|
|
455
|
+
["month", 720 * 60 * 60 * 1e3],
|
|
456
|
+
["week", 10080 * 60 * 1e3],
|
|
457
|
+
["day", 1440 * 60 * 1e3],
|
|
458
|
+
["hour", 3600 * 1e3],
|
|
459
|
+
["minute", 60 * 1e3],
|
|
460
|
+
["second", 1e3]
|
|
458
461
|
];
|
|
459
|
-
function
|
|
460
|
-
let
|
|
461
|
-
if (
|
|
462
|
-
for (let [e,
|
|
463
|
-
|
|
462
|
+
function Y(e) {
|
|
463
|
+
let t = `${e}`.match(/[a-zA-Z]+$/)?.[0] || "", n = t ? +`${e}`.slice(0, -t.length) : +e;
|
|
464
|
+
if (t) {
|
|
465
|
+
for (let [e, r] of q) if (n *= r, e === t) {
|
|
466
|
+
t = "";
|
|
464
467
|
break;
|
|
465
468
|
}
|
|
466
469
|
}
|
|
467
|
-
if (
|
|
468
|
-
return
|
|
469
|
-
}
|
|
470
|
-
function Y(e, t) {
|
|
471
|
-
let n = Math.sign(e);
|
|
472
|
-
e = Math.abs(e);
|
|
473
|
-
let r = "ms";
|
|
474
|
-
for (let [n, i] of t?.intervalLevels || q) {
|
|
475
|
-
if (e < i) break;
|
|
476
|
-
e /= i, r = n;
|
|
477
|
-
}
|
|
478
|
-
return `${n < 0 ? "-" : ""}${z(e, t) || ""}${r}`;
|
|
470
|
+
if (t || isNaN(n)) throw Error(`Invalid interval: ${e}`);
|
|
471
|
+
return n;
|
|
479
472
|
}
|
|
480
|
-
function X(e) {
|
|
473
|
+
function X(e, t) {
|
|
474
|
+
let { locale: n, style: r = "long" } = t ?? {}, i = Math.abs(e);
|
|
475
|
+
for (let [e, t] of J) if (i >= t) return new Intl.NumberFormat(n, {
|
|
476
|
+
style: "unit",
|
|
477
|
+
unit: e,
|
|
478
|
+
unitDisplay: r
|
|
479
|
+
}).format(Math.round(i / t));
|
|
480
|
+
return new Intl.NumberFormat(n, {
|
|
481
|
+
style: "unit",
|
|
482
|
+
unit: "second",
|
|
483
|
+
unitDisplay: r
|
|
484
|
+
}).format(Math.round(i / 1e3));
|
|
485
|
+
}
|
|
486
|
+
function Z(e) {
|
|
481
487
|
return [
|
|
482
488
|
e.getFullYear(),
|
|
483
489
|
e.getMonth() + 1,
|
|
484
490
|
e.getDate()
|
|
485
491
|
].map((e) => `${e}`.padStart(2, "0")).join("-");
|
|
486
492
|
}
|
|
487
|
-
function
|
|
493
|
+
function Q(e) {
|
|
488
494
|
return new Intl.DateTimeFormat("en-GB", {
|
|
489
495
|
dateStyle: "short",
|
|
490
496
|
timeStyle: "medium"
|
|
491
497
|
}).format(new Date(e));
|
|
492
498
|
}
|
|
499
|
+
function $(e, t) {
|
|
500
|
+
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 J) if (r >= t) return new Intl.RelativeTimeFormat(i, {
|
|
502
|
+
numeric: a,
|
|
503
|
+
style: o
|
|
504
|
+
}).format(Math.round(n / t), e);
|
|
505
|
+
return new Intl.RelativeTimeFormat(i, {
|
|
506
|
+
numeric: a,
|
|
507
|
+
style: o
|
|
508
|
+
}).format(Math.round(n / 1e3), "second");
|
|
509
|
+
}
|
|
493
510
|
//#endregion
|
|
494
|
-
export { A as BufferReader, k as CrLf, F as Logger, P as Queue, W as SimpleRequestError, h as b64decode, m as b64encode, _ as b64urlDecode, g as b64urlEncode, G as buildSearchParams, D as bytesToHex, j as concatBuffer, y as decodeText,
|
|
511
|
+
export { A as BufferReader, k as CrLf, F as Logger, P as Queue, W as SimpleRequestError, h as b64decode, m as b64encode, _ as b64urlDecode, g as b64urlEncode, G as buildSearchParams, D as bytesToHex, j as concatBuffer, y as decodeText, T as defer, L as deflateNumber, v as encodeText, X as formatDuration, Z as formatISODate, z as formatNumber, B as formatPercentage, w as formatString, I as inflateNumber, N as limitConcurrency, O as loadJS, Y as normalizeInterval, H as rateLimiter, b as replacerBigInt, Q as reprDate, C as reprJson, M as searchBuffer, K as simpleRequest, $ as timeAgo, S as truncateString, U as waitUntil, E as wrapFunction };
|
package/dist/request.d.ts
CHANGED
|
@@ -16,11 +16,10 @@ export declare class SimpleRequestError extends Error {
|
|
|
16
16
|
options?: IRequestOptions;
|
|
17
17
|
}, response?: Response | undefined, cause?: unknown);
|
|
18
18
|
}
|
|
19
|
-
export declare function buildSearchParams(init?: IRequestOptions[
|
|
19
|
+
export declare function buildSearchParams(init?: IRequestOptions["searchParams"]): URLSearchParams;
|
|
20
20
|
export declare function simpleRequest<T = unknown>(url: string | URL, options?: IRequestOptions): {
|
|
21
21
|
arrayBuffer(): Promise<ArrayBuffer>;
|
|
22
22
|
blob(): Promise<Blob>;
|
|
23
23
|
json<U = T>(): Promise<U>;
|
|
24
24
|
text(): Promise<string>;
|
|
25
|
-
abort(): void;
|
|
26
25
|
};
|
package/dist/time.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
export declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export declare function
|
|
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. */
|
|
10
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gera2ld/common",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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
|
}
|