@cgtk/std 0.0.193 → 0.0.194

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 (121) hide show
  1. package/array.d.ts +1 -1
  2. package/array.js +1 -1
  3. package/assign.d.ts +1 -1
  4. package/assign.js +1 -1
  5. package/async.d.ts +1 -1
  6. package/async.js +2 -2
  7. package/basen.js +1 -1
  8. package/buffer.d.ts +1 -1
  9. package/buffer.js +1 -1
  10. package/checks.d.ts +1 -1
  11. package/checks.js +2 -2
  12. package/dom.d.ts +1 -1
  13. package/dom.js +3 -3
  14. package/fn.d.ts +1 -1
  15. package/fn.js +1 -1
  16. package/http.js +2 -2
  17. package/iterable.d.ts +1 -1
  18. package/iterable.js +3 -3
  19. package/map.d.ts +1 -1
  20. package/map.js +2 -2
  21. package/math.js +1 -1
  22. package/npy.d.ts +1 -1
  23. package/number.js +1 -1
  24. package/package.json +2 -2
  25. package/port.d.ts +1 -1
  26. package/port.js +4 -4
  27. package/progress.d.ts +1 -1
  28. package/rect.d.ts +1 -1
  29. package/rect.js +1 -1
  30. package/resource.d.ts +1 -1
  31. package/resource.js +2 -2
  32. package/schedule.d.ts +1 -1
  33. package/schedule.js +2 -2
  34. package/signal.d.ts +1 -1
  35. package/signal.js +4 -4
  36. package/src/array.d.ts +14 -0
  37. package/src/array.js +21 -0
  38. package/src/assign.d.ts +8 -0
  39. package/src/assign.js +26 -0
  40. package/src/async.d.ts +8 -0
  41. package/src/async.js +31 -0
  42. package/src/base64.d.ts +1 -0
  43. package/src/base64.js +7 -0
  44. package/src/basen.d.ts +12 -0
  45. package/src/basen.js +31 -0
  46. package/src/buffer.d.ts +14 -0
  47. package/src/buffer.js +54 -0
  48. package/src/checks.d.ts +29 -0
  49. package/src/checks.js +44 -0
  50. package/src/constants.d.ts +5 -0
  51. package/src/constants.js +1 -0
  52. package/src/dom.d.ts +50 -0
  53. package/src/dom.js +36 -0
  54. package/src/fn.d.ts +33 -0
  55. package/src/fn.js +61 -0
  56. package/src/http.d.ts +9 -0
  57. package/src/http.js +33 -0
  58. package/src/index.d.ts +1 -0
  59. package/src/index.js +1 -0
  60. package/src/iterable.d.ts +43 -0
  61. package/src/iterable.js +158 -0
  62. package/src/json.d.ts +1 -0
  63. package/src/json.js +1 -0
  64. package/src/map.d.ts +12 -0
  65. package/src/map.js +35 -0
  66. package/src/math.d.ts +34 -0
  67. package/src/math.js +43 -0
  68. package/src/npy.d.ts +10 -0
  69. package/src/npy.js +29 -0
  70. package/src/number.d.ts +4 -0
  71. package/src/number.js +13 -0
  72. package/src/object.d.ts +15 -0
  73. package/src/object.js +13 -0
  74. package/src/port.d.ts +30 -0
  75. package/src/port.js +56 -0
  76. package/src/progress.d.ts +6 -0
  77. package/src/progress.js +1 -0
  78. package/src/rect.d.ts +5 -0
  79. package/src/rect.js +16 -0
  80. package/src/resource.d.ts +12 -0
  81. package/src/resource.js +28 -0
  82. package/src/schedule.d.ts +10 -0
  83. package/src/schedule.js +33 -0
  84. package/src/set.d.ts +2 -0
  85. package/src/set.js +2 -0
  86. package/src/signal.d.ts +37 -0
  87. package/src/signal.js +89 -0
  88. package/src/stream.d.ts +6 -0
  89. package/src/stream.js +26 -0
  90. package/src/string.d.ts +8 -0
  91. package/src/string.js +29 -0
  92. package/src/struct.d.ts +42 -0
  93. package/src/struct.js +58 -0
  94. package/src/tree.d.ts +11 -0
  95. package/src/tree.js +25 -0
  96. package/src/treemap.d.ts +13 -0
  97. package/src/treemap.js +71 -0
  98. package/src/typedarray.d.ts +31 -0
  99. package/src/typedarray.js +39 -0
  100. package/src/types.d.ts +63 -0
  101. package/src/types.js +1 -0
  102. package/src/utils.d.ts +4 -0
  103. package/src/utils.js +30 -0
  104. package/src/weak.d.ts +2 -0
  105. package/src/weak.js +5 -0
  106. package/stream.d.ts +1 -1
  107. package/stream.js +2 -2
  108. package/string.d.ts +1 -1
  109. package/string.js +2 -2
  110. package/struct.d.ts +2 -2
  111. package/struct.js +2 -2
  112. package/tree.d.ts +1 -1
  113. package/tree.js +3 -3
  114. package/treemap.d.ts +1 -1
  115. package/treemap.js +3 -3
  116. package/typedarray.d.ts +1 -1
  117. package/typedarray.js +3 -3
  118. package/utils.d.ts +1 -1
  119. package/utils.js +1 -1
  120. package/weak.d.ts +1 -1
  121. package/weak.js +1 -1
package/array.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, Tuple } from "./types";
1
+ import type { Fn, Tuple } from "./types.js";
2
2
  export declare const empty: <T>() => T[];
3
3
  export declare const push: <T>(xs: T[], x: T) => T[];
4
4
  export declare const unshift: <T>(xs: T[], x: T) => T[];
package/array.js CHANGED
@@ -1,4 +1,4 @@
1
- import { comp, scan } from "./fn";
1
+ import { comp, scan } from "./fn.js";
2
2
  export const empty = () => [];
3
3
  export const push = (xs, x) => (xs.push(x), xs);
4
4
  export const unshift = (xs, x) => (xs.unshift(x), xs);
package/assign.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { RecordOf } from "./types";
1
+ import type { RecordOf } from "./types.js";
2
2
  type PO = RecordOf<unknown>;
3
3
  type Merge<T extends PO, S extends PO> = {
4
4
  [K in keyof (T & S)]: K extends keyof S ? (K extends keyof T ? (S[K] extends PO ? (T[K] extends PO ? Merge<T[K], S[K]> : S[K]) : S[K]) : S[K]) : (K extends keyof T ? T[K] : never);
package/assign.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isUndefined, isNull } from "./checks";
1
+ import { isUndefined, isNull } from "./checks.js";
2
2
  const isPrimitive = (x) => {
3
3
  const t = typeof x;
4
4
  return t === "string" || t === "number" || t === "boolean";
package/async.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, Constantly, Maybe, Dispose, MaybePromise } from "./types";
1
+ import type { Fn, Constantly, Maybe, Dispose, MaybePromise } from "./types.js";
2
2
  export declare const timeout: (ms: number, signal?: AbortSignal) => Promise<unknown>;
3
3
  export declare const delay: (ms: number, signal?: AbortSignal) => <T>(x: T) => Promise<T>;
4
4
  export declare const probing: <T>(p: Promise<T>) => Constantly<Maybe<T>>;
package/async.js CHANGED
@@ -1,5 +1,5 @@
1
- import { filterT, disposable } from "./fn";
2
- import { isDefined } from "./checks";
1
+ import { filterT, disposable } from "./fn.js";
2
+ import { isDefined } from "./checks.js";
3
3
  export const timeout = (ms, signal) => new Promise((resolve, reject) => {
4
4
  if (signal?.aborted)
5
5
  reject(signal.reason);
package/basen.js CHANGED
@@ -1,4 +1,4 @@
1
- import { toFixedMax } from "./number";
1
+ import { toFixedMax } from "./number.js";
2
2
  export const basen = (b) => (m = 0, n = 0) => [b, m, n];
3
3
  export const base10 = basen(10);
4
4
  export const normalize = (x) => {
package/buffer.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn } from "./types";
1
+ import type { Fn } from "./types.js";
2
2
  export interface BufReader {
3
3
  offset: number;
4
4
  line: Fn<void, string>;
package/buffer.js CHANGED
@@ -1,4 +1,4 @@
1
- import { readLine } from './string';
1
+ import { readLine } from './string.js';
2
2
  export const bufReader = (data, offset = 0) => {
3
3
  const rl = readLine();
4
4
  const dv = new DataView(data.buffer, data.byteOffset);
package/checks.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { TypedArray, NumberArray, BigIntArray, Pred, TPred, Maybe } from "./types";
1
+ import type { TypedArray, NumberArray, BigIntArray, Pred, TPred, Maybe } from "./types.js";
2
2
  export declare const isTypedArray: (x: any) => x is TypedArray;
3
3
  export declare const isNumberArray: (x: any) => x is NumberArray;
4
4
  export declare const isBigIntArray: (x: any) => x is BigIntArray;
package/checks.js CHANGED
@@ -1,5 +1,5 @@
1
- import { comp, not, apply, and } from "./fn";
2
- import { retain } from "./array";
1
+ import { comp, not, apply, and } from "./fn.js";
2
+ import { retain } from "./array.js";
3
3
  export const isTypedArray = (x) => x && (x instanceof Uint8Array || x instanceof Uint8ClampedArray ||
4
4
  x instanceof Uint16Array || x instanceof Uint32Array ||
5
5
  x instanceof Int8Array || x instanceof Int16Array || x instanceof Int32Array ||
package/dom.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, Dispose, RecordOf, Maybe } from "./types";
1
+ import type { Fn, Dispose, RecordOf, Maybe } from "./types.js";
2
2
  export declare const customEvent: <T>(type: string, detail: T, { composed, bubbles, cancelable }?: EventInit) => CustomEvent<T>;
3
3
  type ListenOpts = boolean | AddEventListenerOptions;
4
4
  export declare const on: {
package/dom.js CHANGED
@@ -1,6 +1,6 @@
1
- import { forEach, noop } from "./fn";
2
- import { isDefined } from "./checks";
3
- import { EMPTY } from "./constants";
1
+ import { forEach, noop } from "./fn.js";
2
+ import { isDefined } from "./checks.js";
3
+ import { EMPTY } from "./constants.js";
4
4
  export const customEvent = (type, detail, { composed = false, bubbles = false, cancelable = false } = EMPTY.OBJ) => new CustomEvent(type, { bubbles, cancelable, composed, detail });
5
5
  export const on = (el, name, listener, opts = false) => {
6
6
  el.addEventListener(name, listener, opts);
package/fn.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, FnV, FnF, Pred, TPred, FirstParam, Last, Constantly, Maybe, Dispose, Reactive } from "./types";
1
+ import type { Fn, FnV, FnF, Pred, TPred, FirstParam, Last, Constantly, Maybe, Dispose, Reactive } from "./types.js";
2
2
  export declare const tee: <T>(f: Fn<T, void>) => Fn<T, T>;
3
3
  export declare const comp: <Fs extends Fn[]>(...fs: Fs) => (x: FirstParam<Fs[0]>) => Last<Fs> extends Fn<any, infer R> ? R : never;
4
4
  export declare const pipe: <T, Fs extends [Fn<T>, ...Fn[]]>(x: T, ...fs: Fs) => Last<Fs> extends Fn<any, infer R> ? R : never;
package/fn.js CHANGED
@@ -1,4 +1,4 @@
1
- import { add } from "./set";
1
+ import { add } from "./set.js";
2
2
  export const tee = f => x => (f(x), x);
3
3
  export const comp = (...fs) => (x) => fs.reduce((x, f) => f(x), x);
4
4
  export const pipe = (x, ...fs) => fs.reduce((x, f) => f(x), x);
package/http.js CHANGED
@@ -1,5 +1,5 @@
1
- import { tee } from "./fn";
2
- import { assert } from "./checks";
1
+ import { tee } from "./fn.js";
2
+ import { assert } from "./checks.js";
3
3
  export const assertStatus = (status) => (resp) => assert(resp.status == status, `Invalid HTTP Status ${resp.status}: ${resp.url}`);
4
4
  export const contentLength = (response) => {
5
5
  const cl = response.headers.get("content-length");
package/iterable.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, FnV, Indexed, Pred } from "./types";
1
+ import type { Fn, FnV, Indexed, Pred } from "./types.js";
2
2
  export declare const next: <T>(xs: Iterator<T>) => T;
3
3
  export declare const repeat: (n: number) => <A>(x: A) => Iterator<A>;
4
4
  export declare const just: <T>(x: T) => IteratorObject<T>;
package/iterable.js CHANGED
@@ -1,6 +1,6 @@
1
- import { assert, isDefined } from "./checks";
2
- import { pipe, comp, scan, apply, noop, identity, constantly } from "./fn";
3
- import { set } from "./map";
1
+ import { assert, isDefined } from "./checks.js";
2
+ import { pipe, comp, scan, apply, noop, identity, constantly } from "./fn.js";
3
+ import { set } from "./map.js";
4
4
  export const next = (xs) => {
5
5
  const { done, value } = xs.next();
6
6
  assert(isDefined(done) && !done, "iterator done");
package/map.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { FnV, Maybe } from "./types";
1
+ import type { FnV, Maybe } from "./types.js";
2
2
  export declare const transform: <K, S, V>({ create, update, remove }: {
3
3
  create: FnV<[S, K, Map<K, V>], V>;
4
4
  update: FnV<[V, S, K, Map<K, V>], V>;
package/map.js CHANGED
@@ -1,5 +1,5 @@
1
- import { isDefined } from "./checks";
2
- import * as I from "./iterable";
1
+ import { isDefined } from "./checks.js";
2
+ import * as I from "./iterable.js";
3
3
  export const transform = ({ create, update, remove }, dst = new Map()) => (src) => {
4
4
  for (const k of dst.keys())
5
5
  if (!src.has(k)) {
package/math.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isclose } from "./checks";
1
+ import { isclose } from "./checks.js";
2
2
  export const PI = Math.PI;
3
3
  export const TAU = PI * 2;
4
4
  export const HALF_PI = PI / 2;
package/npy.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { TypedArray, Elements } from "./types";
1
+ import type { TypedArray, Elements } from "./types.js";
2
2
  declare const DTYPES: readonly ["|u1", "<u2", "<u4", "|i1", "<i2", "<i4", "<f4", "<f8"];
3
3
  type DType = Elements<typeof DTYPES>;
4
4
  export interface NDArray {
package/number.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isBigInt } from "./checks";
1
+ import { isBigInt } from "./checks.js";
2
2
  export const add = (a, x) => a + x;
3
3
  export const toFixed = (x, n = 0) => isBigInt(x) ? x : x.toFixed(n);
4
4
  export const toFixedMax = (x, n = 0) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cgtk/std",
3
- "version": "0.0.193",
3
+ "version": "0.0.194",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "exports": {
@@ -41,7 +41,7 @@
41
41
  "./utils": "./utils.js"
42
42
  },
43
43
  "devDependencies": {
44
- "typescript": "^5.9.3"
44
+ "typescript": "^6.0.2"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public"
package/port.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Dispose, Fn, Fns, FnV, RecordOf, MaybePromise, Reactive } from "./types";
1
+ import type { Dispose, Fn, Fns, FnV, RecordOf, MaybePromise, Reactive } from "./types.js";
2
2
  export interface Message<K, V> {
3
3
  type: K;
4
4
  payload: V;
package/port.js CHANGED
@@ -1,7 +1,7 @@
1
- import { forEach, noop, bind } from "./fn";
2
- import { isDefined } from "./checks";
3
- import { on } from "./dom";
4
- import { promise } from "./async";
1
+ import { forEach, noop, bind } from "./fn.js";
2
+ import { isDefined } from "./checks.js";
3
+ import { on } from "./dom.js";
4
+ import { promise } from "./async.js";
5
5
  ;
6
6
  export const onMessages = (target, fs) => on(target, "message", ({ data: { type, payload } }) => (type in fs) && fs[type](payload));
7
7
  export const onMessage = (target, name, f) => on(target, "message", ({ data: { type, payload } }) => type == name && f(payload));
package/progress.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn } from './types';
1
+ import type { Fn } from './types.js';
2
2
  export type Progress = {
3
3
  begin: Fn<void>;
4
4
  progress: Fn<number>;
package/rect.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Tuple } from "./types";
1
+ import type { Tuple } from "./types.js";
2
2
  export type Rect = Tuple<number, 4>;
3
3
  export declare const rect: (x: number, y: number, w: number, h: number) => Rect;
4
4
  export declare const vertical: (ratio: number[]) => (i: number) => ([x, _, w, h]: Rect) => Rect;
package/rect.js CHANGED
@@ -1,4 +1,4 @@
1
- import { add } from "./number";
1
+ import { add } from "./number.js";
2
2
  export const rect = (x, y, w, h) => [x, y, w, h];
3
3
  export const vertical = (ratio) => {
4
4
  const tot = ratio.reduce(add, 0);
package/resource.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, Dispose, Structure, Reactive } from "./types";
1
+ import type { Fn, Dispose, Structure, Reactive } from "./types.js";
2
2
  export type Resource<T> = Reactive<T, Dispose | void> & {
3
3
  value: T;
4
4
  dispose: Dispose;
package/resource.js CHANGED
@@ -1,5 +1,5 @@
1
- import { forEach, noop } from "./fn";
2
- import { isFunction } from "./checks";
1
+ import { forEach, noop } from "./fn.js";
2
+ import { isFunction } from "./checks.js";
3
3
  export const resource = (value, dispose = noop) => {
4
4
  const res = ((f) => forEach(f(value) ?? noop, dispose));
5
5
  res.value = value;
package/schedule.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Dispose, Fn, FnV, Reactive } from './types';
1
+ import type { Dispose, Fn, FnV, Reactive } from './types.js';
2
2
  export declare const raf: Reactive<number>;
3
3
  export declare const timeout: (ms: number) => Reactive<void>;
4
4
  export declare const schedule: <T>(s: Reactive<T>) => <F extends FnV>(f: F) => FnV<Parameters<F>, Dispose>;
package/schedule.js CHANGED
@@ -1,5 +1,5 @@
1
- import { isUndefined, isDefined, counted } from './checks';
2
- import { comp, lazy, bind, apply, call, where, disposing, noop } from './fn';
1
+ import { isUndefined, isDefined, counted } from './checks.js';
2
+ import { comp, lazy, bind, apply, call, where, disposing, noop } from './fn.js';
3
3
  export const raf = f => bind(cancelAnimationFrame, requestAnimationFrame(f));
4
4
  export const timeout = (ms) => f => bind(clearTimeout, setTimeout(f, ms));
5
5
  export const schedule = (s) => (f) => call(comp(apply(bind((lazy), f)), s));
package/signal.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, FnV, Fns, Pred, Reactive, RecordOf, Structure } from "./types";
1
+ import type { Fn, FnV, Fns, Pred, Reactive, RecordOf, Structure } from "./types.js";
2
2
  export type Signal<T> = Fn<T> & {
3
3
  on: Reactive<T>;
4
4
  };
package/signal.js CHANGED
@@ -1,7 +1,7 @@
1
- import { after, constantly, noop, bind, comp, forEach, filter as filter_, identity } from "./fn";
2
- import { mapValues } from "./object";
3
- import { isDefined, isFunction } from "./checks";
4
- import { add } from "./set";
1
+ import { after, constantly, noop, bind, comp, forEach, filter as filter_, identity } from "./fn.js";
2
+ import { mapValues } from "./object.js";
3
+ import { isDefined, isFunction } from "./checks.js";
4
+ import { add } from "./set.js";
5
5
  export const create = (f, on) => {
6
6
  const g = f;
7
7
  g.on = on;
package/src/array.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { Fn, Tuple } from "./types.js";
2
+ export declare const empty: <T>() => T[];
3
+ export declare const push: <T>(xs: T[], x: T) => T[];
4
+ export declare const unshift: <T>(xs: T[], x: T) => T[];
5
+ export declare const get: (i: number) => <T>(xs: T[]) => T;
6
+ export declare const pop: <T>(xs: T[]) => T[];
7
+ export declare const shift: <T>(xs: T[]) => T[];
8
+ export declare const first: <T>(xs: T[]) => T;
9
+ export declare const cap: <T>(n: number, f: Fn<T[]>) => Fn<T[], T[]>;
10
+ export declare const retain: <T>(n: number, xs?: T[]) => (x: T) => T[];
11
+ export declare const tuple: <T, N extends number>(...xs: T[] & {
12
+ length: N;
13
+ }) => Tuple<T, N>;
14
+ export declare const seek: <T>(arr: ArrayLike<T>, vals: Set<T | number>, offset?: number) => number;
package/src/array.js ADDED
@@ -0,0 +1,21 @@
1
+ import { comp, scan } from "./fn.js";
2
+ export const empty = () => [];
3
+ export const push = (xs, x) => (xs.push(x), xs);
4
+ export const unshift = (xs, x) => (xs.unshift(x), xs);
5
+ export const get = (i) => (xs) => xs[i];
6
+ export const pop = (xs) => (xs.pop(), xs);
7
+ export const shift = (xs) => (xs.shift(), xs);
8
+ export const first = (xs) => xs[0];
9
+ export const cap = (n, f) => xs => {
10
+ while (xs.length > n)
11
+ f(xs);
12
+ return xs;
13
+ };
14
+ export const retain = (n, xs = []) => comp(scan((push), xs), cap(n, shift));
15
+ export const tuple = (...xs) => xs;
16
+ export const seek = (arr, vals, offset = 0) => {
17
+ for (let i = offset, n = arr.length; i < n; i++)
18
+ if (vals.has(arr[i]))
19
+ return i;
20
+ return -1;
21
+ };
@@ -0,0 +1,8 @@
1
+ import type { RecordOf } from "./types.js";
2
+ type PO = RecordOf<unknown>;
3
+ type Merge<T extends PO, S extends PO> = {
4
+ [K in keyof (T & S)]: K extends keyof S ? (K extends keyof T ? (S[K] extends PO ? (T[K] extends PO ? Merge<T[K], S[K]> : S[K]) : S[K]) : S[K]) : (K extends keyof T ? T[K] : never);
5
+ };
6
+ type Assign<T extends PO, Ts extends PO[]> = Ts extends [infer A extends PO, ...infer R extends PO[]] ? Assign<Merge<T, A>, R> : T;
7
+ export declare const assign: <T extends PO, Ts extends PO[]>(target: T, ...sources: Ts) => Assign<T, Ts>;
8
+ export {};
package/src/assign.js ADDED
@@ -0,0 +1,26 @@
1
+ import { isUndefined, isNull } from "./checks.js";
2
+ const isPrimitive = (x) => {
3
+ const t = typeof x;
4
+ return t === "string" || t === "number" || t === "boolean";
5
+ };
6
+ function isPlainObject(x) {
7
+ if (typeof x !== 'object')
8
+ return false;
9
+ const prototype = Object.getPrototypeOf(x);
10
+ return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
11
+ }
12
+ const canClone = (x) => isPrimitive(x) || isPlainObject(x) &&
13
+ Object.values(x).every(x => isUndefined(x) || isNull(x) || isPrimitive(x) || canClone(x));
14
+ const merge = (target, source) => {
15
+ Object.keys(source).forEach(key => {
16
+ const s = source[key], t = target[key];
17
+ if (isPlainObject(t) && isPlainObject(s))
18
+ target[key] = merge(t, s);
19
+ else if (canClone(s))
20
+ target[key] = structuredClone(s);
21
+ else
22
+ target[key] = s;
23
+ });
24
+ return target;
25
+ };
26
+ export const assign = (target, ...sources) => sources.reduce(merge, target);
package/src/async.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { Fn, Constantly, Maybe, Dispose, MaybePromise } from "./types.js";
2
+ export declare const timeout: (ms: number, signal?: AbortSignal) => Promise<unknown>;
3
+ export declare const delay: (ms: number, signal?: AbortSignal) => <T>(x: T) => Promise<T>;
4
+ export declare const probing: <T>(p: Promise<T>) => Constantly<Maybe<T>>;
5
+ export declare const resolved: <T>(p: Promise<T>) => (f: Fn<T>) => () => void;
6
+ export declare const promise: <T>(x: MaybePromise<T>) => Promise<any>;
7
+ export declare const then: <T>(p: Promise<T>) => <R>(f: Fn<T, R>) => Promise<R>;
8
+ export declare const dispose: (p: Promise<Dispose>) => Dispose;
package/src/async.js ADDED
@@ -0,0 +1,31 @@
1
+ import { filterT, disposable } from "./fn.js";
2
+ import { isDefined } from "./checks.js";
3
+ export const timeout = (ms, signal) => new Promise((resolve, reject) => {
4
+ if (signal?.aborted)
5
+ reject(signal.reason);
6
+ else {
7
+ const t = setTimeout(() => {
8
+ if (signal?.aborted)
9
+ reject(signal.reason);
10
+ else
11
+ resolve(undefined);
12
+ }, ms);
13
+ signal?.addEventListener("abort", () => (clearTimeout(t), reject(signal.reason)), { once: true });
14
+ }
15
+ });
16
+ export const delay = (ms, signal) => async (x) => (await timeout(ms, signal), x);
17
+ export const probing = (p) => {
18
+ let value = undefined;
19
+ p.then(x => value = x);
20
+ return () => value;
21
+ };
22
+ export const resolved = (p) => {
23
+ const val = probing(p);
24
+ return (f) => {
25
+ const g = (filterT((isDefined), f));
26
+ return () => g(val());
27
+ };
28
+ };
29
+ export const promise = (x) => (x instanceof Promise) ? x : Promise.resolve(x);
30
+ export const then = (p) => (f) => p.then(f);
31
+ export const dispose = (p) => disposable(async (dispose) => dispose(await p));
@@ -0,0 +1 @@
1
+ export declare const decodeBytes: (str: string) => Uint8Array<ArrayBuffer>;
package/src/base64.js ADDED
@@ -0,0 +1,7 @@
1
+ export const decodeBytes = (str) => {
2
+ const raw = atob(str), size = raw.length;
3
+ const res = new Uint8Array(size);
4
+ for (let i = 0; i < size; i++)
5
+ res[i] = raw.charCodeAt(i);
6
+ return res;
7
+ };
package/src/basen.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export type Base<N extends number> = [n: N, m: number, d: number];
2
+ export declare const basen: <B extends number>(b: B) => (m?: number, n?: number) => Base<B>;
3
+ export declare const base10: (m?: number, n?: number) => Base<10>;
4
+ export declare const normalize: <B extends number>(x: Base<B>) => Base<B>;
5
+ export declare const add: <B extends number>([b, m0, n0]: Base<B>, [_, m1, n1]: Base<B>) => Base<B>;
6
+ export declare const mul: <B extends number>([b, m0, n0]: Base<B>, [_, m1, n1]: Base<B>) => Base<B>;
7
+ export declare const addN: <B extends number>(x: Base<B>, a: number) => Base<B>;
8
+ export declare const mulN: <B extends number>(x: Base<B>, a: number) => Base<B>;
9
+ export declare const change: <B extends number, D extends number>([b, m, n]: Base<B>, d: D) => Base<D>;
10
+ export declare const toNumber: <B extends number>([b, m, n]: Base<B>) => number;
11
+ export declare const toString: <B extends number>([b, m, n]: Base<B>, d?: number) => string;
12
+ export declare const toExponential: ([_, m, n]: Base<10>, digits?: number, dMax?: number) => string;
package/src/basen.js ADDED
@@ -0,0 +1,31 @@
1
+ import { toFixedMax } from "./number.js";
2
+ export const basen = (b) => (m = 0, n = 0) => [b, m, n];
3
+ export const base10 = basen(10);
4
+ export const normalize = (x) => {
5
+ if (!Number.isInteger(x[2])) {
6
+ const n = Math.floor(x[2]);
7
+ x[1] *= x[0] ** (x[2] - n);
8
+ x[2] = n;
9
+ }
10
+ const d = x[1] != 0 ? Math.floor(Math.log(Math.abs(x[1])) / Math.log(x[0])) : 0;
11
+ x[1] /= x[0] ** d;
12
+ x[2] += d;
13
+ return x;
14
+ };
15
+ export const add = ([b, m0, n0], [_, m1, n1]) => {
16
+ const n = Math.max(n0, n1);
17
+ const m = m0 * b ** (n0 - n) + m1 * b ** (n1 - n);
18
+ return [b, m, n];
19
+ };
20
+ export const mul = ([b, m0, n0], [_, m1, n1]) => [b, m0 * m1, n0 + n1];
21
+ export const addN = (x, a) => (x[1] += a, normalize(x));
22
+ export const mulN = (x, a) => (x[1] *= a, normalize(x));
23
+ export const change = ([b, m, n], d) => {
24
+ const k = n * Math.log(b) / Math.log(d);
25
+ return basen(d)(m, k);
26
+ };
27
+ export const toNumber = ([b, m, n]) => m * b ** n;
28
+ export const toString = ([b, m, n], d = 2) => `${m.toFixed(d)}x${b}^${n}`;
29
+ export const toExponential = ([_, m, n], digits = 2, dMax = 2) => Math.abs(n) <= dMax
30
+ ? toFixedMax(m * 10 ** n, digits)
31
+ : `${toFixedMax(m, digits)}e${n.toFixed(0)}`;
@@ -0,0 +1,14 @@
1
+ import type { Fn } from "./types.js";
2
+ export interface BufReader {
3
+ offset: number;
4
+ line: Fn<void, string>;
5
+ u8: Fn<void, number>;
6
+ u16: Fn<boolean, number>;
7
+ u32: Fn<boolean, number>;
8
+ i8: Fn<void, number>;
9
+ i16: Fn<boolean, number>;
10
+ i32: Fn<boolean, number>;
11
+ f32: Fn<boolean, number>;
12
+ f64: Fn<boolean, number>;
13
+ }
14
+ export declare const bufReader: (data: Uint8Array, offset?: number) => BufReader;
package/src/buffer.js ADDED
@@ -0,0 +1,54 @@
1
+ import { readLine } from './string.js';
2
+ export const bufReader = (data, offset = 0) => {
3
+ const rl = readLine();
4
+ const dv = new DataView(data.buffer, data.byteOffset);
5
+ return {
6
+ get offset() { return offset; },
7
+ set offset(x) { offset = x; },
8
+ line() {
9
+ const line = rl(data, offset);
10
+ offset += line.length;
11
+ return line;
12
+ },
13
+ u8() {
14
+ const x = dv.getUint8(offset);
15
+ offset += 1;
16
+ return x;
17
+ },
18
+ u16(littleEndian = true) {
19
+ const x = dv.getUint16(offset, littleEndian);
20
+ offset += 2;
21
+ return x;
22
+ },
23
+ u32(littleEndian = true) {
24
+ const x = dv.getUint16(offset, littleEndian);
25
+ offset += 4;
26
+ return x;
27
+ },
28
+ i8() {
29
+ const x = dv.getInt8(offset);
30
+ offset += 1;
31
+ return x;
32
+ },
33
+ i16(littleEndian = true) {
34
+ const x = dv.getInt16(offset, littleEndian);
35
+ offset += 2;
36
+ return x;
37
+ },
38
+ i32(littleEndian = true) {
39
+ const x = dv.getInt32(offset, littleEndian);
40
+ offset += 4;
41
+ return x;
42
+ },
43
+ f32(littleEndian = true) {
44
+ const x = dv.getFloat32(offset, littleEndian);
45
+ offset += 4;
46
+ return x;
47
+ },
48
+ f64(littleEndian = true) {
49
+ const x = dv.getFloat64(offset, littleEndian);
50
+ offset += 8;
51
+ return x;
52
+ },
53
+ };
54
+ };
@@ -0,0 +1,29 @@
1
+ import type { TypedArray, NumberArray, BigIntArray, Pred, TPred, Maybe } from "./types.js";
2
+ export declare const isTypedArray: (x: any) => x is TypedArray;
3
+ export declare const isNumberArray: (x: any) => x is NumberArray;
4
+ export declare const isBigIntArray: (x: any) => x is BigIntArray;
5
+ export declare const isUndefined: (x: any) => x is undefined;
6
+ export declare const isNull: (x: any) => x is null;
7
+ export declare const isDefined: <T>(x: Maybe<T>) => x is T;
8
+ export declare function assert(condition: boolean, msg: string): asserts condition;
9
+ export declare function assertType<T>(x: any, pred: TPred<T>, msg: string): asserts x is T;
10
+ export declare const isObject: TPred<Object>;
11
+ export declare const isString: TPred<string>;
12
+ export declare const isNumber: TPred<number>;
13
+ export declare const isBigInt: TPred<bigint>;
14
+ export declare const isPositive: Pred<number>;
15
+ export declare const isTs: <T>(p: TPred<T>) => TPred<T[]>;
16
+ export declare const isclose: (a: number, b: number, eps?: number) => boolean;
17
+ export declare const allclose: (a: Array<number> | NumberArray, b: Array<number> | NumberArray, eps?: number) => boolean;
18
+ export declare const equal: <T>(a: T, b?: T) => boolean;
19
+ export declare const strictEqual: <T>(a: T, b?: T) => boolean;
20
+ export declare const isDiffPrev: <T>(eq?: (a: T, b?: T | undefined) => boolean) => Pred<T>;
21
+ export declare const isLonger: <T>(delta: number) => Pred<T>;
22
+ export declare const isFunction: <T extends Function>(x: any) => x is T;
23
+ export declare const isAborted: (signal: AbortSignal) => () => boolean;
24
+ export declare const isArrayBufferLike: (x: any) => x is ArrayBufferLike;
25
+ export declare const isArrayBufferView: (x: any) => x is ArrayBufferView;
26
+ export declare const isEmpty: (x: {
27
+ length: number;
28
+ }) => boolean;
29
+ export declare const counted: (n: number) => () => boolean;
package/src/checks.js ADDED
@@ -0,0 +1,44 @@
1
+ import { comp, not, apply, and } from "./fn.js";
2
+ import { retain } from "./array.js";
3
+ export const isTypedArray = (x) => x && (x instanceof Uint8Array || x instanceof Uint8ClampedArray ||
4
+ x instanceof Uint16Array || x instanceof Uint32Array ||
5
+ x instanceof Int8Array || x instanceof Int16Array || x instanceof Int32Array ||
6
+ x instanceof Float16Array || x instanceof Float32Array || x instanceof Float64Array ||
7
+ x instanceof BigUint64Array || x instanceof BigInt64Array);
8
+ export const isNumberArray = (x) => x && (x instanceof Uint8Array || x instanceof Uint8ClampedArray ||
9
+ x instanceof Uint16Array || x instanceof Uint32Array ||
10
+ x instanceof Int8Array || x instanceof Int16Array || x instanceof Int32Array ||
11
+ x instanceof Float16Array || x instanceof Float32Array || x instanceof Float64Array);
12
+ export const isBigIntArray = (x) => x && (x instanceof BigUint64Array || x instanceof BigInt64Array);
13
+ export const isUndefined = (x) => x === undefined;
14
+ export const isNull = (x) => x === null;
15
+ export const isDefined = (x) => !isUndefined(x);
16
+ export function assert(condition, msg) {
17
+ if (!condition)
18
+ throw new Error(msg);
19
+ }
20
+ export function assertType(x, pred, msg) { assert(pred(x), msg); }
21
+ export const isObject = (x) => x !== null && typeof x === "object";
22
+ export const isString = (x) => typeof x === "string";
23
+ export const isNumber = (x) => typeof x === 'number';
24
+ export const isBigInt = (x) => typeof x === "bigint";
25
+ export const isPositive = (x) => x > 0;
26
+ export const isTs = (p) => and(Array.isArray, (xs) => xs.every(p));
27
+ export const isclose = (a, b, eps = Number.EPSILON) => Math.abs(a - b) < eps;
28
+ export const allclose = (a, b, eps) => a.length == b.length && a.every((x, i) => isclose(x, b[i], eps));
29
+ export const equal = (a, b) => a == b;
30
+ export const strictEqual = (a, b) => a === b;
31
+ export const isDiffPrev = (eq = (strictEqual)) => comp(retain(2), not(apply(eq)));
32
+ export const isLonger = (delta) => comp((x) => [performance.now(), x], retain(2), (xs) => xs.length < 2 || xs[1][0] - xs[0][0] > delta);
33
+ export const isFunction = (x) => typeof x === "function";
34
+ export const isAborted = (signal) => () => signal.aborted;
35
+ export const isArrayBufferLike = (x) => x instanceof ArrayBuffer || x instanceof SharedArrayBuffer;
36
+ export const isArrayBufferView = (x) => x != null &&
37
+ isArrayBufferLike(x.buffer) &&
38
+ isNumber(x.byteOffset) &&
39
+ isNumber(x.byteLength);
40
+ export const isEmpty = (x) => x.length == 0;
41
+ export const counted = (n) => {
42
+ let m = 0;
43
+ return () => m++ < n;
44
+ };
@@ -0,0 +1,5 @@
1
+ export declare const EMPTY: Readonly<{
2
+ OBJ: {};
3
+ STR: "";
4
+ LIST: never[];
5
+ }>;
@@ -0,0 +1 @@
1
+ export const EMPTY = Object.freeze({ OBJ: {}, STR: "", LIST: [] });