@cgtk/std 0.0.185 → 0.0.186
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 +3 -1
- package/array.js +4 -2
- package/assign.d.ts +1 -1
- package/assign.js +1 -1
- package/async.d.ts +4 -1
- package/async.js +5 -2
- package/checks.d.ts +3 -3
- package/checks.js +5 -5
- package/dom.d.ts +27 -16
- package/dom.js +7 -2
- package/fn.d.ts +11 -7
- package/fn.js +19 -14
- package/http.d.ts +1 -0
- package/http.js +5 -0
- package/iterable.d.ts +1 -0
- package/iterable.js +11 -1
- package/map.d.ts +5 -6
- package/map.js +3 -4
- package/math.d.ts +3 -0
- package/math.js +4 -0
- package/npy.d.ts +10 -0
- package/npy.js +29 -0
- package/number.d.ts +1 -0
- package/number.js +6 -0
- package/object.d.ts +8 -6
- package/object.js +6 -4
- package/package.json +7 -3
- package/port.d.ts +29 -0
- package/port.js +53 -0
- package/progress.d.ts +1 -1
- package/rect.d.ts +5 -0
- package/rect.js +16 -0
- package/resource.d.ts +9 -0
- package/resource.js +20 -0
- package/schedule.d.ts +6 -4
- package/schedule.js +11 -22
- package/set.d.ts +1 -0
- package/set.js +1 -0
- package/signal.d.ts +9 -4
- package/signal.js +28 -5
- package/string.d.ts +3 -1
- package/string.js +6 -0
- package/struct.d.ts +34 -20
- package/struct.js +45 -46
- package/tree.d.ts +1 -10
- package/tree.js +3 -9
- package/treemap.js +1 -1
- package/typedarray.d.ts +12 -4
- package/typedarray.js +4 -2
- package/types.d.ts +30 -10
- package/utils.d.ts +1 -1
- package/utils.js +1 -1
- package/weak.d.ts +1 -1
- package/weak.js +1 -1
package/array.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { Fn, Tuple, Numbers } from "./types
|
|
1
|
+
import type { Fn, Tuple, Numbers } from "./types";
|
|
2
|
+
export declare const empty: <T>() => T[];
|
|
2
3
|
export declare const push: <T>(xs: T[], x: T) => T[];
|
|
3
4
|
export declare const unshift: <T>(xs: T[], x: T) => T[];
|
|
5
|
+
export declare const get: (i: number) => <T>(xs: T[]) => T;
|
|
4
6
|
export declare const pop: <T>(xs: T[]) => T[];
|
|
5
7
|
export declare const shift: <T>(xs: T[]) => T[];
|
|
6
8
|
export declare const first: <T>(xs: T[]) => T;
|
package/array.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { comp, scan } from "./fn
|
|
1
|
+
import { comp, scan } from "./fn";
|
|
2
|
+
export const empty = () => [];
|
|
2
3
|
export const push = (xs, x) => (xs.push(x), xs);
|
|
3
4
|
export const unshift = (xs, x) => (xs.unshift(x), xs);
|
|
5
|
+
export const get = (i) => (xs) => xs[i];
|
|
4
6
|
export const pop = (xs) => (xs.pop(), xs);
|
|
5
7
|
export const shift = (xs) => (xs.shift(), xs);
|
|
6
8
|
export const first = (xs) => xs[0];
|
|
@@ -9,7 +11,7 @@ export const cap = (n, f) => xs => {
|
|
|
9
11
|
f(xs);
|
|
10
12
|
return xs;
|
|
11
13
|
};
|
|
12
|
-
export const retain = (n, xs = []) => comp(scan(push)
|
|
14
|
+
export const retain = (n, xs = []) => comp(scan((push), xs), cap(n, shift));
|
|
13
15
|
export const tuple = (...xs) => xs;
|
|
14
16
|
export const seek = (arr, vals, offset = 0) => {
|
|
15
17
|
for (let i = offset, n = arr.length; i < n; i++)
|
package/assign.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RecordOf } from "./types
|
|
1
|
+
import type { RecordOf } from "./types";
|
|
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
package/async.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import type { Fn, Constantly, Optional } from "./types
|
|
1
|
+
import type { Fn, Constantly, Optional, Dispose, MaybePromise } from "./types";
|
|
2
2
|
export declare const timeout: (ms: number, signal?: AbortSignal) => Promise<unknown>;
|
|
3
3
|
export declare const delay: (ms: number) => <T>(x: T) => Promise<T>;
|
|
4
4
|
export declare const probing: <T>(p: Promise<T>) => Constantly<Optional<T>>;
|
|
5
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/async.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { filterT } from "./fn
|
|
2
|
-
import { isDefined } from "./checks
|
|
1
|
+
import { filterT, disposable } from "./fn";
|
|
2
|
+
import { isDefined } from "./checks";
|
|
3
3
|
export const timeout = (ms, signal) => new Promise((resolve, reject) => {
|
|
4
4
|
if (signal?.aborted)
|
|
5
5
|
reject(signal.reason);
|
|
@@ -25,3 +25,6 @@ export const resolved = (p) => {
|
|
|
25
25
|
return () => g(val());
|
|
26
26
|
};
|
|
27
27
|
};
|
|
28
|
+
export const promise = (x) => (x instanceof Promise) ? x : Promise.resolve(x);
|
|
29
|
+
export const then = (p) => (f) => p.then(f);
|
|
30
|
+
export const dispose = (p) => disposable(async (dispose) => dispose(await p));
|
package/checks.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { TypedArray,
|
|
1
|
+
import type { TypedArray, Pred, TPred, Optional, Numbers } from "./types";
|
|
2
2
|
export declare const isTypedArray: (x: any) => x is TypedArray;
|
|
3
|
-
export declare const isTypedArrayConstructor: (x: any) => x is TypedArrayConstructor;
|
|
4
3
|
export declare const isUndefined: (x: any) => x is undefined;
|
|
5
4
|
export declare const isNull: (x: any) => x is null;
|
|
6
5
|
export declare const isDefined: <T>(x: Optional<T>) => x is T;
|
|
@@ -13,10 +12,10 @@ export declare const isPositive: Pred<number>;
|
|
|
13
12
|
export declare const isTs: <T>(p: TPred<T>) => TPred<T[]>;
|
|
14
13
|
export declare const isclose: (a: number, b: number, eps?: number) => boolean;
|
|
15
14
|
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
15
|
export declare const equal: <T>(a: T, b?: T) => boolean;
|
|
18
16
|
export declare const strictEqual: <T>(a: T, b?: T) => boolean;
|
|
19
17
|
export declare const isDiffPrev: <T>(eq?: (a: T, b?: T | undefined) => boolean) => Pred<T>;
|
|
18
|
+
export declare const isLonger: <T>(delta: number) => Pred<T>;
|
|
20
19
|
export declare const isFunction: <T extends Function>(x: any) => x is T;
|
|
21
20
|
export declare const isAborted: (signal: AbortSignal) => () => boolean;
|
|
22
21
|
export declare const isArrayBufferLike: (x: any) => x is ArrayBufferLike;
|
|
@@ -24,3 +23,4 @@ export declare const isArrayBufferView: (x: any) => x is ArrayBufferView;
|
|
|
24
23
|
export declare const isEmpty: (x: {
|
|
25
24
|
length: number;
|
|
26
25
|
}) => boolean;
|
|
26
|
+
export declare const counted: (n: number) => () => boolean;
|
package/checks.js
CHANGED
|
@@ -4,10 +4,6 @@ export const isTypedArray = (x) => x && (x instanceof Uint8Array || x instanceof
|
|
|
4
4
|
x instanceof Uint16Array || x instanceof Uint32Array ||
|
|
5
5
|
x instanceof Int8Array || x instanceof Int16Array || x instanceof Int32Array ||
|
|
6
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
7
|
export const isUndefined = (x) => x === undefined;
|
|
12
8
|
export const isNull = (x) => x === null;
|
|
13
9
|
export const isDefined = (x) => !isUndefined(x);
|
|
@@ -23,10 +19,10 @@ export const isPositive = (x) => x > 0;
|
|
|
23
19
|
export const isTs = (p) => and(Array.isArray, (xs) => xs.every(p));
|
|
24
20
|
export const isclose = (a, b, eps = Number.EPSILON) => Math.abs(a - b) < eps;
|
|
25
21
|
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
22
|
export const equal = (a, b) => a == b;
|
|
28
23
|
export const strictEqual = (a, b) => a === b;
|
|
29
24
|
export const isDiffPrev = (eq = (strictEqual)) => comp(retain(2), not(apply(eq)));
|
|
25
|
+
export const isLonger = (delta) => comp((x) => [performance.now(), x], retain(2), (xs) => xs.length < 2 || xs[1][0] - xs[0][0] > delta);
|
|
30
26
|
export const isFunction = (x) => typeof x === "function";
|
|
31
27
|
export const isAborted = (signal) => () => signal.aborted;
|
|
32
28
|
export const isArrayBufferLike = (x) => x instanceof ArrayBuffer || x instanceof SharedArrayBuffer;
|
|
@@ -35,3 +31,7 @@ export const isArrayBufferView = (x) => x != null &&
|
|
|
35
31
|
isNumber(x.byteOffset) &&
|
|
36
32
|
isNumber(x.byteLength);
|
|
37
33
|
export const isEmpty = (x) => x.length == 0;
|
|
34
|
+
export const counted = (n) => {
|
|
35
|
+
let m = 0;
|
|
36
|
+
return () => m++ < n;
|
|
37
|
+
};
|
package/dom.d.ts
CHANGED
|
@@ -1,39 +1,50 @@
|
|
|
1
|
-
import type { Fn } from "./types
|
|
1
|
+
import type { Fn, Dispose, RecordOf, Optional } from "./types";
|
|
2
2
|
export declare const customEvent: <T>(type: string, detail: T, { composed, bubbles, cancelable }?: EventInit) => CustomEvent<T>;
|
|
3
|
+
type ListenOpts = boolean | AddEventListenerOptions;
|
|
3
4
|
export declare const on: {
|
|
4
|
-
<T extends HTMLElement, K extends keyof HTMLElementEventMap>(el: T, name: K, listener: Fn<HTMLElementEventMap[K]>, opts?:
|
|
5
|
-
<T extends SVGGraphicsElement, K extends keyof SVGElementEventMap>(el: T, name: K, listener: Fn<SVGElementEventMap[K]>, opts?:
|
|
6
|
-
<K extends keyof
|
|
7
|
-
<K extends keyof
|
|
8
|
-
<K extends keyof
|
|
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;
|
|
9
12
|
};
|
|
10
13
|
export declare const onAll: {
|
|
11
14
|
<T extends keyof HTMLElementEventMap>(el: HTMLElement, listeners: {
|
|
12
15
|
[k in T]: Fn<HTMLElementEventMap[k]>;
|
|
13
|
-
}, opts?:
|
|
16
|
+
}, opts?: ListenOpts): Dispose;
|
|
14
17
|
<T extends keyof SVGElementEventMap>(el: SVGGraphicsElement, listeners: {
|
|
15
18
|
[k in T]: Fn<SVGElementEventMap[k]>;
|
|
16
|
-
}, opts?:
|
|
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;
|
|
17
23
|
<T extends keyof DocumentEventMap>(el: Document, listeners: {
|
|
18
24
|
[k in T]: Fn<DocumentEventMap[k]>;
|
|
19
|
-
}, opts?:
|
|
25
|
+
}, opts?: ListenOpts): Dispose;
|
|
20
26
|
<T extends keyof WindowEventMap>(el: Window, listeners: {
|
|
21
27
|
[k in T]: Fn<WindowEventMap[k]>;
|
|
22
|
-
}, opts?:
|
|
28
|
+
}, opts?: ListenOpts): Dispose;
|
|
29
|
+
<T extends keyof WorkerEventMap>(el: Worker, listeners: {
|
|
30
|
+
[k in T]: Fn<WorkerEventMap[k]>;
|
|
31
|
+
}, opts?: ListenOpts): Dispose;
|
|
23
32
|
<T extends keyof AbortSignalEventMap>(el: AbortSignal, listeners: {
|
|
24
33
|
[k in T]: Fn<AbortSignalEventMap[k]>;
|
|
25
|
-
}, opts?:
|
|
34
|
+
}, opts?: ListenOpts): Dispose;
|
|
26
35
|
};
|
|
27
36
|
type DragHandlers = Record<'down' | 'move' | 'up', Fn<PointerEvent>>;
|
|
28
37
|
export declare const onDrag: {
|
|
29
|
-
(el: HTMLElement, fns: Partial<DragHandlers>):
|
|
30
|
-
(el: SVGGraphicsElement, fns: Partial<DragHandlers>):
|
|
38
|
+
(el: HTMLElement, fns: Partial<DragHandlers>): Dispose;
|
|
39
|
+
(el: SVGGraphicsElement, fns: Partial<DragHandlers>): Dispose;
|
|
31
40
|
};
|
|
32
41
|
export declare const onDragMove: {
|
|
33
|
-
(el: HTMLElement, f: Fn<PointerEvent>):
|
|
34
|
-
(el: SVGGraphicsElement, f: Fn<PointerEvent>):
|
|
42
|
+
(el: HTMLElement, f: Fn<PointerEvent>): Dispose;
|
|
43
|
+
(el: SVGGraphicsElement, f: Fn<PointerEvent>): Dispose;
|
|
35
44
|
};
|
|
36
|
-
export declare const onResize: (el: Element, f: Fn<ResizeObserverEntry[]>, options?: ResizeObserverOptions) =>
|
|
45
|
+
export declare const onResize: (el: Element, f: Fn<ResizeObserverEntry[]>, options?: ResizeObserverOptions) => Dispose;
|
|
37
46
|
export declare const preventDefault: <T extends Event>(e: T) => void;
|
|
38
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) => Optional<RecordOf<string>>;
|
|
39
50
|
export {};
|
package/dom.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { forEach, noop } from "./fn
|
|
2
|
-
import {
|
|
1
|
+
import { forEach, noop } from "./fn";
|
|
2
|
+
import { isDefined } from "./checks";
|
|
3
|
+
import { EMPTY } from "./constants";
|
|
3
4
|
export const customEvent = (type, detail, { composed = false, bubbles = false, cancelable = false } = EMPTY.OBJ) => new CustomEvent(type, { bubbles, cancelable, composed, detail });
|
|
4
5
|
export const on = (el, name, listener, opts = false) => {
|
|
5
6
|
el.addEventListener(name, listener, opts);
|
|
@@ -29,3 +30,7 @@ export const loadImage = (img, src) => new Promise((resolve) => {
|
|
|
29
30
|
const off = on(img, "load", () => (off(), resolve(img)));
|
|
30
31
|
img.src = src;
|
|
31
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/fn.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Fn, FnV, FnF, Pred, TPred, FirstParam, Last, Constantly, Optional } from "./types
|
|
1
|
+
import type { Fn, FnV, FnF, Pred, TPred, FirstParam, Last, Constantly, Optional, Dispose, Reactive } from "./types";
|
|
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;
|
|
@@ -15,15 +15,19 @@ export declare const apply: <F extends FnV>(f: F) => (x: Parameters<F>) => Retur
|
|
|
15
15
|
export declare const call: <T extends unknown[]>(f: Fn<T>) => (...xs: T) => ReturnType<typeof f>;
|
|
16
16
|
export declare const bind: <A extends unknown[], B extends unknown[], R>(f: (...args: [...A, ...B]) => R, ...args: A) => (...args: B) => R;
|
|
17
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
18
|
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) =>
|
|
19
|
+
export declare const before: <F extends Fn<void>>(f: F) => FnF;
|
|
21
20
|
export declare const after: <F extends Fn<void>>(f: F) => FnF;
|
|
22
21
|
export declare const mapForEach: <T, R>(...fs: Fn<T, Fn<R>>[]) => (x: T) => (x: R) => void;
|
|
23
22
|
export declare const filter: <T>(p: Pred<T>, f: Fn<T>) => (x: T) => void;
|
|
24
23
|
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
|
|
24
|
+
export declare const scan: <T, S>(f: FnV<[S, T], S>, a: S) => Fn<T, S>;
|
|
26
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>;
|
|
27
|
-
export declare const abortable: (f: Fn<AbortSignal
|
|
28
|
-
export declare const disposable: (f: Fn<Fn<
|
|
29
|
-
export declare const disposing: () => <F extends FnV<any[], Optional<
|
|
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[], Optional<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/fn.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { add } from "./set";
|
|
1
2
|
export const tee = f => x => (f(x), x);
|
|
2
3
|
export const comp = (...fs) => (x) => fs.reduce((x, f) => f(x), x);
|
|
3
4
|
export const pipe = (x, ...fs) => fs.reduce((x, f) => f(x), x);
|
|
@@ -14,16 +15,8 @@ export const apply = (f) => (x) => f.apply(null, x);
|
|
|
14
15
|
export const call = (f) => (...xs) => f(xs);
|
|
15
16
|
export const bind = (f, ...args) => f.bind(null, ...args);
|
|
16
17
|
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
18
|
export const where = (p, f, g) => (x) => p(x) ? f(x) : g(x);
|
|
26
|
-
export const before = (f) =>
|
|
19
|
+
export const before = (f) => g => (...xs) => (f(), g(...xs));
|
|
27
20
|
export const after = (f) => g => (...xs) => {
|
|
28
21
|
const r = g(...xs);
|
|
29
22
|
f();
|
|
@@ -34,23 +27,35 @@ export const filter = (p, f) => (x) => { if (p(x))
|
|
|
34
27
|
f(x); };
|
|
35
28
|
export const filterT = (p, f) => (x) => { if (p(x))
|
|
36
29
|
f(x); };
|
|
37
|
-
export const scan = (f
|
|
30
|
+
export const scan = (f, a) => (x) => a = f(a, x);
|
|
38
31
|
export const memoize = (f, key = (...xs) => xs[0], cache = new Map()) => (...xs) => {
|
|
39
32
|
const k = key(...xs);
|
|
40
33
|
if (!cache.has(k))
|
|
41
34
|
cache.set(k, f(...xs));
|
|
42
35
|
return cache.get(k);
|
|
43
36
|
};
|
|
44
|
-
export const abortable = (f) => {
|
|
37
|
+
export const abortable = (f, reason) => {
|
|
45
38
|
const ctrl = new AbortController();
|
|
46
39
|
f(ctrl.signal);
|
|
47
|
-
return (
|
|
40
|
+
return () => ctrl.abort(reason);
|
|
48
41
|
};
|
|
49
42
|
export const disposable = (f, disposes = new Set()) => {
|
|
50
|
-
f(
|
|
43
|
+
f(bind(add, disposes));
|
|
51
44
|
return () => (disposes.forEach(g => g()), disposes.clear());
|
|
52
45
|
};
|
|
53
46
|
export const disposing = () => {
|
|
54
47
|
let dispose = noop;
|
|
55
|
-
|
|
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));
|
|
56
59
|
};
|
|
60
|
+
export const voidify = (f) => (...xs) => { f(...xs); };
|
|
61
|
+
export const effectual = (f, g) => disposable(d => d(f(idemp(d, g))));
|
package/http.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export declare const fetchBuffer: (url: string, init?: RequestInit) => Promise<U
|
|
|
5
5
|
export declare const fetchText: (url: string, init?: RequestInit) => Promise<string>;
|
|
6
6
|
export declare const fetchDOM: (url: string, init?: RequestInit) => Promise<HTMLElement>;
|
|
7
7
|
export declare const fetchImage: (src: string) => Promise<HTMLImageElement>;
|
|
8
|
+
export declare const fetchImageBitmap: (url: string, init?: RequestInit) => Promise<ImageBitmap>;
|
|
8
9
|
export declare const fetchJSON: (url: string, init?: RequestInit) => Promise<any>;
|
|
9
10
|
export declare const fetchDataUrl: (url: string) => Promise<string>;
|
package/http.js
CHANGED
|
@@ -22,6 +22,11 @@ export const fetchImage = (src) => new Promise((resolve, reject) => {
|
|
|
22
22
|
image.onload = () => resolve(image);
|
|
23
23
|
image.onerror = reject;
|
|
24
24
|
});
|
|
25
|
+
export const fetchImageBitmap = async (url, init) => {
|
|
26
|
+
const res = await fetch(url, init);
|
|
27
|
+
const blob = await res.blob();
|
|
28
|
+
return await createImageBitmap(blob, { colorSpaceConversion: "none" });
|
|
29
|
+
};
|
|
25
30
|
export const fetchJSON = (url, init) => fetchBuffer(url, init).then(fromBytes);
|
|
26
31
|
export const fetchDataUrl = (url) => fetch(url)
|
|
27
32
|
.then(resp => resp.blob())
|
package/iterable.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export declare const prepend: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<
|
|
|
19
19
|
export declare const append: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
|
20
20
|
export declare const first: <T>(xs: IteratorObject<T>) => T | undefined;
|
|
21
21
|
export declare const reduce: <T, S>(f: FnV<[S, T, number], S>, a: S) => (xs: IteratorObject<T>) => S;
|
|
22
|
+
export declare const reduceRec: <T, A, R>(xs: IteratorObject<T>, f: FnV<[A, T, Fn<A>], R>, o: A, result: Fn<A, R>) => R;
|
|
22
23
|
export declare const traverse: <T>(children: Fn<T, Iterable<T>>, f: Fn<T>) => Fn<T>;
|
|
23
24
|
export declare const take: (n: number) => <T>(xs: IteratorObject<T>) => Generator<T>;
|
|
24
25
|
export declare function arange(a: number, b?: number, s?: number): Generator<number, void, unknown>;
|
package/iterable.js
CHANGED
|
@@ -29,6 +29,16 @@ export const prepend = (ys) => (xs) => concat(ys, xs);
|
|
|
29
29
|
export const append = (ys) => (xs) => concat(xs, ys);
|
|
30
30
|
export const first = (xs) => xs.find(constantly(true));
|
|
31
31
|
export const reduce = (f, a) => (xs) => xs.reduce(f, a);
|
|
32
|
+
export const reduceRec = (xs, f, o, result) => {
|
|
33
|
+
const next = (o) => {
|
|
34
|
+
const { done, value } = xs.next();
|
|
35
|
+
if (done)
|
|
36
|
+
return result(o);
|
|
37
|
+
else
|
|
38
|
+
return f(o, value, next);
|
|
39
|
+
};
|
|
40
|
+
return next(o);
|
|
41
|
+
};
|
|
32
42
|
export const traverse = (children, f) => (x) => (f(x), forEach(traverse(children, f))(Iterator.from(children(x))));
|
|
33
43
|
export const take = n => function* (xs) {
|
|
34
44
|
for (let i = 0; i < n; i++) {
|
|
@@ -129,7 +139,7 @@ export const untilA = (p) => async function* (xs) {
|
|
|
129
139
|
}
|
|
130
140
|
};
|
|
131
141
|
export const reduceA = (f, a) => async (xs) => {
|
|
132
|
-
const g = scan(f
|
|
142
|
+
const g = scan(f, a);
|
|
133
143
|
await forEachA()(mapA((x) => a = g(x))(xs));
|
|
134
144
|
return a;
|
|
135
145
|
};
|
package/map.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const
|
|
3
|
-
create: FnV<[
|
|
4
|
-
update: FnV<[V,
|
|
1
|
+
import type { FnV, Optional } from "./types";
|
|
2
|
+
export declare const transform: <K, S, V>({ create, update, remove }: {
|
|
3
|
+
create: FnV<[S, K, Map<K, V>], V>;
|
|
4
|
+
update: FnV<[V, S, K, Map<K, V>], V>;
|
|
5
5
|
remove?: FnV<[V, K, Map<K, V>]>;
|
|
6
|
-
}
|
|
7
|
-
export declare const mapAlloc: <T, S, K = any>(f: FnV<[T, S]>, alloc: Fn<void, T>, remove?: FnV<[T, K, Map<K, T>]>) => (dst?: Map<K, T>) => (src: Map<K, S>) => Map<K, T>;
|
|
6
|
+
}, dst?: Map<K, V>) => (src: Map<K, S>) => Map<K, V>;
|
|
8
7
|
export declare const set: <K, V>(m: Map<K, V>, [i, x]: [K, V]) => Map<K, V>;
|
|
9
8
|
export declare const get: <K, V>(m: Map<K, V>, k: K, d: V) => V;
|
|
10
9
|
export declare const update: <K, V>(m: Map<K, V>, k: K, f: FnV<[V | undefined, K, Map<K, V>], V>) => Map<K, V>;
|
package/map.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isDefined } from "./checks
|
|
2
|
-
import * as I from "./iterable
|
|
3
|
-
export const
|
|
1
|
+
import { isDefined } from "./checks";
|
|
2
|
+
import * as I from "./iterable";
|
|
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)) {
|
|
6
6
|
remove && remove(dst.get(k), k, dst);
|
|
@@ -14,7 +14,6 @@ export const map = ({ create, update, remove }) => (dst = new Map()) => (src) =>
|
|
|
14
14
|
});
|
|
15
15
|
return dst;
|
|
16
16
|
};
|
|
17
|
-
export const mapAlloc = (f, alloc, remove) => map({ create: e => f(alloc(), e), update: f, remove });
|
|
18
17
|
export const set = (m, [i, x]) => (m.set(i, x), m);
|
|
19
18
|
export const get = (m, k, d) => m.has(k) ? m.get(k) : d;
|
|
20
19
|
export const update = (m, k, f) => set(m, [k, f(m.get(k), k, m)]);
|
package/math.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare const FOUR_OVER_PI: number;
|
|
|
10
10
|
export declare const INV_PI: number;
|
|
11
11
|
export declare const INV_TAU: number;
|
|
12
12
|
export declare const INV_HALF_PI: number;
|
|
13
|
+
export declare const U32_MAX = 4294967295;
|
|
13
14
|
export declare const EPS: number;
|
|
14
15
|
export declare const radians: (x: number) => number;
|
|
15
16
|
export declare const degrees: (x: number) => number;
|
|
@@ -29,3 +30,5 @@ export declare const logn: (n: number, x: number) => number;
|
|
|
29
30
|
export declare const floordiv: (x: number, b: number) => number;
|
|
30
31
|
export declare const zoom: (x: number, s: number, p: number) => number;
|
|
31
32
|
export declare const align: (x: number, b: number) => number;
|
|
33
|
+
export declare const sign11: (x: number) => 1 | -1;
|
|
34
|
+
export declare const nudge: (x: number, v?: number, eps?: number) => number;
|
package/math.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isclose } from "./checks";
|
|
1
2
|
export const PI = Math.PI;
|
|
2
3
|
export const TAU = PI * 2;
|
|
3
4
|
export const HALF_PI = PI / 2;
|
|
@@ -10,6 +11,7 @@ export const FOUR_OVER_PI = 4 / Math.PI;
|
|
|
10
11
|
export const INV_PI = 1 / PI;
|
|
11
12
|
export const INV_TAU = 1 / TAU;
|
|
12
13
|
export const INV_HALF_PI = 1 / HALF_PI;
|
|
14
|
+
export const U32_MAX = 0xFFFFFFFF;
|
|
13
15
|
export const EPS = Number.EPSILON;
|
|
14
16
|
const DEG2RAD = PI / 180, RAD2DEG = 180 / PI;
|
|
15
17
|
export const radians = (x) => x * DEG2RAD;
|
|
@@ -37,3 +39,5 @@ export const logn = (n, x) => Math.log(x) / Math.log(n);
|
|
|
37
39
|
export const floordiv = (x, b) => Math.floor(x / b + (x < 0 ? 1 : 0));
|
|
38
40
|
export const zoom = (x, s, p) => (x - p) * s + p;
|
|
39
41
|
export const align = (x, b) => (x + b - 1) & -b;
|
|
42
|
+
export const sign11 = (x) => x >= 0 ? 1 : -1;
|
|
43
|
+
export const nudge = (x, v = 0, eps = EPS) => isclose(x, v, eps) ? sign11(x - v) * eps + v : x;
|
package/npy.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TypedArray, Elements } from "./types";
|
|
2
|
+
declare const DTYPES: readonly ["|u1", "<u2", "<u4", "|i1", "<i2", "<i4", "<f4", "<f8"];
|
|
3
|
+
type DType = Elements<typeof DTYPES>;
|
|
4
|
+
export interface NDArray {
|
|
5
|
+
data: TypedArray;
|
|
6
|
+
dtype: DType;
|
|
7
|
+
shape: number[];
|
|
8
|
+
}
|
|
9
|
+
export declare const read: (buf: ArrayBuffer) => NDArray;
|
|
10
|
+
export {};
|
package/npy.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const DTYPES = [
|
|
2
|
+
"|u1", "<u2", "<u4",
|
|
3
|
+
"|i1", "<i2", "<i4",
|
|
4
|
+
"<f4", "<f8",
|
|
5
|
+
// "<u8", "<i8",
|
|
6
|
+
];
|
|
7
|
+
const TYPED_ARRAY = {
|
|
8
|
+
"|u1": Uint8Array,
|
|
9
|
+
"<u2": Uint16Array,
|
|
10
|
+
"<u4": Uint32Array,
|
|
11
|
+
"|i1": Int8Array,
|
|
12
|
+
"<i2": Int16Array,
|
|
13
|
+
"<i4": Int32Array,
|
|
14
|
+
"<f4": Float32Array,
|
|
15
|
+
"<f8": Float64Array,
|
|
16
|
+
// "<u8": BigUint64Array,
|
|
17
|
+
// "<i8": BigInt64Array,
|
|
18
|
+
};
|
|
19
|
+
export const read = (buf) => {
|
|
20
|
+
const headerLength = new DataView(buf.slice(8, 10)).getUint8(0);
|
|
21
|
+
const hcontents = new TextDecoder("utf-8").decode(buf.slice(10, 10 + headerLength));
|
|
22
|
+
const { descr, shape } = JSON.parse(hcontents
|
|
23
|
+
.replace(/'/g, '"')
|
|
24
|
+
.replace("False", "false")
|
|
25
|
+
.replace("(", "[")
|
|
26
|
+
.replace(/,*\),*/g, "]"));
|
|
27
|
+
const data = new TYPED_ARRAY[descr](buf, headerLength + 10);
|
|
28
|
+
return { data, shape, dtype: descr };
|
|
29
|
+
};
|
package/number.d.ts
CHANGED
package/number.js
CHANGED
|
@@ -3,3 +3,9 @@ export const toFixedMax = (x, n = 0) => {
|
|
|
3
3
|
const y = Number(x.toFixed(n));
|
|
4
4
|
return `${Math.sign(y) == -1 ? '-' : ''}${Math.abs(y)}`;
|
|
5
5
|
};
|
|
6
|
+
export const roundTo = (x, n) => {
|
|
7
|
+
const inv = 1. / n;
|
|
8
|
+
const result = Math.round(x * inv) / inv;
|
|
9
|
+
const decimals = n.toString().split(".")[1]?.length || 0;
|
|
10
|
+
return parseFloat(result.toFixed(decimals));
|
|
11
|
+
};
|
package/object.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import type { RecordOf, Elements, Optional,
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const
|
|
1
|
+
import type { RecordOf, Elements, Optional, FnV, ValueOf, EntriesTuple, UnionToTuple, Entry } from "./types.js";
|
|
2
|
+
export declare const entries: <T extends RecordOf<any>>(o: T) => EntriesTuple<T>;
|
|
3
|
+
export declare const keys: <T extends RecordOf<any>>(o: T) => UnionToTuple<keyof T>;
|
|
4
|
+
export declare const entry: <K extends PropertyKey, V>(k: K, v: V) => Entry<K, V>;
|
|
5
|
+
export declare const fromEntries: <T extends Entry<PropertyKey, any>[]>(xs: T) => { [K in T[number] as K[0]]: K[1]; };
|
|
6
|
+
export declare const mapEntries: <T extends RecordOf<any>, S>(o: T, f: FnV<[Entry<keyof T, ValueOf<T>>, number], Entry<keyof T, S>>) => { [K in Entry<keyof T, S> as K[0]]: K[1]; };
|
|
7
|
+
export declare const mapValues: <T extends RecordOf<any>, S>(o: T, f: FnV<[ValueOf<T>, keyof T], S>) => { [K in Entry<keyof T, S> as K[0]]: K[1]; };
|
|
6
8
|
export declare const pick: <T extends RecordOf<any>, R extends Array<keyof T>>(obj: T, keys: R) => Pick<T, Elements<R>>;
|
|
7
9
|
export declare const omit: <T extends RecordOf<any>, R extends Array<keyof T>>(obj: T, keys: R) => Omit<T, Elements<R>>;
|
|
8
|
-
export declare const assoc: <T
|
|
10
|
+
export declare const assoc: <T extends RecordOf<any>>(a: T, [k, v]: [keyof T, ValueOf<T>]) => T;
|
|
9
11
|
type Merge<T extends RecordOf<unknown>, S extends RecordOf<unknown>, V> = {
|
|
10
12
|
[K in keyof (T & S)]: V;
|
|
11
13
|
};
|
package/object.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
export const
|
|
2
|
-
export const
|
|
3
|
-
export const
|
|
4
|
-
export const
|
|
1
|
+
export const entries = (o) => Object.entries(o);
|
|
2
|
+
export const keys = (o) => Object.keys(o);
|
|
3
|
+
export const entry = (k, v) => [k, v];
|
|
4
|
+
export const fromEntries = (xs) => Object.fromEntries(xs);
|
|
5
|
+
export const mapEntries = (o, f) => fromEntries(entries(o).map(f));
|
|
6
|
+
export const mapValues = (o, f) => mapEntries(o, ([k, v]) => entry(k, f(v, k)));
|
|
5
7
|
export const pick = (obj, keys) => keys.reduce((a, k) => (a[k] = obj[k], a), {});
|
|
6
8
|
export const omit = (obj, keys) => pick(obj, Object.keys(obj).filter(k => !keys.includes(k)));
|
|
7
9
|
export const assoc = (a, [k, v]) => (a[k] = v, a);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cgtk/std",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.186",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"exports": {
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"./signal": "./signal.js",
|
|
13
13
|
"./assign": "./assign.js",
|
|
14
14
|
"./map": "./map.js",
|
|
15
|
+
"./set": "./set.js",
|
|
15
16
|
"./array": "./array.js",
|
|
16
17
|
"./string": "./string.js",
|
|
17
18
|
"./number": "./number.js",
|
|
@@ -19,7 +20,6 @@
|
|
|
19
20
|
"./async": "./async.js",
|
|
20
21
|
"./schedule": "./schedule.js",
|
|
21
22
|
"./iterable": "./iterable.js",
|
|
22
|
-
"./gen": "./gen.js",
|
|
23
23
|
"./tree": "./tree.js",
|
|
24
24
|
"./treemap": "./treemap.js",
|
|
25
25
|
"./stream": "./stream.js",
|
|
@@ -34,10 +34,14 @@
|
|
|
34
34
|
"./struct": "./struct.js",
|
|
35
35
|
"./buffer": "./buffer.js",
|
|
36
36
|
"./base64": "./base64.js",
|
|
37
|
+
"./npy": "./npy.js",
|
|
38
|
+
"./port": "./port.js",
|
|
39
|
+
"./rect": "./rect.js",
|
|
40
|
+
"./resource": "./resource.js",
|
|
37
41
|
"./utils": "./utils.js"
|
|
38
42
|
},
|
|
39
43
|
"devDependencies": {
|
|
40
|
-
"typescript": "^5.
|
|
44
|
+
"typescript": "^5.9.2"
|
|
41
45
|
},
|
|
42
46
|
"publishConfig": {
|
|
43
47
|
"access": "public"
|
package/port.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Dispose, Fn, Fns, FnV, RecordOf, MaybePromise } from "./types";
|
|
2
|
+
export interface Message<K, V> {
|
|
3
|
+
type: K;
|
|
4
|
+
payload: V;
|
|
5
|
+
}
|
|
6
|
+
export type Port = {
|
|
7
|
+
postMessage(message: any, options?: StructuredSerializeOptions): void;
|
|
8
|
+
};
|
|
9
|
+
export declare const onMessages: <S extends RecordOf<any>, T = Worker>(target: MessageEventTarget<T>, fs: Fns<S>) => Dispose;
|
|
10
|
+
export declare const onMessage: <K extends string, S, T = Worker>(target: MessageEventTarget<T>, name: K, f: Fn<S>) => Dispose;
|
|
11
|
+
export declare const postMessages: <T extends RecordOf<any>>(port: Port) => { [k in keyof T]: FnV<[T[k], (StructuredSerializeOptions | undefined)?]>; };
|
|
12
|
+
export interface Request {
|
|
13
|
+
id: number;
|
|
14
|
+
name: PropertyKey;
|
|
15
|
+
args: any;
|
|
16
|
+
}
|
|
17
|
+
export interface Response {
|
|
18
|
+
id: number;
|
|
19
|
+
result: any;
|
|
20
|
+
}
|
|
21
|
+
interface TransferableFnV<T extends any[], R> {
|
|
22
|
+
(...args: T): R;
|
|
23
|
+
transfer(ts?: Transferable[]): FnV<T, R>;
|
|
24
|
+
}
|
|
25
|
+
export declare const client: <S extends RecordOf<any>, T = Worker>(target: Port & MessageEventTarget<T>) => { [k in keyof S]: TransferableFnV<Parameters<S[k]>, Promise<ReturnType<S[k]>>>; };
|
|
26
|
+
export declare const server: <S extends RecordOf<any>, T = Worker>(target: Port & MessageEventTarget<T>) => { [k in keyof S]: Fn<FnV<Parameters<S[k]>, MaybePromise<ReturnType<S[k]>>>, Dispose>; };
|
|
27
|
+
export declare const startClient: (worker: Worker, f: Fn<Worker, Dispose | void>) => Promise<Dispose>;
|
|
28
|
+
export declare const startServer: (worker: Worker, f: Fn<Worker, MaybePromise<Dispose>>) => Promise<Dispose>;
|
|
29
|
+
export {};
|