@cgtk/std 0.0.182
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/array.d.ts +12 -0
- package/array.js +19 -0
- package/assign.d.ts +8 -0
- package/assign.js +26 -0
- package/async.d.ts +5 -0
- package/async.js +27 -0
- package/basen.d.ts +12 -0
- package/basen.js +31 -0
- package/buffer.d.ts +14 -0
- package/buffer.js +54 -0
- package/checks.d.ts +26 -0
- package/checks.js +37 -0
- package/constants.d.ts +5 -0
- package/constants.js +1 -0
- package/dom.d.ts +39 -0
- package/dom.js +31 -0
- package/fn.d.ts +29 -0
- package/fn.js +56 -0
- package/http.d.ts +9 -0
- package/http.js +32 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/iterable.d.ts +41 -0
- package/iterable.js +142 -0
- package/json.d.ts +1 -0
- package/json.js +1 -0
- package/map.d.ts +13 -0
- package/map.js +36 -0
- package/math.d.ts +31 -0
- package/math.js +39 -0
- package/number.d.ts +2 -0
- package/number.js +5 -0
- package/object.d.ts +13 -0
- package/object.js +11 -0
- package/package.json +44 -0
- package/progress.d.ts +6 -0
- package/progress.js +1 -0
- package/schedule.d.ts +8 -0
- package/schedule.js +44 -0
- package/signal.d.ts +19 -0
- package/signal.js +25 -0
- package/stream.d.ts +6 -0
- package/stream.js +26 -0
- package/string.d.ts +5 -0
- package/string.js +22 -0
- package/struct.d.ts +25 -0
- package/struct.js +58 -0
- package/tree.d.ts +20 -0
- package/tree.js +33 -0
- package/treemap.d.ts +13 -0
- package/treemap.js +68 -0
- package/typedarray.d.ts +23 -0
- package/typedarray.js +44 -0
- package/types.d.ts +42 -0
- package/types.js +1 -0
- package/utils.d.ts +4 -0
- package/utils.js +30 -0
- package/weak.d.ts +2 -0
- package/weak.js +5 -0
package/array.d.ts
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { Fn, Tuple, Numbers } from "./types.js";
|
2
|
+
export declare const push: <T>(xs: T[], x: T) => T[];
|
3
|
+
export declare const unshift: <T>(xs: T[], x: T) => T[];
|
4
|
+
export declare const pop: <T>(xs: T[]) => T[];
|
5
|
+
export declare const shift: <T>(xs: T[]) => T[];
|
6
|
+
export declare const first: <T>(xs: T[]) => T;
|
7
|
+
export declare const cap: <T>(n: number, f: Fn<T[]>) => Fn<T[], T[]>;
|
8
|
+
export declare const retain: <T>(n: number, xs?: T[]) => (x: T) => T[];
|
9
|
+
export declare const tuple: <T, N extends number>(...xs: T[] & {
|
10
|
+
length: N;
|
11
|
+
}) => Tuple<T, N>;
|
12
|
+
export declare const seek: <T>(arr: T[] | Numbers, vals: Set<T | number>, offset?: number) => number;
|
package/array.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
import { comp, scan } from "./fn.js";
|
2
|
+
export const push = (xs, x) => (xs.push(x), xs);
|
3
|
+
export const unshift = (xs, x) => (xs.unshift(x), xs);
|
4
|
+
export const pop = (xs) => (xs.pop(), xs);
|
5
|
+
export const shift = (xs) => (xs.shift(), xs);
|
6
|
+
export const first = (xs) => xs[0];
|
7
|
+
export const cap = (n, f) => xs => {
|
8
|
+
while (xs.length > n)
|
9
|
+
f(xs);
|
10
|
+
return xs;
|
11
|
+
};
|
12
|
+
export const retain = (n, xs = []) => comp(scan(push)(xs), cap(n, shift));
|
13
|
+
export const tuple = (...xs) => xs;
|
14
|
+
export const seek = (arr, vals, offset = 0) => {
|
15
|
+
for (let i = offset, n = arr.length; i < n; i++)
|
16
|
+
if (vals.has(arr[i]))
|
17
|
+
return i;
|
18
|
+
return -1;
|
19
|
+
};
|
package/assign.d.ts
ADDED
@@ -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/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/async.d.ts
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
import type { Fn, Constantly, Optional } from "./types.js";
|
2
|
+
export declare const timeout: (ms: number, signal?: AbortSignal) => Promise<unknown>;
|
3
|
+
export declare const delay: (ms: number) => <T>(x: T) => Promise<T>;
|
4
|
+
export declare const probing: <T>(p: Promise<T>) => Constantly<Optional<T>>;
|
5
|
+
export declare const resolved: <T>(p: Promise<T>) => (f: Fn<T>) => () => void;
|
package/async.js
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import { filterT } 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
|
+
setTimeout(() => {
|
8
|
+
if (signal?.aborted)
|
9
|
+
reject(signal.reason);
|
10
|
+
else
|
11
|
+
resolve(undefined);
|
12
|
+
}, ms);
|
13
|
+
}
|
14
|
+
});
|
15
|
+
export const delay = (ms) => async (x) => (await timeout(ms), x);
|
16
|
+
export const probing = (p) => {
|
17
|
+
let value = undefined;
|
18
|
+
p.then(x => value = x);
|
19
|
+
return () => value;
|
20
|
+
};
|
21
|
+
export const resolved = (p) => {
|
22
|
+
const val = probing(p);
|
23
|
+
return (f) => {
|
24
|
+
const g = (filterT((isDefined), f));
|
25
|
+
return () => g(val());
|
26
|
+
};
|
27
|
+
};
|
package/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/basen.js
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
import { toFixedMax } from "./number";
|
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)}`;
|
package/buffer.d.ts
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
import type { Fn } from "./types";
|
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/buffer.js
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
import { readLine } from './string';
|
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
|
+
};
|
package/checks.d.ts
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
import type { TypedArray, TypedArrayConstructor, Pred, TPred, FnV, Optional, Numbers } from "./types";
|
2
|
+
export declare const isTypedArray: (x: any) => x is TypedArray;
|
3
|
+
export declare const isTypedArrayConstructor: (x: any) => x is TypedArrayConstructor;
|
4
|
+
export declare const isUndefined: (x: any) => x is undefined;
|
5
|
+
export declare const isNull: (x: any) => x is null;
|
6
|
+
export declare const isDefined: <T>(x: Optional<T>) => x is T;
|
7
|
+
export declare function assert(condition: boolean, msg: string): asserts condition;
|
8
|
+
export declare function assertType<T>(x: any, pred: TPred<T>, msg: string): asserts x is T;
|
9
|
+
export declare const isObject: TPred<Object>;
|
10
|
+
export declare const isString: TPred<string>;
|
11
|
+
export declare const isNumber: TPred<number>;
|
12
|
+
export declare const isPositive: Pred<number>;
|
13
|
+
export declare const isTs: <T>(p: TPred<T>) => TPred<T[]>;
|
14
|
+
export declare const isclose: (a: number, b: number, eps?: number) => boolean;
|
15
|
+
export declare const allclose: (a: Numbers, b: Numbers, eps?: number) => boolean;
|
16
|
+
export declare const ternary: <A, B, C>(f: FnV<[A, B], C>) => (a: Optional<A>, b: Optional<B>) => C;
|
17
|
+
export declare const equal: <T>(a: T, b?: T) => boolean;
|
18
|
+
export declare const strictEqual: <T>(a: T, b?: T) => boolean;
|
19
|
+
export declare const isDiffPrev: <T>(eq?: (a: T, b?: T | undefined) => boolean) => Pred<T>;
|
20
|
+
export declare const isFunction: <T extends Function>(x: any) => x is T;
|
21
|
+
export declare const isAborted: (signal: AbortSignal) => () => boolean;
|
22
|
+
export declare const isArrayBufferLike: (x: any) => x is ArrayBufferLike;
|
23
|
+
export declare const isArrayBufferView: (x: any) => x is ArrayBufferView;
|
24
|
+
export declare const isEmpty: (x: {
|
25
|
+
length: number;
|
26
|
+
}) => boolean;
|
package/checks.js
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
import { comp, not, apply, and } from "./fn";
|
2
|
+
import { retain } from "./array";
|
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 Float32Array || x instanceof Float64Array);
|
7
|
+
export const isTypedArrayConstructor = (x) => x && (x == Uint8Array || x == Uint8ClampedArray ||
|
8
|
+
x == Uint16Array || x == Uint32Array ||
|
9
|
+
x == Int8Array || x == Int16Array || x == Int32Array ||
|
10
|
+
x == Float32Array || x == Float64Array);
|
11
|
+
export const isUndefined = (x) => x === undefined;
|
12
|
+
export const isNull = (x) => x === null;
|
13
|
+
export const isDefined = (x) => !isUndefined(x);
|
14
|
+
export function assert(condition, msg) {
|
15
|
+
if (!condition)
|
16
|
+
throw new Error(msg);
|
17
|
+
}
|
18
|
+
export function assertType(x, pred, msg) { assert(pred(x), msg); }
|
19
|
+
export const isObject = (x) => x !== null && typeof x === "object";
|
20
|
+
export const isString = (x) => typeof x === "string";
|
21
|
+
export const isNumber = (x) => typeof x === 'number';
|
22
|
+
export const isPositive = (x) => x > 0;
|
23
|
+
export const isTs = (p) => and(Array.isArray, (xs) => xs.every(p));
|
24
|
+
export const isclose = (a, b, eps = Number.EPSILON) => Math.abs(a - b) < eps;
|
25
|
+
export const allclose = (a, b, eps) => a.length == b.length && a.every((x, i) => isclose(x, b[i], eps));
|
26
|
+
export const ternary = (f) => (a, b) => ((!isUndefined(a) && !isUndefined(b)) ? f(a, b) : isUndefined(a) ? b : a);
|
27
|
+
export const equal = (a, b) => a == b;
|
28
|
+
export const strictEqual = (a, b) => a === b;
|
29
|
+
export const isDiffPrev = (eq = (strictEqual)) => comp(retain(2), not(apply(eq)));
|
30
|
+
export const isFunction = (x) => typeof x === "function";
|
31
|
+
export const isAborted = (signal) => () => signal.aborted;
|
32
|
+
export const isArrayBufferLike = (x) => x instanceof ArrayBuffer || x instanceof SharedArrayBuffer;
|
33
|
+
export const isArrayBufferView = (x) => x != null &&
|
34
|
+
isArrayBufferLike(x.buffer) &&
|
35
|
+
isNumber(x.byteOffset) &&
|
36
|
+
isNumber(x.byteLength);
|
37
|
+
export const isEmpty = (x) => x.length == 0;
|
package/constants.d.ts
ADDED
package/constants.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export const EMPTY = Object.freeze({ OBJ: {}, STR: "", LIST: [] });
|
package/dom.d.ts
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
import type { Fn } from "./types.js";
|
2
|
+
export declare const customEvent: <T>(type: string, detail: T, { composed, bubbles, cancelable }?: EventInit) => CustomEvent<T>;
|
3
|
+
export declare const on: {
|
4
|
+
<T extends HTMLElement, K extends keyof HTMLElementEventMap>(el: T, name: K, listener: Fn<HTMLElementEventMap[K]>, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
5
|
+
<T extends SVGGraphicsElement, K extends keyof SVGElementEventMap>(el: T, name: K, listener: Fn<SVGElementEventMap[K]>, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
6
|
+
<K extends keyof DocumentEventMap>(el: Document, name: K, listener: Fn<DocumentEventMap[K]>, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
7
|
+
<K extends keyof WindowEventMap>(el: Window, name: K, listener: Fn<WindowEventMap[K]>, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
8
|
+
<K extends keyof AbortSignalEventMap>(el: AbortSignal, name: K, listener: Fn<AbortSignalEventMap[K]>, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
9
|
+
};
|
10
|
+
export declare const onAll: {
|
11
|
+
<T extends keyof HTMLElementEventMap>(el: HTMLElement, listeners: {
|
12
|
+
[k in T]: Fn<HTMLElementEventMap[k]>;
|
13
|
+
}, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
14
|
+
<T extends keyof SVGElementEventMap>(el: SVGGraphicsElement, listeners: {
|
15
|
+
[k in T]: Fn<SVGElementEventMap[k]>;
|
16
|
+
}, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
17
|
+
<T extends keyof DocumentEventMap>(el: Document, listeners: {
|
18
|
+
[k in T]: Fn<DocumentEventMap[k]>;
|
19
|
+
}, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
20
|
+
<T extends keyof WindowEventMap>(el: Window, listeners: {
|
21
|
+
[k in T]: Fn<WindowEventMap[k]>;
|
22
|
+
}, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
23
|
+
<T extends keyof AbortSignalEventMap>(el: AbortSignal, listeners: {
|
24
|
+
[k in T]: Fn<AbortSignalEventMap[k]>;
|
25
|
+
}, opts?: boolean | AddEventListenerOptions): Fn<void>;
|
26
|
+
};
|
27
|
+
type DragHandlers = Record<'down' | 'move' | 'up', Fn<PointerEvent>>;
|
28
|
+
export declare const onDrag: {
|
29
|
+
(el: HTMLElement, fns: Partial<DragHandlers>): Fn<void>;
|
30
|
+
(el: SVGGraphicsElement, fns: Partial<DragHandlers>): Fn<void>;
|
31
|
+
};
|
32
|
+
export declare const onDragMove: {
|
33
|
+
(el: HTMLElement, f: Fn<PointerEvent>): Fn<void>;
|
34
|
+
(el: SVGGraphicsElement, f: Fn<PointerEvent>): Fn<void>;
|
35
|
+
};
|
36
|
+
export declare const onResize: (el: Element, f: Fn<ResizeObserverEntry[]>, options?: ResizeObserverOptions) => () => void;
|
37
|
+
export declare const preventDefault: <T extends Event>(e: T) => void;
|
38
|
+
export declare const loadImage: (img: HTMLImageElement, src: string) => Promise<HTMLImageElement>;
|
39
|
+
export {};
|
package/dom.js
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
import { forEach, noop } from "./fn.js";
|
2
|
+
import { EMPTY } from "./constants.js";
|
3
|
+
export const customEvent = (type, detail, { composed = false, bubbles = false, cancelable = false } = EMPTY.OBJ) => new CustomEvent(type, { bubbles, cancelable, composed, detail });
|
4
|
+
export const on = (el, name, listener, opts = false) => {
|
5
|
+
el.addEventListener(name, listener, opts);
|
6
|
+
return () => el.removeEventListener(name, listener, opts);
|
7
|
+
};
|
8
|
+
export const onAll = (el, fs, opts) => forEach(...Object.keys(fs).map(k => on(el, k, fs[k], opts)));
|
9
|
+
export const onDrag = (el, { down = noop, move = noop, up = noop }) => on(el, 'pointerdown', e => {
|
10
|
+
const pointerId = e.pointerId;
|
11
|
+
el.setPointerCapture(pointerId);
|
12
|
+
down(e);
|
13
|
+
const offMove = on(el, 'pointermove', move);
|
14
|
+
const offUp = on(el, 'pointerup', e => {
|
15
|
+
up(e);
|
16
|
+
el.releasePointerCapture(pointerId);
|
17
|
+
offMove();
|
18
|
+
offUp();
|
19
|
+
});
|
20
|
+
});
|
21
|
+
export const onDragMove = (el, move) => onDrag(el, { move });
|
22
|
+
export const onResize = (el, f, options) => {
|
23
|
+
const ro = new ResizeObserver(f);
|
24
|
+
ro.observe(el, options);
|
25
|
+
return () => ro.disconnect();
|
26
|
+
};
|
27
|
+
export const preventDefault = (e) => e.preventDefault();
|
28
|
+
export const loadImage = (img, src) => new Promise((resolve) => {
|
29
|
+
const off = on(img, "load", () => (off(), resolve(img)));
|
30
|
+
img.src = src;
|
31
|
+
});
|
package/fn.d.ts
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
import type { Fn, FnV, FnF, Pred, TPred, FirstParam, Last, Constantly, Optional } 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 idempotent: <F extends FnV<any[], Fn<void> | any>>(f: F) => (...xs: Parameters<F>) => void;
|
19
|
+
export declare const where: <T, U, V>(p: Pred<T>, f: Fn<T, U>, g: Fn<T, V>) => (x: T) => U | V;
|
20
|
+
export declare const before: <F extends Fn<void>>(f: F) => <G extends FnV>(g: G) => (...xs: Parameters<G>) => ReturnType<G>;
|
21
|
+
export declare const after: <F extends Fn<void>>(f: F) => FnF;
|
22
|
+
export declare const mapForEach: <T, R>(...fs: Fn<T, Fn<R>>[]) => (x: T) => (x: R) => void;
|
23
|
+
export declare const filter: <T>(p: Pred<T>, f: Fn<T>) => (x: T) => void;
|
24
|
+
export declare const filterT: <T>(p: TPred<T>, f: Fn<T>) => <S>(x: S) => void;
|
25
|
+
export declare const scan: <T, S>(f: FnV<[S, T], S>) => Fn<S, Fn<T, S>>;
|
26
|
+
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>;
|
27
|
+
export declare const abortable: (f: Fn<AbortSignal>) => <T = any>(reason?: T) => void;
|
28
|
+
export declare const disposable: (f: Fn<Fn<Fn<void>, Fn<void>>>, disposes?: Set<Fn<void>>) => () => void;
|
29
|
+
export declare const disposing: () => <F extends FnV<any[], Optional<Fn<void>>>>(f: F) => (...xs: Parameters<F>) => Fn<void>;
|
package/fn.js
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
export const tee = f => x => (f(x), x);
|
2
|
+
export const comp = (...fs) => (x) => fs.reduce((x, f) => f(x), x);
|
3
|
+
export const pipe = (x, ...fs) => fs.reduce((x, f) => f(x), x);
|
4
|
+
export const forEach = (...fs) => (x) => fs.forEach(f => f(x));
|
5
|
+
export const map = (...fs) => (x) => fs.map(f => f(x));
|
6
|
+
export const and = (...fs) => (x) => fs.reduce((a, f) => a && f(x), true);
|
7
|
+
export const or = (...fs) => (x) => fs.reduce((a, f) => a || f(x), false);
|
8
|
+
export const not = (p) => (x) => !p(x);
|
9
|
+
export const comb = (c) => (...fs) => (x) => c(...fs.map(f => f(x)));
|
10
|
+
export const noop = () => { };
|
11
|
+
export const identity = (x) => x;
|
12
|
+
export const constantly = (x) => () => x;
|
13
|
+
export const apply = (f) => (x) => f.apply(null, x);
|
14
|
+
export const call = (f) => (...xs) => f(xs);
|
15
|
+
export const bind = (f, ...args) => f.bind(null, ...args);
|
16
|
+
export const lazy = (f, ...xs) => () => f(...xs);
|
17
|
+
export const idempotent = (f) => {
|
18
|
+
let clear = noop;
|
19
|
+
return (...xs) => {
|
20
|
+
clear();
|
21
|
+
const y = f(...xs);
|
22
|
+
clear = typeof y === "function" ? y : noop;
|
23
|
+
};
|
24
|
+
};
|
25
|
+
export const where = (p, f, g) => (x) => p(x) ? f(x) : g(x);
|
26
|
+
export const before = (f) => (g) => (...xs) => (f(), g(...xs));
|
27
|
+
export const after = (f) => g => (...xs) => {
|
28
|
+
const r = g(...xs);
|
29
|
+
f();
|
30
|
+
return r;
|
31
|
+
};
|
32
|
+
export const mapForEach = (...fs) => comp(map(...fs), apply((forEach)));
|
33
|
+
export const filter = (p, f) => (x) => { if (p(x))
|
34
|
+
f(x); };
|
35
|
+
export const filterT = (p, f) => (x) => { if (p(x))
|
36
|
+
f(x); };
|
37
|
+
export const scan = (f) => (a) => (x) => a = f(a, x);
|
38
|
+
export const memoize = (f, key = (...xs) => xs[0], cache = new Map()) => (...xs) => {
|
39
|
+
const k = key(...xs);
|
40
|
+
if (!cache.has(k))
|
41
|
+
cache.set(k, f(...xs));
|
42
|
+
return cache.get(k);
|
43
|
+
};
|
44
|
+
export const abortable = (f) => {
|
45
|
+
const ctrl = new AbortController();
|
46
|
+
f(ctrl.signal);
|
47
|
+
return (reason) => ctrl.abort(reason);
|
48
|
+
};
|
49
|
+
export const disposable = (f, disposes = new Set()) => {
|
50
|
+
f(g => (disposes.add(g), () => disposes.delete(g)));
|
51
|
+
return () => (disposes.forEach(g => g()), disposes.clear());
|
52
|
+
};
|
53
|
+
export const disposing = () => {
|
54
|
+
let dispose = noop;
|
55
|
+
return (f) => (...xs) => (dispose(), dispose = f(...xs) ?? noop);
|
56
|
+
};
|
package/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 toByteArray: (stream: ReadableStream<Uint8Array>) => Promise<Uint8Array<ArrayBufferLike>>;
|
4
|
+
export declare const fetchBuffer: (url: string, init?: RequestInit) => Promise<Uint8Array<ArrayBufferLike>>;
|
5
|
+
export declare const fetchText: (url: string, init?: RequestInit) => Promise<string>;
|
6
|
+
export declare const fetchDOM: (url: string, init?: RequestInit) => Promise<HTMLElement>;
|
7
|
+
export declare const fetchImage: (src: string) => Promise<HTMLImageElement>;
|
8
|
+
export declare const fetchJSON: (url: string, init?: RequestInit) => Promise<any>;
|
9
|
+
export declare const fetchDataUrl: (url: string) => Promise<string>;
|
package/http.js
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
import { tee } from "./fn";
|
2
|
+
import { assert } from "./checks";
|
3
|
+
import { reduceArray } from './stream';
|
4
|
+
import { concat } from "./typedarray";
|
5
|
+
import { fromBytes } from "./json";
|
6
|
+
export const assertStatus = (status) => (resp) => assert(resp.status == status, `Invalid HTTP Status ${resp.status}: ${resp.url}`);
|
7
|
+
export const contentLength = (response) => {
|
8
|
+
const cl = response.headers.get("content-length");
|
9
|
+
return cl ? parseInt(cl) : -1;
|
10
|
+
};
|
11
|
+
export const toByteArray = async (stream) => concat(await reduceArray(stream));
|
12
|
+
export const fetchBuffer = (url, init) => fetch(url, init)
|
13
|
+
.then(tee(assertStatus(200)))
|
14
|
+
.then(resp => resp.body)
|
15
|
+
.then(toByteArray);
|
16
|
+
export const fetchText = (url, init) => fetchBuffer(url, init).then(buf => new TextDecoder("utf-8").decode(buf));
|
17
|
+
export const fetchDOM = (url, init) => fetchText(url, init).then(txt => new DOMParser().parseFromString(txt, "image/svg+xml").documentElement);
|
18
|
+
export const fetchImage = (src) => new Promise((resolve, reject) => {
|
19
|
+
const image = new Image();
|
20
|
+
image.crossOrigin = "anonymous";
|
21
|
+
image.src = src;
|
22
|
+
image.onload = () => resolve(image);
|
23
|
+
image.onerror = reject;
|
24
|
+
});
|
25
|
+
export const fetchJSON = (url, init) => fetchBuffer(url, init).then(fromBytes);
|
26
|
+
export const fetchDataUrl = (url) => fetch(url)
|
27
|
+
.then(resp => resp.blob())
|
28
|
+
.then(blob => new Promise(resolve => {
|
29
|
+
const f = new FileReader();
|
30
|
+
f.addEventListener("load", () => resolve(f.result));
|
31
|
+
f.readAsDataURL(blob);
|
32
|
+
}));
|
package/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/iterable.d.ts
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
import type { Fn, FnV, Indexed, Pred } from "./types.js";
|
2
|
+
export declare const repeat: (n: number) => <A>(x: A) => Iterator<A>;
|
3
|
+
export declare const just: <T>(x: T) => IteratorObject<T>;
|
4
|
+
export declare const enumerate: (n?: number) => <T>(x: T) => Indexed<T>;
|
5
|
+
export declare function empty(): Generator<never, void, unknown>;
|
6
|
+
export declare function concat<T>(...xxs: ReadonlyArray<IteratorObject<T>>): Generator<T, void, unknown>;
|
7
|
+
export declare const toArray: <T>(xs: IteratorObject<T>) => T[];
|
8
|
+
export declare const map: <T, U = T>(f: Fn<T, U>) => (xs: IteratorObject<T>) => IteratorObject<U, undefined, unknown>;
|
9
|
+
export declare const filter: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => IteratorObject<T, undefined, unknown>;
|
10
|
+
export declare const drop: (n: number) => <T>(xs: IteratorObject<T>) => IteratorObject<T, undefined, unknown>;
|
11
|
+
export declare const flatMap: <T, U = Iterable<T>>(f: Fn<T, Iterator<U> | Iterable<U>>) => (xs: IteratorObject<T>) => IteratorObject<U, undefined, unknown>;
|
12
|
+
export declare const forEach: <T>(f?: Fn<T>) => (xs: IteratorObject<T>) => void;
|
13
|
+
export declare const some: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => boolean;
|
14
|
+
export declare const every: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => boolean;
|
15
|
+
export declare const find: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => T | undefined;
|
16
|
+
export declare const flat: <T>(xs: IteratorObject<IteratorObject<T>>) => IteratorObject<T, undefined, unknown>;
|
17
|
+
export declare const prepend: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
18
|
+
export declare const append: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
19
|
+
export declare const first: <T>(xs: IteratorObject<T>) => T | undefined;
|
20
|
+
export declare const reduce: <T, S>(f: FnV<[S, T], S>, a: S) => (xs: IteratorObject<T>) => S;
|
21
|
+
export declare const traverse: <T>(children: Fn<T, Iterable<T>>, f: Fn<T>) => Fn<T>;
|
22
|
+
export declare const take: (n: number) => <T>(xs: IteratorObject<T>) => Generator<T>;
|
23
|
+
export declare function arange(a: number, b?: number, s?: number): Generator<number, void, unknown>;
|
24
|
+
export declare const linspace: (a: number, b: number, n: number) => Generator<number, void, unknown>;
|
25
|
+
export declare const steps: (a: number, b: number, s?: number, o?: number, eps?: number) => Generator<number, void, unknown>;
|
26
|
+
export declare const strided: (n: number, s: number) => <T>(xs: Iterable<T>) => Generator<T[], void, unknown>;
|
27
|
+
export declare const stopAt: <T>(p: Pred<T>) => (xs: IteratorObject<T>) => Generator<T, void, unknown>;
|
28
|
+
export declare const until: (p: Pred<void>) => <A>(xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
29
|
+
export type Iterators<T extends any[]> = {
|
30
|
+
[k in keyof T]: IteratorObject<T[k]>;
|
31
|
+
};
|
32
|
+
export declare function zip<T extends unknown[]>(...xs: Iterators<T>): Generator<T>;
|
33
|
+
export declare const iterA: <T>(xs: Promise<T>[]) => AsyncGenerator<Awaited<T>, void, unknown>;
|
34
|
+
export declare const forEachA: <T>(f?: Fn<T>) => (xs: AsyncIterable<T>) => Promise<void>;
|
35
|
+
export declare const mapA: <A, B>(f: Fn<A, B>) => Fn<AsyncIterator<A>, AsyncIterableIterator<B>>;
|
36
|
+
export declare const flatMapA: <A, B>(f: Fn<A, Iterable<B>>) => (xs: AsyncIterator<A>) => AsyncGenerator<Awaited<B>, void, any>;
|
37
|
+
export declare const filterA: <A>(f: Pred<A>) => Fn<AsyncIterator<A>, AsyncIterableIterator<A>>;
|
38
|
+
export declare const untilA: (p: Pred<void>) => <A>(xs: AsyncIterator<A>) => AsyncGenerator<Awaited<A>, void, unknown>;
|
39
|
+
export declare const reduceA: <T, S>(f: FnV<[S, T], S>, a: S) => (xs: AsyncIterator<T>) => Promise<S>;
|
40
|
+
export declare const race: (n: number) => <T>(xs: IteratorObject<Promise<T>>) => AsyncGenerator<Awaited<T>, void, unknown>;
|
41
|
+
export declare const zmap: <T extends unknown[]>(f: FnV<T>) => (...xs: Iterators<T>) => void;
|