@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 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, ...a } = t || {}, o = new AbortController(), s = r ? setTimeout(() => o.abort(), r) : null, c = new Headers(a.headers);
402
- n != null && (a.body = JSON.stringify(n), c.set("content-type", "application/json"));
403
- let l = new URL(e);
404
- i && (l.search = G(i).toString());
405
- async function u(e) {
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(l, {
409
- ...a,
410
- headers: c,
411
- signal: o.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: l.toString(),
416
- method: a.method || "GET",
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: l.toString(),
423
- method: a.method || "GET",
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 u((e) => e.arrayBuffer());
430
+ return l((e) => e.arrayBuffer());
433
431
  },
434
432
  async blob() {
435
- return u((e) => e.blob());
433
+ return l((e) => e.blob());
436
434
  },
437
435
  async json() {
438
- return c.has("accept") || c.set("accept", "application/json"), u((e) => e.json());
436
+ return s.has("accept") || s.set("accept", "application/json"), l((e) => e.json());
439
437
  },
440
438
  async text() {
441
- return u((e) => e.text());
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 J(e, t) {
460
- let n = `${e}`.match(/[a-zA-Z]+$/)?.[0] || "", r = n ? +`${e}`.slice(0, -n.length) : +e;
461
- if (n) {
462
- for (let [e, i] of t?.intervalLevels || q) if (r *= i, e === n) {
463
- n = "";
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 (n || isNaN(r)) throw Error(`Invalid interval: ${e}`);
468
- return r;
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 Z(e) {
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, q as defaultIntervalLevels, T as defer, L as deflateNumber, v as encodeText, X as formatDate, Y as formatInterval, z as formatNumber, B as formatPercentage, w as formatString, I as inflateNumber, N as limitConcurrency, O as loadJS, J as normalizeInterval, H as rateLimiter, b as replacerBigInt, Z as reprDate, C as reprJson, M as searchBuffer, K as simpleRequest, S as truncateString, U as waitUntil, E as wrapFunction };
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['searchParams']): URLSearchParams;
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
- import { type INumberOptions } from './number.ts';
2
- export declare const defaultIntervalLevels: Array<[string, number]>;
3
- export declare function normalizeInterval(interval: string | number, options?: Partial<{
4
- intervalLevels: Array<[string, number]>;
5
- }>): number;
6
- export declare function formatInterval(interval: number, options?: Partial<INumberOptions & {
7
- intervalLevels: Array<[string, number]>;
8
- }>): string;
9
- export declare function formatDate(date: Date): string;
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.0",
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 run clean && pnpm run /^build:/"
27
+ "build": "pnpm clean && pnpm build:js && pnpm build:types"
28
28
  }
29
29
  }