@cgtk/std 0.0.194 → 0.0.196
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 +2 -1
- package/array.js +2 -1
- package/async.d.ts +3 -3
- package/async.js +15 -2
- package/buffer.d.ts +7 -14
- package/buffer.js +25 -51
- package/channel.d.ts +37 -0
- package/channel.js +82 -0
- package/checks.d.ts +1 -0
- package/checks.js +4 -3
- package/dom.d.ts +21 -2
- package/dom.js +67 -4
- package/fn.d.ts +5 -9
- package/fn.js +4 -7
- package/math.d.ts +2 -0
- package/math.js +5 -0
- package/package.json +3 -6
- package/resource.d.ts +8 -1
- package/resource.js +26 -8
- package/schedule.d.ts +3 -3
- package/schedule.js +8 -6
- package/string.d.ts +1 -0
- package/string.js +1 -0
- package/types.d.ts +2 -6
- package/signal.d.ts +0 -37
- package/signal.js +0 -89
- package/src/array.d.ts +0 -14
- package/src/array.js +0 -21
- package/src/assign.d.ts +0 -8
- package/src/assign.js +0 -26
- package/src/async.d.ts +0 -8
- package/src/async.js +0 -31
- package/src/base64.d.ts +0 -1
- package/src/base64.js +0 -7
- package/src/basen.d.ts +0 -12
- package/src/basen.js +0 -31
- package/src/checks.d.ts +0 -29
- package/src/checks.js +0 -44
- package/src/constants.d.ts +0 -5
- package/src/constants.js +0 -1
- package/src/dom.d.ts +0 -50
- package/src/dom.js +0 -36
- package/src/fn.d.ts +0 -33
- package/src/fn.js +0 -61
- package/src/http.d.ts +0 -9
- package/src/http.js +0 -33
- package/src/index.d.ts +0 -1
- package/src/index.js +0 -1
- package/src/iterable.d.ts +0 -43
- package/src/iterable.js +0 -158
- package/src/json.d.ts +0 -1
- package/src/json.js +0 -1
- package/src/map.d.ts +0 -12
- package/src/map.js +0 -35
- package/src/math.d.ts +0 -34
- package/src/math.js +0 -43
- package/src/npy.d.ts +0 -10
- package/src/npy.js +0 -29
- package/src/number.d.ts +0 -4
- package/src/number.js +0 -13
- package/src/object.d.ts +0 -15
- package/src/object.js +0 -13
- package/src/port.d.ts +0 -30
- package/src/port.js +0 -56
- package/src/progress.d.ts +0 -6
- package/src/progress.js +0 -1
- package/src/rect.d.ts +0 -5
- package/src/rect.js +0 -16
- package/src/resource.d.ts +0 -12
- package/src/resource.js +0 -28
- package/src/schedule.d.ts +0 -10
- package/src/schedule.js +0 -33
- package/src/set.d.ts +0 -2
- package/src/set.js +0 -2
- package/src/signal.d.ts +0 -37
- package/src/signal.js +0 -89
- package/src/stream.d.ts +0 -6
- package/src/stream.js +0 -26
- package/src/string.d.ts +0 -8
- package/src/string.js +0 -29
- package/src/struct.d.ts +0 -42
- package/src/struct.js +0 -58
- package/src/tree.d.ts +0 -11
- package/src/tree.js +0 -25
- package/src/treemap.d.ts +0 -13
- package/src/treemap.js +0 -71
- package/src/typedarray.d.ts +0 -31
- package/src/typedarray.js +0 -39
- package/src/types.d.ts +0 -63
- package/src/types.js +0 -1
- package/src/utils.d.ts +0 -4
- package/src/utils.js +0 -30
- package/src/weak.d.ts +0 -2
- package/src/weak.js +0 -5
- /package/{src/buffer.d.ts → bytes.d.ts} +0 -0
- /package/{src/buffer.js → bytes.js} +0 -0
package/src/port.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { forEach, noop, bind } from "./fn.js";
|
|
2
|
-
import { isDefined } from "./checks.js";
|
|
3
|
-
import { on } from "./dom.js";
|
|
4
|
-
import { promise } from "./async.js";
|
|
5
|
-
;
|
|
6
|
-
export const onMessages = (target, fs) => on(target, "message", ({ data: { type, payload } }) => (type in fs) && fs[type](payload));
|
|
7
|
-
export const onMessage = (target, name, f) => on(target, "message", ({ data: { type, payload } }) => type == name && f(payload));
|
|
8
|
-
export const messageSources = (worker) => new Proxy({}, {
|
|
9
|
-
get: (_, k) => bind((onMessage), worker, k)
|
|
10
|
-
});
|
|
11
|
-
export const postMessages = (port) => new Proxy({}, {
|
|
12
|
-
get: (_, type) => (payload, options) => port.postMessage({ type, payload }, options)
|
|
13
|
-
});
|
|
14
|
-
export const client = (target) => {
|
|
15
|
-
const fns = new Map();
|
|
16
|
-
onMessage(target, "rpc", ({ id, result }) => {
|
|
17
|
-
const f = fns.get(id);
|
|
18
|
-
if (isDefined(f))
|
|
19
|
-
f(result), fns.delete(id);
|
|
20
|
-
});
|
|
21
|
-
const { rpc } = postMessages(target);
|
|
22
|
-
let _id = 0;
|
|
23
|
-
return new Proxy({}, {
|
|
24
|
-
get(_, name) {
|
|
25
|
-
const g = (transfer) => (...args) => {
|
|
26
|
-
const id = _id++;
|
|
27
|
-
rpc({ id, name, args }, { transfer });
|
|
28
|
-
return new Promise(resolve => fns.set(id, resolve));
|
|
29
|
-
};
|
|
30
|
-
const f = ((...args) => g()(...args));
|
|
31
|
-
f.transfer = g;
|
|
32
|
-
return f;
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
export const server = (target) => {
|
|
37
|
-
const fns = new Map();
|
|
38
|
-
const { rpc } = postMessages(target);
|
|
39
|
-
onMessage(target, "rpc", async ({ id, name, args }) => {
|
|
40
|
-
const f = fns.get(name);
|
|
41
|
-
if (isDefined(f))
|
|
42
|
-
rpc({ id, result: await promise(f(...args)) });
|
|
43
|
-
});
|
|
44
|
-
return new Proxy({}, {
|
|
45
|
-
get: (_, k) => (f) => (fns.set(k, f), () => fns.delete(k))
|
|
46
|
-
});
|
|
47
|
-
};
|
|
48
|
-
export const startClient = async (worker, f) => {
|
|
49
|
-
const cli = client(worker);
|
|
50
|
-
await cli.start();
|
|
51
|
-
return forEach(f(worker) ?? noop, () => cli.stop().then(() => worker.terminate()));
|
|
52
|
-
};
|
|
53
|
-
export const startServer = async (worker, f) => {
|
|
54
|
-
const ser = server(worker);
|
|
55
|
-
return ser.start(async () => { ser.stop(await promise(f(worker))); });
|
|
56
|
-
};
|
package/src/progress.d.ts
DELETED
package/src/progress.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/src/rect.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { Tuple } from "./types.js";
|
|
2
|
-
export type Rect = Tuple<number, 4>;
|
|
3
|
-
export declare const rect: (x: number, y: number, w: number, h: number) => Rect;
|
|
4
|
-
export declare const vertical: (ratio: number[]) => (i: number) => ([x, _, w, h]: Rect) => Rect;
|
|
5
|
-
export declare const horizontal: (ratio: number[]) => (i: number) => ([_, y, w, h]: Rect) => Rect;
|
package/src/rect.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { add } from "./number.js";
|
|
2
|
-
export const rect = (x, y, w, h) => [x, y, w, h];
|
|
3
|
-
export const vertical = (ratio) => {
|
|
4
|
-
const tot = ratio.reduce(add, 0);
|
|
5
|
-
const cum = [0];
|
|
6
|
-
ratio.forEach(x => cum.push(x / tot + cum[cum.length - 1]));
|
|
7
|
-
cum.pop();
|
|
8
|
-
return (i) => ([x, _, w, h]) => [x, cum[i] * h, w, ratio[i] / tot * h];
|
|
9
|
-
};
|
|
10
|
-
export const horizontal = (ratio) => {
|
|
11
|
-
const tot = ratio.reduce(add, 0);
|
|
12
|
-
const cum = [0];
|
|
13
|
-
ratio.forEach(x => cum.push(x / tot + cum[cum.length - 1]));
|
|
14
|
-
cum.pop();
|
|
15
|
-
return (i) => ([_, y, w, h]) => [cum[i] * w, y, ratio[i] / tot * w, h];
|
|
16
|
-
};
|
package/src/resource.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Fn, Dispose, Structure, Reactive } from "./types.js";
|
|
2
|
-
export type Resource<T> = Reactive<T, Dispose | void> & {
|
|
3
|
-
value: T;
|
|
4
|
-
dispose: Dispose;
|
|
5
|
-
map<S>(f: Fn<T, S>): Resource<S>;
|
|
6
|
-
};
|
|
7
|
-
export declare const resource: <T>(value: T, dispose?: Dispose) => Resource<T>;
|
|
8
|
-
type Unwrapped<T> = T extends Resource<infer U> ? U : {
|
|
9
|
-
[K in keyof T]: Unwrapped<T[K]>;
|
|
10
|
-
};
|
|
11
|
-
export declare const compose: <R extends Structure<Resource<any>>>(rs: R) => Resource<Unwrapped<R>>;
|
|
12
|
-
export {};
|
package/src/resource.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { forEach, noop } from "./fn.js";
|
|
2
|
-
import { isFunction } from "./checks.js";
|
|
3
|
-
export const resource = (value, dispose = noop) => {
|
|
4
|
-
const res = ((f) => forEach(f(value) ?? noop, dispose));
|
|
5
|
-
res.value = value;
|
|
6
|
-
res.dispose = dispose;
|
|
7
|
-
res.map = (f) => resource(f(value), dispose);
|
|
8
|
-
return res;
|
|
9
|
-
};
|
|
10
|
-
const isResource = (r) => isFunction(r)
|
|
11
|
-
&& Object.hasOwn(r, "value")
|
|
12
|
-
&& Object.hasOwn(r, "dispose")
|
|
13
|
-
&& isFunction(r["dispose"]);
|
|
14
|
-
export const compose = (rs) => {
|
|
15
|
-
const disposes = [];
|
|
16
|
-
const rec = (rs) => {
|
|
17
|
-
if (isResource(rs)) {
|
|
18
|
-
disposes.push(rs.dispose);
|
|
19
|
-
return rs.value;
|
|
20
|
-
}
|
|
21
|
-
else if (Array.isArray(rs)) {
|
|
22
|
-
return rs.map(rec);
|
|
23
|
-
}
|
|
24
|
-
return Object.fromEntries(Object.entries(rs).map(([k, v]) => [k, rec(v)]));
|
|
25
|
-
};
|
|
26
|
-
const value = rec(rs);
|
|
27
|
-
return resource(value, forEach(...disposes));
|
|
28
|
-
};
|
package/src/schedule.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { Dispose, Fn, FnV, Reactive } from './types.js';
|
|
2
|
-
export declare const raf: Reactive<number>;
|
|
3
|
-
export declare const timeout: (ms: number) => Reactive<void>;
|
|
4
|
-
export declare const schedule: <T>(s: Reactive<T>) => <F extends FnV>(f: F) => FnV<Parameters<F>, Dispose>;
|
|
5
|
-
export declare const idempotent: <T>(s: Reactive<T>, d: Fn<Dispose, Dispose>) => <F extends FnV>(f: F) => FnV<Parameters<F>, Dispose>;
|
|
6
|
-
export declare const debounce: <T>(sched?: Reactive<T>) => Reactive<T>;
|
|
7
|
-
export declare const reset: <T>(sched?: Reactive<T>) => Reactive<T>;
|
|
8
|
-
export declare const repeatedly: <T>(f: Fn<void>, sched?: Reactive<T>) => () => any;
|
|
9
|
-
export declare const delayAfter: <F extends FnV<any>>(delta: number, n: number, f: F) => (x: any) => any;
|
|
10
|
-
export declare const micro: <F extends FnV>(f: F) => (...xs: Parameters<F>) => void;
|
package/src/schedule.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { isUndefined, isDefined, counted } from './checks.js';
|
|
2
|
-
import { comp, lazy, bind, apply, call, where, disposing, noop } from './fn.js';
|
|
3
|
-
export const raf = f => bind(cancelAnimationFrame, requestAnimationFrame(f));
|
|
4
|
-
export const timeout = (ms) => f => bind(clearTimeout, setTimeout(f, ms));
|
|
5
|
-
export const schedule = (s) => (f) => call(comp(apply(bind((lazy), f)), s));
|
|
6
|
-
export const idempotent = (s, d) => (f) => disposing()((...xs) => d(s(() => f(...xs))));
|
|
7
|
-
export const debounce = (sched = raf) => {
|
|
8
|
-
let cancel;
|
|
9
|
-
const stop = () => { if (isDefined(cancel))
|
|
10
|
-
cancel(), cancel = undefined; };
|
|
11
|
-
return (f) => {
|
|
12
|
-
if (isUndefined(cancel))
|
|
13
|
-
cancel = sched((x) => (cancel = undefined, f(x)));
|
|
14
|
-
return stop;
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
export const reset = (sched = raf) => {
|
|
18
|
-
let cancel = noop;
|
|
19
|
-
const stop = () => (cancel(), cancel = noop);
|
|
20
|
-
return (f) => {
|
|
21
|
-
stop();
|
|
22
|
-
cancel = sched(f);
|
|
23
|
-
return stop;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
export const repeatedly = (f, sched = raf) => {
|
|
27
|
-
let cancel;
|
|
28
|
-
const g = () => (f(), (cancel = sched(g)));
|
|
29
|
-
cancel = sched(g);
|
|
30
|
-
return () => cancel();
|
|
31
|
-
};
|
|
32
|
-
export const delayAfter = (delta, n, f) => where(counted(n), f, schedule(reset(timeout(delta)))(f));
|
|
33
|
-
export const micro = (f) => (...xs) => queueMicrotask(() => f(...xs));
|
package/src/set.d.ts
DELETED
package/src/set.js
DELETED
package/src/signal.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { Fn, FnV, Fns, Pred, Reactive, RecordOf, Structure } from "./types.js";
|
|
2
|
-
export type Signal<T> = Fn<T> & {
|
|
3
|
-
on: Reactive<T>;
|
|
4
|
-
};
|
|
5
|
-
export declare const create: <T>(f: Fn<T>, on: Reactive<T>) => Signal<T>;
|
|
6
|
-
export declare const signal: <T>(fns?: Set<Fn<T>>) => Signal<T>;
|
|
7
|
-
export declare const map: <T, S>(source: Reactive<T>, f: Fn<T, S>) => Reactive<S>;
|
|
8
|
-
export declare const filter: <T>(source: Reactive<T>, p: Pred<T>) => Reactive<T>;
|
|
9
|
-
export declare const cache: <T>(source: Reactive<T>) => Reactive<T>;
|
|
10
|
-
export interface ValueLike<T> {
|
|
11
|
-
value: T;
|
|
12
|
-
on: Reactive<T>;
|
|
13
|
-
}
|
|
14
|
-
export interface Box<T, F extends RecordOf<FnV>> extends ValueLike<T> {
|
|
15
|
-
ops: F;
|
|
16
|
-
}
|
|
17
|
-
export interface Value<T> extends ValueLike<T> {
|
|
18
|
-
op: <F extends FnV>(f: Fn<T, F>) => F;
|
|
19
|
-
ops: <F extends RecordOf<FnV>>(f: Fn<T, F>) => F;
|
|
20
|
-
set: Fn<T>;
|
|
21
|
-
update(): void;
|
|
22
|
-
map<S>(f: Fn<T, S>): Reactive<S>;
|
|
23
|
-
box: <F extends RecordOf<FnV>>(f: Fn<T, F>) => Box<T, F>;
|
|
24
|
-
}
|
|
25
|
-
export declare const value: <T>(x: T, fs?: Set<Fn<T>>) => Value<T>;
|
|
26
|
-
export type Values<T extends RecordOf<any>> = {
|
|
27
|
-
[k in keyof T]: Value<T[k]>;
|
|
28
|
-
};
|
|
29
|
-
export declare const values: <T extends RecordOf<any>>(vals: T) => Values<T>;
|
|
30
|
-
type Unwrapped<T> = T extends ValueLike<infer U> ? U : {
|
|
31
|
-
[K in keyof T]: Unwrapped<T[K]>;
|
|
32
|
-
};
|
|
33
|
-
export declare const compose: <T extends Structure<ValueLike<any>>, S = Unwrapped<T>>(xs: T, transform?: Fn<Unwrapped<T>, S>) => Reactive<S>;
|
|
34
|
-
export declare const mux: <I extends RecordOf<any>, O extends RecordOf<any>>(init: Fn<Fns<O>, Fns<I>>) => Fns<I> & {
|
|
35
|
-
on: { [k in keyof O]: Reactive<O[k]>; };
|
|
36
|
-
};
|
|
37
|
-
export {};
|
package/src/signal.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
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
|
-
export const create = (f, on) => {
|
|
6
|
-
const g = f;
|
|
7
|
-
g.on = on;
|
|
8
|
-
return g;
|
|
9
|
-
};
|
|
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
|
-
};
|
|
22
|
-
;
|
|
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);
|
|
27
|
-
const notify = after(() => sig(x));
|
|
28
|
-
const op = (f) => notify(f(x));
|
|
29
|
-
const on = (f) => (f(x), sig.on(f));
|
|
30
|
-
const ops = (f) => mapValues(f(x), notify);
|
|
31
|
-
return {
|
|
32
|
-
get value() { return x; },
|
|
33
|
-
set value(y) { sig(x = y); },
|
|
34
|
-
op, on, ops,
|
|
35
|
-
set: op(constantly((y) => x = y)),
|
|
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
|
-
})
|
|
43
|
-
};
|
|
44
|
-
};
|
|
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;
|
|
72
|
-
};
|
|
73
|
-
export const mux = (init) => {
|
|
74
|
-
const sigs = {};
|
|
75
|
-
const ins = init(new Proxy({}, {
|
|
76
|
-
get: (_, k) => (x) => {
|
|
77
|
-
const f = sigs[k];
|
|
78
|
-
if (isDefined(f))
|
|
79
|
-
f(x);
|
|
80
|
-
}
|
|
81
|
-
}));
|
|
82
|
-
ins.on = new Proxy({}, {
|
|
83
|
-
get: (_, k) => f => {
|
|
84
|
-
const sig = sigs[k] ??= signal();
|
|
85
|
-
return sig.on(f);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
return ins;
|
|
89
|
-
};
|
package/src/stream.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { Fn } from "./types.js";
|
|
2
|
-
export declare const readable: <T>(x: T) => ReadableStream<any>;
|
|
3
|
-
export declare function iterate<T>(rs: ReadableStream<T>): AsyncGenerator<Awaited<T>, void, unknown>;
|
|
4
|
-
export declare const map: <T, S>(f: Fn<T, S>) => TransformStream<T, S>;
|
|
5
|
-
export declare const tap: <T>(start: TransformerStartCallback<T> | undefined, t: Fn<T>, flush?: TransformerFlushCallback<T>) => TransformStream<T, any>;
|
|
6
|
-
export declare const reduceArray: <T>(stream: ReadableStream<T>, xs?: T[]) => Promise<T[]>;
|
package/src/stream.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { push } from "./array.js";
|
|
2
|
-
import { reduceA } from "./iterable.js";
|
|
3
|
-
export const readable = (x) => new ReadableStream({
|
|
4
|
-
start: c => (c.enqueue(x), c.close())
|
|
5
|
-
});
|
|
6
|
-
export async function* iterate(rs) {
|
|
7
|
-
const reader = rs.getReader();
|
|
8
|
-
try {
|
|
9
|
-
let x = await reader.read();
|
|
10
|
-
while (!x.done)
|
|
11
|
-
yield x.value, x = await reader.read();
|
|
12
|
-
}
|
|
13
|
-
finally {
|
|
14
|
-
reader.releaseLock();
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export const map = (f) => new TransformStream({ transform(x, c) { c.enqueue(f(x)); } });
|
|
18
|
-
export const tap = (start, t, flush) => new TransformStream({
|
|
19
|
-
start,
|
|
20
|
-
transform(x, c) {
|
|
21
|
-
t(x);
|
|
22
|
-
c.enqueue(x);
|
|
23
|
-
},
|
|
24
|
-
flush
|
|
25
|
-
});
|
|
26
|
-
export const reduceArray = (stream, xs = []) => reduceA((push), xs)(iterate(stream));
|
package/src/string.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Tuple } from "./types.js";
|
|
2
|
-
export declare const basename: (path: string) => string;
|
|
3
|
-
export declare const dirname: (path: string) => string;
|
|
4
|
-
export declare const splitext: (path: string) => Tuple<string, 2>;
|
|
5
|
-
export declare const objectURL: (data: Uint8Array<ArrayBuffer>, type: string) => string;
|
|
6
|
-
export declare const decode: (label?: string, opts?: TextDecoderOptions) => (x: Uint8Array, stream?: boolean) => string;
|
|
7
|
-
export declare const readLine: (delims?: Set<number>) => (data: Uint8Array, offset?: number) => string;
|
|
8
|
-
export declare const DATA_URL: RegExp;
|
package/src/string.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { EMPTY } from "./constants.js";
|
|
2
|
-
import { seek } from "./array.js";
|
|
3
|
-
export const basename = (path) => path.split("/").at(-1) ?? "";
|
|
4
|
-
export const dirname = (path) => {
|
|
5
|
-
const parts = path.split("/");
|
|
6
|
-
parts.pop();
|
|
7
|
-
return parts.join("/");
|
|
8
|
-
};
|
|
9
|
-
export const splitext = (path) => {
|
|
10
|
-
const idx = path.lastIndexOf(".");
|
|
11
|
-
if (idx == -1)
|
|
12
|
-
return [path, ""];
|
|
13
|
-
return [path.slice(0, idx), path.slice(idx + 1)];
|
|
14
|
-
};
|
|
15
|
-
export const objectURL = (data, type) => URL.createObjectURL(new Blob([data], { type }));
|
|
16
|
-
export const decode = (label = "utf-8", opts = {
|
|
17
|
-
fatal: false, ignoreBOM: false
|
|
18
|
-
}) => {
|
|
19
|
-
const decoder = new TextDecoder(label, opts);
|
|
20
|
-
return (x, stream = false) => decoder.decode(x, { stream });
|
|
21
|
-
};
|
|
22
|
-
export const readLine = (delims = new Set([0x0A])) => {
|
|
23
|
-
const dec = decode();
|
|
24
|
-
return (data, offset = 0) => {
|
|
25
|
-
const n = seek(data, delims, offset);
|
|
26
|
-
return n > 0 ? dec(data.subarray(offset, n + 1)) : EMPTY.STR;
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
export const DATA_URL = /^data:(?<mime>[\w/\-\+]+)?(;(?<params>[\w\-]+\=[^;,\s]+)*)?(;(?<encoding>base64))?,(?<data>.*)$/;
|
package/src/struct.d.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { TypedArray, Fn, NumberArray } from "./types.js";
|
|
2
|
-
import type { Constructor } from "./typedarray.js";
|
|
3
|
-
export interface Type {
|
|
4
|
-
size: number;
|
|
5
|
-
align: number;
|
|
6
|
-
}
|
|
7
|
-
export type Vec<T extends TypedArray, N extends number> = Type & {
|
|
8
|
-
length: N;
|
|
9
|
-
type: Constructor<T>;
|
|
10
|
-
};
|
|
11
|
-
export declare const u8: <N extends number = 1>(length?: N, align?: number) => Vec<Uint8Array<ArrayBufferLike>, N>;
|
|
12
|
-
export declare const u16: <N extends number = 1>(length?: N, align?: number) => Vec<Uint16Array<ArrayBufferLike>, N>;
|
|
13
|
-
export declare const u32: <N extends number = 1>(length?: N, align?: number) => Vec<Uint32Array<ArrayBufferLike>, N>;
|
|
14
|
-
export declare const u64: <N extends number = 1>(length?: N, align?: number) => Vec<BigUint64Array<ArrayBufferLike>, N>;
|
|
15
|
-
export declare const i8: <N extends number = 1>(length?: N, align?: number) => Vec<Int8Array<ArrayBufferLike>, N>;
|
|
16
|
-
export declare const i16: <N extends number = 1>(length?: N, align?: number) => Vec<Int16Array<ArrayBufferLike>, N>;
|
|
17
|
-
export declare const i32: <N extends number = 1>(length?: N, align?: number) => Vec<Int32Array<ArrayBufferLike>, N>;
|
|
18
|
-
export declare const i64: <N extends number = 1>(length?: N, align?: number) => Vec<BigInt64Array<ArrayBufferLike>, N>;
|
|
19
|
-
export declare const f16: <N extends number = 1>(length?: N, align?: number) => Vec<Float16Array<ArrayBufferLike>, N>;
|
|
20
|
-
export declare const f32: <N extends number = 1>(length?: N, align?: number) => Vec<Float32Array<ArrayBufferLike>, N>;
|
|
21
|
-
export declare const f64: <N extends number = 1>(length?: N, align?: number) => Vec<Float64Array<ArrayBufferLike>, N>;
|
|
22
|
-
export type Array<T extends Type> = Type & {
|
|
23
|
-
type: T;
|
|
24
|
-
length: number;
|
|
25
|
-
offset: Fn<number, number>;
|
|
26
|
-
};
|
|
27
|
-
export declare const isArray: (t: any) => t is Array<any>;
|
|
28
|
-
export declare const array: <T extends Type>(type: T, length: number, align?: number) => Array<T>;
|
|
29
|
-
type Field<T extends Type> = readonly [name: string, type: T, align?: number];
|
|
30
|
-
export type Struct<T extends ReadonlyArray<Field<any>>> = Type & {
|
|
31
|
-
fields: {
|
|
32
|
-
[K in T[number] as K[0]]: K[1];
|
|
33
|
-
};
|
|
34
|
-
offset(key: T[number][0]): number;
|
|
35
|
-
};
|
|
36
|
-
export declare const isStruct: (t: any) => t is Struct<any>;
|
|
37
|
-
export declare const struct: <T extends ReadonlyArray<Field<any>>>(fs: T, align?: number, offset?: number) => Struct<T>;
|
|
38
|
-
export type View<T extends Type> = T extends Vec<infer C, any> ? C : T extends Array<infer S> ? View<S>[] : T extends Struct<infer F> ? {
|
|
39
|
-
[K in F[number] as K[0]]: K[1] extends Vec<infer C, infer N> ? (N extends 1 ? (C extends NumberArray ? number : bigint) : C) : View<K[1]>;
|
|
40
|
-
} : never;
|
|
41
|
-
export declare const view: <T extends Vec<any, any> | Array<any> | Struct<any>>(t: T, buf?: ArrayBufferLike, offset?: number) => View<T>;
|
|
42
|
-
export {};
|
package/src/struct.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { align as _align } from "./math.js";
|
|
2
|
-
import { isBigIntArray, isNumberArray } from "./checks.js";
|
|
3
|
-
const vec = (type) => (length = 1, align = type.BYTES_PER_ELEMENT) => ({
|
|
4
|
-
type, align, length, size: type.BYTES_PER_ELEMENT * length,
|
|
5
|
-
});
|
|
6
|
-
export const u8 = vec(Uint8Array);
|
|
7
|
-
export const u16 = vec(Uint16Array);
|
|
8
|
-
export const u32 = vec(Uint32Array);
|
|
9
|
-
export const u64 = vec(BigUint64Array);
|
|
10
|
-
export const i8 = vec(Int8Array);
|
|
11
|
-
export const i16 = vec(Int16Array);
|
|
12
|
-
export const i32 = vec(Int32Array);
|
|
13
|
-
export const i64 = vec(BigInt64Array);
|
|
14
|
-
export const f16 = vec(Float16Array);
|
|
15
|
-
export const f32 = vec(Float32Array);
|
|
16
|
-
export const f64 = vec(Float64Array);
|
|
17
|
-
export const isArray = (t) => Object.hasOwn(t, "length") && Object.hasOwn(t, "offset");
|
|
18
|
-
export const array = (type, length, align = type.align) => {
|
|
19
|
-
const step = _align(type.size, align);
|
|
20
|
-
return ({
|
|
21
|
-
type, align, length,
|
|
22
|
-
size: step * length,
|
|
23
|
-
offset: i => i * step
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
export const isStruct = (t) => Object.hasOwn(t, "offset") && Object.hasOwn(t, "fields");
|
|
27
|
-
export const struct = (fs, align = 0, offset = 0) => {
|
|
28
|
-
const fields = {}, offsets = {};
|
|
29
|
-
for (const [name, type, align_ = type.align] of fs) {
|
|
30
|
-
align = Math.max(align, align_);
|
|
31
|
-
offset = _align(offset, align_);
|
|
32
|
-
fields[name] = type;
|
|
33
|
-
offsets[name] = offset;
|
|
34
|
-
offset += type.size;
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
size: _align(offset, align),
|
|
38
|
-
align,
|
|
39
|
-
fields: fields,
|
|
40
|
-
offset: k => offsets[k]
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
const property = (value) => ({
|
|
44
|
-
enumerable: true,
|
|
45
|
-
...((isNumberArray(value) || isBigIntArray(value)) && value.length == 1 ? {
|
|
46
|
-
get: () => value[0],
|
|
47
|
-
set: (x) => value[0] = x,
|
|
48
|
-
} : { value })
|
|
49
|
-
});
|
|
50
|
-
export const view = (t, buf = new ArrayBuffer(t.size), offset = 0) => {
|
|
51
|
-
if (isArray(t)) {
|
|
52
|
-
return Array.from({ length: t.length }, (_, i) => view(t.type, buf, offset + t.offset(i)));
|
|
53
|
-
}
|
|
54
|
-
else if (isStruct(t)) {
|
|
55
|
-
return Object.entries(t.fields).reduce((o, [name, type]) => Object.defineProperty(o, name, property(view(type, buf, offset + t.offset(name)))), {});
|
|
56
|
-
}
|
|
57
|
-
return new t.type(buf, offset, t.length);
|
|
58
|
-
};
|
package/src/tree.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Fn, FnV, TPred, PredV } from './types.js';
|
|
2
|
-
export type Node<S, T = S> = T | Tree<S, T>;
|
|
3
|
-
export type Tree<S, T = S> = [S, Node<S, T>[]];
|
|
4
|
-
export declare const tree: <S, T = S>(value: S, children?: Node<S, T>[]) => Tree<S, T>;
|
|
5
|
-
export declare const isTree: <S, T = S>(x: Node<S, T>) => x is Tree<S, T>;
|
|
6
|
-
export declare const children: <S, T = S>(x: Node<S, T>, p?: TPred<Tree<S, T>, Node<S, T>>) => Node<S, T>[];
|
|
7
|
-
export declare const value: <S, T = S>(x: Node<S, T>, p?: TPred<Tree<S, T>, Node<S, T>>) => S | T;
|
|
8
|
-
export declare const transform: <S, T = S, A = Tree<S, T>, B = Node<S, T>, C = void>(node: FnV<[Tree<S, T>, Fn<C, Fn<Node<S, T>>>, C], A>, leaf: FnV<[T, C], B>, p?: TPred<Tree<S, T>>) => Fn<C, Fn<Node<S, T>, A | B>>;
|
|
9
|
-
export declare const map: <S, T = S, U = S, V = T, C = void>(parent: FnV<[S, C], U>, child: FnV<[T, C], V>, update?: FnV<[C, S], C>, p?: TPred<Tree<S, T>>) => Fn<C, Fn<Node<S, T>, Node<U, V>>>;
|
|
10
|
-
export declare const filter: <S, T = S, C = void>(ps: PredV<[Node<S, T>, C]>, update?: FnV<[C, S], C>, p?: TPred<Tree<S, T>>) => Fn<C, Fn<Node<S, T>, T | Tree<S, T>>>;
|
|
11
|
-
export declare const zip: <S, T, U, V>(na: Node<S, T>[], nb: Node<U, V>[]) => Node<[S, U], [T, V]>[];
|
package/src/tree.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { identity } from './fn.js';
|
|
2
|
-
import { EMPTY } from './constants.js';
|
|
3
|
-
import { zip as zipI } from './iterable.js';
|
|
4
|
-
export const tree = (value, children = []) => [value, children];
|
|
5
|
-
export const isTree = (x) => Array.isArray(x) && x.length == 2 && Array.isArray(x[1]);
|
|
6
|
-
export const children = (x, p = isTree) => p(x) ? x[1] : EMPTY.LIST;
|
|
7
|
-
export const value = (x, p = isTree) => p(x) ? x[0] : x;
|
|
8
|
-
export const transform = (node, leaf, p = isTree) => {
|
|
9
|
-
const f = (c) => (x) => p(x) ? node(x, f, c) : leaf(x, c);
|
|
10
|
-
return f;
|
|
11
|
-
};
|
|
12
|
-
export const map = (parent, child, update = identity, p = isTree) => transform((x, f, c) => tree(parent(x[0], c), x[1].map(f(update(c, x[0])))), child, p);
|
|
13
|
-
export const filter = (ps, update = identity, p = isTree) => transform((x, f, c) => tree(x[0], x[1].filter(x => ps(x, c)).map(f(update(c, x[0])))), identity, p);
|
|
14
|
-
export const zip = (na, nb) => {
|
|
15
|
-
const res = [];
|
|
16
|
-
for (const [a, b] of zipI(Iterator.from(na), Iterator.from(nb))) {
|
|
17
|
-
if (!isTree(a) && !isTree(a))
|
|
18
|
-
res.push([value(a), value(b)]);
|
|
19
|
-
else if ((isTree(a) && isTree(a)))
|
|
20
|
-
res.push([[value(a), value(b)], zip(children(a), children(b))]);
|
|
21
|
-
else
|
|
22
|
-
throw new Error("[tree] a and b should be both tree or node");
|
|
23
|
-
}
|
|
24
|
-
return res;
|
|
25
|
-
};
|
package/src/treemap.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Maybe } from "./types.js";
|
|
2
|
-
export interface Treemap<T = number> {
|
|
3
|
-
set(node: T, parent?: T): Treemap<T>;
|
|
4
|
-
delete(key: T): void;
|
|
5
|
-
parent(key: T): Maybe<T>;
|
|
6
|
-
size: number;
|
|
7
|
-
children(key: T): T[];
|
|
8
|
-
roots(): IteratorObject<T>;
|
|
9
|
-
ancestors(node: T): IteratorObject<T>;
|
|
10
|
-
dfs(root: T): IteratorObject<T>;
|
|
11
|
-
bfs(): IteratorObject<T>;
|
|
12
|
-
}
|
|
13
|
-
export declare const treemap: <T = number>() => Treemap<T>;
|
package/src/treemap.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { before } from "./fn.js";
|
|
2
|
-
import { isDefined } from "./checks.js";
|
|
3
|
-
import { EMPTY } from "./constants.js";
|
|
4
|
-
export const treemap = () => {
|
|
5
|
-
const nodes = new Set();
|
|
6
|
-
const parents = new Map();
|
|
7
|
-
const children = new Map();
|
|
8
|
-
let changed = false;
|
|
9
|
-
const update = () => {
|
|
10
|
-
const hasChanged = changed;
|
|
11
|
-
if (hasChanged) {
|
|
12
|
-
children.clear();
|
|
13
|
-
for (const k of nodes) {
|
|
14
|
-
const p = parents.get(k);
|
|
15
|
-
if (isDefined(p)) {
|
|
16
|
-
if (!children.has(p))
|
|
17
|
-
children.set(p, []);
|
|
18
|
-
children.get(p).push(k);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
changed = false;
|
|
22
|
-
}
|
|
23
|
-
return hasChanged;
|
|
24
|
-
};
|
|
25
|
-
const updated = before(update);
|
|
26
|
-
const getChildren = updated((key) => children.get(key) ?? EMPTY.LIST);
|
|
27
|
-
const parent = updated((key) => parents.get(key));
|
|
28
|
-
const roots = () => nodes.values().filter(k => !parents.has(k));
|
|
29
|
-
return {
|
|
30
|
-
set(node, parent) {
|
|
31
|
-
nodes.add(node);
|
|
32
|
-
if (isDefined(parent)) {
|
|
33
|
-
nodes.add(parent);
|
|
34
|
-
parents.set(node, parent);
|
|
35
|
-
}
|
|
36
|
-
changed = true;
|
|
37
|
-
return this;
|
|
38
|
-
},
|
|
39
|
-
delete(key) {
|
|
40
|
-
nodes.delete(key);
|
|
41
|
-
parents.delete(key);
|
|
42
|
-
changed = true;
|
|
43
|
-
},
|
|
44
|
-
get size() {
|
|
45
|
-
update();
|
|
46
|
-
return nodes.size;
|
|
47
|
-
},
|
|
48
|
-
parent,
|
|
49
|
-
children: getChildren,
|
|
50
|
-
roots,
|
|
51
|
-
ancestors: updated(function* (node) {
|
|
52
|
-
for (let cur = node; isDefined(cur); cur = parent(cur))
|
|
53
|
-
yield cur;
|
|
54
|
-
}),
|
|
55
|
-
dfs: updated(function* (root) {
|
|
56
|
-
const stack = [root];
|
|
57
|
-
for (let node; stack.length > 0;) {
|
|
58
|
-
yield node = stack.pop();
|
|
59
|
-
const items = getChildren(node);
|
|
60
|
-
for (let i = items.length - 1; i >= 0; i--)
|
|
61
|
-
stack.push(items[i]);
|
|
62
|
-
}
|
|
63
|
-
}),
|
|
64
|
-
bfs: updated(function* (queue = [...roots()]) {
|
|
65
|
-
for (let node; queue.length > 0;) {
|
|
66
|
-
yield node = queue.shift();
|
|
67
|
-
queue.push(...getChildren(node));
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
|
-
};
|
|
71
|
-
};
|
package/src/typedarray.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { FnV, TypedArray, NumberArray, BufferType, Number } from "./types.js";
|
|
2
|
-
export interface Constructor<T extends TypedArray> {
|
|
3
|
-
new (length: number): T;
|
|
4
|
-
new (buffer: BufferType<T> | ArrayBufferLike, byteOffset?: number, length?: number): T;
|
|
5
|
-
readonly BYTES_PER_ELEMENT: number;
|
|
6
|
-
of(...items: Number<T>[]): T;
|
|
7
|
-
from(arrayLike: ArrayLike<Number<T>>): T;
|
|
8
|
-
}
|
|
9
|
-
export type View<T extends TypedArray> = FnV<[BufferType<T>?, number?], T>;
|
|
10
|
-
export declare const view: <T extends TypedArray>(type: Constructor<T>, length: number) => View<T>;
|
|
11
|
-
export declare const u8: (length: number) => View<Uint8Array<ArrayBufferLike>>;
|
|
12
|
-
export declare const u16: (length: number) => View<Uint16Array<ArrayBufferLike>>;
|
|
13
|
-
export declare const u32: (length: number) => View<Uint32Array<ArrayBufferLike>>;
|
|
14
|
-
export declare const u64: (length: number) => View<BigUint64Array<ArrayBufferLike>>;
|
|
15
|
-
export declare const i8: (length: number) => View<Int8Array<ArrayBufferLike>>;
|
|
16
|
-
export declare const i16: (length: number) => View<Int16Array<ArrayBufferLike>>;
|
|
17
|
-
export declare const i32: (length: number) => View<Int32Array<ArrayBufferLike>>;
|
|
18
|
-
export declare const i64: (length: number) => View<BigInt64Array<ArrayBufferLike>>;
|
|
19
|
-
export declare const f16: (length: number) => View<Float16Array<ArrayBufferLike>>;
|
|
20
|
-
export declare const f32: (length: number) => View<Float32Array<ArrayBufferLike>>;
|
|
21
|
-
export declare const f64: (length: number) => View<Float64Array<ArrayBufferLike>>;
|
|
22
|
-
export declare const UintArray: (n: number) => Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
|
|
23
|
-
export declare const ctor: <T extends TypedArray>(x: T) => Constructor<T>;
|
|
24
|
-
export declare const emptyLike: <T extends TypedArray>(x: T) => T;
|
|
25
|
-
export declare const set: <T extends TypedArray>(xs: T, x: ArrayLike<Number<T>> | T, i: number) => T;
|
|
26
|
-
export declare const concat: <T extends TypedArray>(chunks: T[]) => T;
|
|
27
|
-
export declare const iter: (size: number, stride?: number) => <T extends TypedArray>(data: T) => Generator<T>;
|
|
28
|
-
export declare const tile: (n: number) => (out: TypedArray, x: TypedArray) => TypedArray;
|
|
29
|
-
export declare const tileEvery: (m: number, n: number) => <T extends TypedArray>(xs: T, out?: T) => T;
|
|
30
|
-
export declare const strided: <T extends TypedArray>(data: T, size: number, stride?: number, out?: T) => T;
|
|
31
|
-
export declare const toFixed: (n: number, sep?: string) => (xs: NumberArray) => string;
|