@cgtk/std 0.0.192 → 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 (128) 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 +3 -3
  6. package/async.js +5 -4
  7. package/basen.js +1 -1
  8. package/buffer.d.ts +1 -1
  9. package/buffer.js +1 -1
  10. package/checks.d.ts +2 -2
  11. package/checks.js +2 -2
  12. package/dom.d.ts +2 -2
  13. package/dom.js +3 -3
  14. package/fn.d.ts +3 -3
  15. package/fn.js +3 -3
  16. package/http.d.ts +6 -7
  17. package/http.js +11 -15
  18. package/iterable.d.ts +1 -1
  19. package/iterable.js +3 -3
  20. package/json.d.ts +1 -1
  21. package/json.js +1 -1
  22. package/map.d.ts +3 -3
  23. package/map.js +2 -2
  24. package/math.js +1 -1
  25. package/npy.d.ts +1 -1
  26. package/number.js +1 -1
  27. package/object.d.ts +2 -2
  28. package/package.json +2 -2
  29. package/port.d.ts +2 -1
  30. package/port.js +7 -4
  31. package/progress.d.ts +1 -1
  32. package/rect.d.ts +1 -1
  33. package/rect.js +1 -1
  34. package/resource.d.ts +10 -8
  35. package/resource.js +22 -15
  36. package/schedule.d.ts +1 -1
  37. package/schedule.js +2 -2
  38. package/set.d.ts +1 -0
  39. package/set.js +1 -0
  40. package/signal.d.ts +18 -5
  41. package/signal.js +56 -15
  42. package/src/array.d.ts +14 -0
  43. package/src/array.js +21 -0
  44. package/src/assign.d.ts +8 -0
  45. package/src/assign.js +26 -0
  46. package/src/async.d.ts +8 -0
  47. package/src/async.js +31 -0
  48. package/src/base64.d.ts +1 -0
  49. package/src/base64.js +7 -0
  50. package/src/basen.d.ts +12 -0
  51. package/src/basen.js +31 -0
  52. package/src/buffer.d.ts +14 -0
  53. package/src/buffer.js +54 -0
  54. package/src/checks.d.ts +29 -0
  55. package/src/checks.js +44 -0
  56. package/src/constants.d.ts +5 -0
  57. package/src/constants.js +1 -0
  58. package/src/dom.d.ts +50 -0
  59. package/src/dom.js +36 -0
  60. package/src/fn.d.ts +33 -0
  61. package/src/fn.js +61 -0
  62. package/src/http.d.ts +9 -0
  63. package/src/http.js +33 -0
  64. package/src/index.d.ts +1 -0
  65. package/src/index.js +1 -0
  66. package/src/iterable.d.ts +43 -0
  67. package/src/iterable.js +158 -0
  68. package/src/json.d.ts +1 -0
  69. package/src/json.js +1 -0
  70. package/src/map.d.ts +12 -0
  71. package/src/map.js +35 -0
  72. package/src/math.d.ts +34 -0
  73. package/src/math.js +43 -0
  74. package/src/npy.d.ts +10 -0
  75. package/src/npy.js +29 -0
  76. package/src/number.d.ts +4 -0
  77. package/src/number.js +13 -0
  78. package/src/object.d.ts +15 -0
  79. package/src/object.js +13 -0
  80. package/src/port.d.ts +30 -0
  81. package/src/port.js +56 -0
  82. package/src/progress.d.ts +6 -0
  83. package/src/progress.js +1 -0
  84. package/src/rect.d.ts +5 -0
  85. package/src/rect.js +16 -0
  86. package/src/resource.d.ts +12 -0
  87. package/src/resource.js +28 -0
  88. package/src/schedule.d.ts +10 -0
  89. package/src/schedule.js +33 -0
  90. package/src/set.d.ts +2 -0
  91. package/src/set.js +2 -0
  92. package/src/signal.d.ts +37 -0
  93. package/src/signal.js +89 -0
  94. package/src/stream.d.ts +6 -0
  95. package/src/stream.js +26 -0
  96. package/src/string.d.ts +8 -0
  97. package/src/string.js +29 -0
  98. package/src/struct.d.ts +42 -0
  99. package/src/struct.js +58 -0
  100. package/src/tree.d.ts +11 -0
  101. package/src/tree.js +25 -0
  102. package/src/treemap.d.ts +13 -0
  103. package/src/treemap.js +71 -0
  104. package/src/typedarray.d.ts +31 -0
  105. package/src/typedarray.js +39 -0
  106. package/src/types.d.ts +63 -0
  107. package/src/types.js +1 -0
  108. package/src/utils.d.ts +4 -0
  109. package/src/utils.js +30 -0
  110. package/src/weak.d.ts +2 -0
  111. package/src/weak.js +5 -0
  112. package/stream.d.ts +1 -1
  113. package/stream.js +2 -2
  114. package/string.d.ts +2 -1
  115. package/string.js +3 -2
  116. package/struct.d.ts +2 -2
  117. package/struct.js +2 -2
  118. package/tree.d.ts +3 -3
  119. package/tree.js +8 -10
  120. package/treemap.d.ts +12 -12
  121. package/treemap.js +62 -59
  122. package/typedarray.d.ts +1 -1
  123. package/typedarray.js +3 -3
  124. package/types.d.ts +3 -7
  125. package/utils.d.ts +1 -1
  126. package/utils.js +1 -1
  127. package/weak.d.ts +2 -2
  128. package/weak.js +1 -1
package/signal.js CHANGED
@@ -1,33 +1,74 @@
1
- import { after, constantly, noop, bind, comp, forEach } from "./fn";
2
- import { mapValues } from "./object";
3
- import { isDefined } 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;
8
8
  return g;
9
9
  };
10
10
  export const signal = (fns = new Set()) => create((x) => fns.forEach(f => f(x)), bind(add, fns));
11
+ export const map = (source, f) => g => source(comp(f, g));
12
+ export const filter = (source, p) => f => source(filter_(p, f));
13
+ export const cache = (source) => {
14
+ let x;
15
+ source(y => x = y);
16
+ return f => {
17
+ if (isDefined(x))
18
+ f(x);
19
+ return source(f);
20
+ };
21
+ };
11
22
  ;
12
- export const value = (x) => {
13
- const sig = signal();
23
+ ;
24
+ const isValueLike = (v) => Object.hasOwn(v, "value") && Object.hasOwn(v, "on") && isFunction(v.on);
25
+ export const value = (x, fs = new Set()) => {
26
+ const sig = signal(fs);
14
27
  const notify = after(() => sig(x));
15
28
  const op = (f) => notify(f(x));
29
+ const on = (f) => (f(x), sig.on(f));
30
+ const ops = (f) => mapValues(f(x), notify);
16
31
  return {
17
32
  get value() { return x; },
18
33
  set value(y) { sig(x = y); },
19
- op,
20
- on: (f) => (f(x), sig.on(f)),
21
- ops: (f) => mapValues(f(x), notify),
34
+ op, on, ops,
22
35
  set: op(constantly((y) => x = y)),
23
- update: op(constantly(noop))
36
+ update: op(constantly(noop)),
37
+ map: bind(map, on),
38
+ box: (f) => ({
39
+ on,
40
+ get value() { return x; },
41
+ ops: ops(f),
42
+ })
24
43
  };
25
44
  };
26
- export const values = (vals) => mapValues(vals, value);
27
- export const map = (source, f) => g => source(comp(f, g));
28
- export const merge = (vals) => f => {
29
- const sub = () => f(mapValues(vals, (v) => v.value));
30
- return forEach(...Object.values(vals).map((v) => v.on(sub)));
45
+ export const values = (vals) => mapValues(vals, v => value(v));
46
+ export const compose = (xs, transform = identity) => f => {
47
+ const collect = (vs) => {
48
+ if (isValueLike(vs)) {
49
+ return vs.value;
50
+ }
51
+ else if (Array.isArray(vs)) {
52
+ return vs.map(collect);
53
+ }
54
+ return Object.fromEntries(Object.entries(vs).map(([k, v]) => [k, collect(v)]));
55
+ };
56
+ const notify = () => f(transform(collect(xs)));
57
+ let ready = false;
58
+ const sub = (vs) => {
59
+ if (isValueLike(vs)) {
60
+ return vs.on(() => { if (ready)
61
+ notify(); });
62
+ }
63
+ else if (Array.isArray(vs)) {
64
+ return forEach(...vs.map(sub));
65
+ }
66
+ return forEach(...Object.values(vs).map(sub));
67
+ };
68
+ const off = sub(xs);
69
+ ready = true;
70
+ notify();
71
+ return off;
31
72
  };
32
73
  export const mux = (init) => {
33
74
  const sigs = {};
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: [] });
package/src/dom.d.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { Fn, Dispose, RecordOf, Maybe } from "./types.js";
2
+ export declare const customEvent: <T>(type: string, detail: T, { composed, bubbles, cancelable }?: EventInit) => CustomEvent<T>;
3
+ type ListenOpts = boolean | AddEventListenerOptions;
4
+ export declare const on: {
5
+ <T extends HTMLElement, K extends keyof HTMLElementEventMap>(el: T, name: K, listener: Fn<HTMLElementEventMap[K]>, opts?: ListenOpts): Dispose;
6
+ <T extends SVGGraphicsElement, K extends keyof SVGElementEventMap>(el: T, name: K, listener: Fn<SVGElementEventMap[K]>, opts?: ListenOpts): Dispose;
7
+ <T, K extends keyof MessageEventTargetEventMap>(el: MessageEventTarget<T>, name: K, listener: Fn<MessageEventTargetEventMap[K]>, opts?: ListenOpts): Dispose;
8
+ <K extends keyof DocumentEventMap>(el: Document, name: K, listener: Fn<DocumentEventMap[K]>, opts?: ListenOpts): Dispose;
9
+ <K extends keyof WindowEventMap>(el: Window, name: K, listener: Fn<WindowEventMap[K]>, opts?: ListenOpts): Dispose;
10
+ <K extends keyof WorkerEventMap>(el: Worker, name: K, listener: Fn<WorkerEventMap[K]>, opts?: ListenOpts): Dispose;
11
+ <K extends keyof AbortSignalEventMap>(el: AbortSignal, name: K, listener: Fn<AbortSignalEventMap[K]>, opts?: ListenOpts): Dispose;
12
+ };
13
+ export declare const onAll: {
14
+ <T extends keyof HTMLElementEventMap>(el: HTMLElement, listeners: {
15
+ [k in T]: Fn<HTMLElementEventMap[k]>;
16
+ }, opts?: ListenOpts): Dispose;
17
+ <T extends keyof SVGElementEventMap>(el: SVGGraphicsElement, listeners: {
18
+ [k in T]: Fn<SVGElementEventMap[k]>;
19
+ }, opts?: ListenOpts): Dispose;
20
+ <T, K extends keyof MessageEventTargetEventMap>(el: MessageEventTarget<T>, listeners: {
21
+ [k in K]: Fn<MessageEventTargetEventMap[k]>;
22
+ }, opts?: ListenOpts): Dispose;
23
+ <T extends keyof DocumentEventMap>(el: Document, listeners: {
24
+ [k in T]: Fn<DocumentEventMap[k]>;
25
+ }, opts?: ListenOpts): Dispose;
26
+ <T extends keyof WindowEventMap>(el: Window, listeners: {
27
+ [k in T]: Fn<WindowEventMap[k]>;
28
+ }, opts?: ListenOpts): Dispose;
29
+ <T extends keyof WorkerEventMap>(el: Worker, listeners: {
30
+ [k in T]: Fn<WorkerEventMap[k]>;
31
+ }, opts?: ListenOpts): Dispose;
32
+ <T extends keyof AbortSignalEventMap>(el: AbortSignal, listeners: {
33
+ [k in T]: Fn<AbortSignalEventMap[k]>;
34
+ }, opts?: ListenOpts): Dispose;
35
+ };
36
+ type DragHandlers = Record<'down' | 'move' | 'up', Fn<PointerEvent>>;
37
+ export declare const onDrag: {
38
+ (el: HTMLElement, fns: Partial<DragHandlers>): Dispose;
39
+ (el: SVGGraphicsElement, fns: Partial<DragHandlers>): Dispose;
40
+ };
41
+ export declare const onDragMove: {
42
+ (el: HTMLElement, f: Fn<PointerEvent>): Dispose;
43
+ (el: SVGGraphicsElement, f: Fn<PointerEvent>): Dispose;
44
+ };
45
+ export declare const onResize: (el: Element, f: Fn<ResizeObserverEntry[]>, options?: ResizeObserverOptions) => Dispose;
46
+ export declare const preventDefault: <T extends Event>(e: T) => void;
47
+ export declare const loadImage: (img: HTMLImageElement, src: string) => Promise<HTMLImageElement>;
48
+ export declare const pushState: (path: string, query?: RecordOf<string>, data?: any) => void;
49
+ export declare const qs: (search?: string) => Maybe<RecordOf<string>>;
50
+ export {};
package/src/dom.js ADDED
@@ -0,0 +1,36 @@
1
+ import { forEach, noop } from "./fn.js";
2
+ import { isDefined } from "./checks.js";
3
+ import { EMPTY } from "./constants.js";
4
+ export const customEvent = (type, detail, { composed = false, bubbles = false, cancelable = false } = EMPTY.OBJ) => new CustomEvent(type, { bubbles, cancelable, composed, detail });
5
+ export const on = (el, name, listener, opts = false) => {
6
+ el.addEventListener(name, listener, opts);
7
+ return () => el.removeEventListener(name, listener, opts);
8
+ };
9
+ export const onAll = (el, fs, opts) => forEach(...Object.keys(fs).map(k => on(el, k, fs[k], opts)));
10
+ export const onDrag = (el, { down = noop, move = noop, up = noop }) => on(el, 'pointerdown', e => {
11
+ const pointerId = e.pointerId;
12
+ el.setPointerCapture(pointerId);
13
+ down(e);
14
+ const offMove = on(el, 'pointermove', move);
15
+ const offUp = on(el, 'pointerup', e => {
16
+ up(e);
17
+ el.releasePointerCapture(pointerId);
18
+ offMove();
19
+ offUp();
20
+ });
21
+ });
22
+ export const onDragMove = (el, move) => onDrag(el, { move });
23
+ export const onResize = (el, f, options) => {
24
+ const ro = new ResizeObserver(f);
25
+ ro.observe(el, options);
26
+ return () => ro.disconnect();
27
+ };
28
+ export const preventDefault = (e) => e.preventDefault();
29
+ export const loadImage = (img, src) => new Promise((resolve) => {
30
+ const off = on(img, "load", () => (off(), resolve(img)));
31
+ img.src = src;
32
+ });
33
+ export const pushState = (path, query, data) => history.pushState(data, EMPTY.STR, isDefined(query) ? `${path}?${new URLSearchParams(query).toString()}` : path);
34
+ export const qs = (search = location.search) => search.length > 0
35
+ ? Object.fromEntries(new URLSearchParams(search).entries())
36
+ : undefined;
package/src/fn.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import type { Fn, FnV, FnF, Pred, TPred, FirstParam, Last, Constantly, Maybe, Dispose, Reactive } from "./types.js";
2
+ export declare const tee: <T>(f: Fn<T, void>) => Fn<T, T>;
3
+ export declare const comp: <Fs extends Fn[]>(...fs: Fs) => (x: FirstParam<Fs[0]>) => Last<Fs> extends Fn<any, infer R> ? R : never;
4
+ export declare const pipe: <T, Fs extends [Fn<T>, ...Fn[]]>(x: T, ...fs: Fs) => Last<Fs> extends Fn<any, infer R> ? R : never;
5
+ export declare const forEach: <T, R = any>(...fs: Fn<T, R>[]) => (x: T) => void;
6
+ export declare const map: <T, R>(...fs: Fn<T, R>[]) => (x: T) => R[];
7
+ export declare const and: <T>(...fs: Pred<T>[]) => (x: T) => boolean;
8
+ export declare const or: <T>(...fs: Pred<T>[]) => (x: T) => boolean;
9
+ export declare const not: <T>(p: Pred<T>) => (x: T) => boolean;
10
+ export declare const comb: <C extends FnV<Fn[]>>(c: C) => (...fs: Fn<FirstParam<FirstParam<C>>, ReturnType<FirstParam<C>>>[]) => (x: FirstParam<FirstParam<C>>) => ReturnType<C>;
11
+ export declare const noop: () => void;
12
+ export declare const identity: <S, T = S>(x: S) => T;
13
+ export declare const constantly: <T>(x: T) => Constantly<T>;
14
+ export declare const apply: <F extends FnV>(f: F) => (x: Parameters<F>) => ReturnType<F>;
15
+ export declare const call: <T extends unknown[]>(f: Fn<T>) => (...xs: T) => ReturnType<typeof f>;
16
+ export declare const bind: <A extends unknown[], B extends unknown[], R>(f: (...args: [...A, ...B]) => R, ...args: A) => (...args: B) => R;
17
+ export declare const lazy: <F extends FnV>(f: F, ...xs: Parameters<F>) => Fn<void, ReturnType<F>>;
18
+ export declare const where: <T, U, V>(p: Pred<T>, f: Fn<T, U>, g: Fn<T, V>) => (x: T) => U | V;
19
+ export declare const before: <F extends Fn<void>>(f: F) => FnF;
20
+ export declare const after: <F extends Fn<void>>(f: F) => FnF;
21
+ export declare const mapForEach: <T, R>(...fs: Fn<T, Fn<R>>[]) => (x: T) => (x: R) => void;
22
+ export declare const filter: <T>(p: Pred<T>, f: Fn<T>) => (x: T) => void;
23
+ export declare const filterT: <T>(p: TPred<T>, f: Fn<T>) => <S>(x: S) => void;
24
+ export declare const scan: <T, S>(f: FnV<[S, T], S>, a: S) => Fn<T, S>;
25
+ export declare const memoize: <T extends any[], K, V>(f: FnV<T, V>, key?: FnV<T, K>, cache?: Map<K, V>) => (...xs: T) => NonNullable<V>;
26
+ export declare const abortable: <T = any>(f: Fn<AbortSignal>) => (reason?: T) => void;
27
+ export declare const disposable: (f: Fn<Fn<Dispose, Dispose>>, disposes?: Set<Dispose>) => Dispose;
28
+ export declare const disposing: () => <F extends FnV<any[], Maybe<Dispose>>>(f: F) => FnV<Parameters<F>, Dispose>;
29
+ export declare const disposeWith: <T>(f: Fn<T, Dispose>) => (g: Fn<Fn<T>>) => Dispose;
30
+ export declare const idemp: <F extends FnV<any[], Dispose | void>>(d: Fn<Dispose, Dispose>, f: F) => FnV<Parameters<F>, Dispose>;
31
+ export declare const toggler: (f: Fn<void, Dispose>) => (x: boolean) => Dispose;
32
+ export declare const voidify: <F extends FnV>(f: F) => (...xs: Parameters<F>) => void;
33
+ export declare const effectual: <T>(f: Reactive<T>, g: Fn<T>) => Dispose;
package/src/fn.js ADDED
@@ -0,0 +1,61 @@
1
+ import { add } from "./set.js";
2
+ export const tee = f => x => (f(x), x);
3
+ export const comp = (...fs) => (x) => fs.reduce((x, f) => f(x), x);
4
+ export const pipe = (x, ...fs) => fs.reduce((x, f) => f(x), x);
5
+ export const forEach = (...fs) => (x) => fs.forEach(f => f(x));
6
+ export const map = (...fs) => (x) => fs.map(f => f(x));
7
+ export const and = (...fs) => (x) => fs.reduce((a, f) => a && f(x), true);
8
+ export const or = (...fs) => (x) => fs.reduce((a, f) => a || f(x), false);
9
+ export const not = (p) => (x) => !p(x);
10
+ export const comb = (c) => (...fs) => (x) => c(...fs.map(f => f(x)));
11
+ export const noop = () => { };
12
+ export const identity = (x) => x;
13
+ export const constantly = (x) => () => x;
14
+ export const apply = (f) => (x) => f.apply(null, x);
15
+ export const call = (f) => (...xs) => f(xs);
16
+ export const bind = (f, ...args) => f.bind(null, ...args);
17
+ export const lazy = (f, ...xs) => () => f(...xs);
18
+ export const where = (p, f, g) => (x) => p(x) ? f(x) : g(x);
19
+ export const before = (f) => g => (...xs) => (f(), g(...xs));
20
+ export const after = (f) => g => (...xs) => {
21
+ const r = g(...xs);
22
+ f();
23
+ return r;
24
+ };
25
+ export const mapForEach = (...fs) => comp(map(...fs), apply((forEach)));
26
+ export const filter = (p, f) => (x) => { if (p(x))
27
+ f(x); };
28
+ export const filterT = (p, f) => (x) => { if (p(x))
29
+ f(x); };
30
+ export const scan = (f, a) => (x) => a = f(a, x);
31
+ export const memoize = (f, key = (...xs) => xs[0], cache = new Map()) => (...xs) => {
32
+ const k = key(...xs);
33
+ if (!cache.has(k))
34
+ cache.set(k, f(...xs));
35
+ return cache.get(k);
36
+ };
37
+ export const abortable = (f) => {
38
+ const ctrl = new AbortController();
39
+ f(ctrl.signal);
40
+ return (reason) => ctrl.abort(reason);
41
+ };
42
+ export const disposable = (f, disposes = new Set()) => {
43
+ f(bind(add, disposes));
44
+ return () => (disposes.forEach(g => g()), disposes.clear());
45
+ };
46
+ export const disposing = () => {
47
+ let dispose = noop;
48
+ const stop = () => dispose();
49
+ return (f) => (...xs) => (stop(), dispose = f(...xs) ?? noop, stop);
50
+ };
51
+ export const disposeWith = (f) => (g) => disposable((d) => g(comp(f, d)));
52
+ export const idemp = (d, f) => disposing()((...xs) => {
53
+ const cleanup = f(...xs) ?? noop;
54
+ return forEach(cleanup, d(cleanup));
55
+ });
56
+ export const toggler = (f) => {
57
+ let cancel = noop;
58
+ return (x) => (cancel(), (cancel = x ? f() : noop));
59
+ };
60
+ export const voidify = (f) => (...xs) => { f(...xs); };
61
+ export const effectual = (f, g) => disposable(d => d(f(idemp(d, g))));
package/src/http.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export declare const assertStatus: (status: number) => (resp: Response) => void;
2
+ export declare const contentLength: (response: Response) => number;
3
+ export declare const fetchBuffer: (url: string | URL | Request, init?: RequestInit) => Promise<ArrayBuffer>;
4
+ export declare const fetchText: (url: string | URL | Request, init?: RequestInit) => Promise<string>;
5
+ export declare const fetchDOM: (url: string | URL | Request, init?: RequestInit) => Promise<HTMLElement>;
6
+ export declare const fetchImage: (src: string) => Promise<HTMLImageElement>;
7
+ export declare const fetchImageBitmap: (url: string | URL | Request, init?: RequestInit) => Promise<ImageBitmap>;
8
+ export declare const fetchJSON: (url: string | URL | Request, init?: RequestInit) => Promise<any>;
9
+ export declare const fetchDataUrl: (url: string | URL | Request, init?: RequestInit) => Promise<string>;
package/src/http.js ADDED
@@ -0,0 +1,33 @@
1
+ import { tee } from "./fn.js";
2
+ import { assert } from "./checks.js";
3
+ export const assertStatus = (status) => (resp) => assert(resp.status == status, `Invalid HTTP Status ${resp.status}: ${resp.url}`);
4
+ export const contentLength = (response) => {
5
+ const cl = response.headers.get("content-length");
6
+ return cl ? parseInt(cl) : -1;
7
+ };
8
+ export const fetchBuffer = (url, init) => fetch(url, init)
9
+ .then(tee(assertStatus(200)))
10
+ .then(resp => resp.blob())
11
+ .then(blob => blob.arrayBuffer());
12
+ export const fetchText = (url, init) => fetchBuffer(url, init).then(buf => new TextDecoder("utf-8").decode(buf));
13
+ export const fetchDOM = (url, init) => fetchText(url, init).then(txt => new DOMParser().parseFromString(txt, "image/svg+xml").documentElement);
14
+ export const fetchImage = (src) => new Promise((resolve, reject) => {
15
+ const image = new Image();
16
+ image.crossOrigin = "anonymous";
17
+ image.src = src;
18
+ image.onload = () => resolve(image);
19
+ image.onerror = reject;
20
+ });
21
+ export const fetchImageBitmap = async (url, init) => fetch(url, init)
22
+ .then(tee(assertStatus(200)))
23
+ .then(resp => resp.blob())
24
+ .then(blob => createImageBitmap(blob, { colorSpaceConversion: "none" }));
25
+ export const fetchJSON = (url, init) => fetchText(url, init).then(JSON.parse);
26
+ export const fetchDataUrl = (url, init) => fetch(url, init)
27
+ .then(tee(assertStatus(200)))
28
+ .then(resp => resp.blob())
29
+ .then(blob => new Promise(resolve => {
30
+ const f = new FileReader();
31
+ f.addEventListener("load", () => resolve(f.result));
32
+ f.readAsDataURL(blob);
33
+ }));