@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/array.d.ts
CHANGED
|
@@ -7,7 +7,8 @@ export declare const pop: <T>(xs: T[]) => T[];
|
|
|
7
7
|
export declare const shift: <T>(xs: T[]) => T[];
|
|
8
8
|
export declare const first: <T>(xs: T[]) => T;
|
|
9
9
|
export declare const cap: <T>(n: number, f: Fn<T[]>) => Fn<T[], T[]>;
|
|
10
|
-
export declare const
|
|
10
|
+
export declare const sliding: <T>(n: number, xs?: T[]) => (x: T) => T[];
|
|
11
|
+
export declare const dropping: <T>(n: number, xs?: T[]) => (x: T) => T[];
|
|
11
12
|
export declare const tuple: <T, N extends number>(...xs: T[] & {
|
|
12
13
|
length: N;
|
|
13
14
|
}) => Tuple<T, N>;
|
package/array.js
CHANGED
|
@@ -11,7 +11,8 @@ export const cap = (n, f) => xs => {
|
|
|
11
11
|
f(xs);
|
|
12
12
|
return xs;
|
|
13
13
|
};
|
|
14
|
-
export const
|
|
14
|
+
export const sliding = (n, xs = []) => comp(scan((push), xs), cap(n, shift));
|
|
15
|
+
export const dropping = (n, xs = []) => comp(scan((push), xs), cap(n, pop));
|
|
15
16
|
export const tuple = (...xs) => xs;
|
|
16
17
|
export const seek = (arr, vals, offset = 0) => {
|
|
17
18
|
for (let i = offset, n = arr.length; i < n; i++)
|
package/async.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Fn,
|
|
1
|
+
import type { Fn, Lazy, Maybe, Dispose, MaybePromise } from "./types.js";
|
|
2
2
|
export declare const timeout: (ms: number, signal?: AbortSignal) => Promise<unknown>;
|
|
3
3
|
export declare const delay: (ms: number, signal?: AbortSignal) => <T>(x: T) => Promise<T>;
|
|
4
|
-
export declare const probing: <T>(p: Promise<T>) =>
|
|
4
|
+
export declare const probing: <T>(p: Promise<T>) => Lazy<Maybe<T>>;
|
|
5
5
|
export declare const resolved: <T>(p: Promise<T>) => (f: Fn<T>) => () => void;
|
|
6
6
|
export declare const promise: <T>(x: MaybePromise<T>) => Promise<any>;
|
|
7
7
|
export declare const then: <T>(p: Promise<T>) => <R>(f: Fn<T, R>) => Promise<R>;
|
|
8
|
-
export declare const dispose: (p: Promise<Dispose>) =>
|
|
8
|
+
export declare const dispose: (p: Promise<Dispose>) => () => void;
|
package/async.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { filterT,
|
|
1
|
+
import { filterT, noop } from "./fn.js";
|
|
2
2
|
import { isDefined } from "./checks.js";
|
|
3
3
|
export const timeout = (ms, signal) => new Promise((resolve, reject) => {
|
|
4
4
|
if (signal?.aborted)
|
|
@@ -28,4 +28,17 @@ export const resolved = (p) => {
|
|
|
28
28
|
};
|
|
29
29
|
export const promise = (x) => (x instanceof Promise) ? x : Promise.resolve(x);
|
|
30
30
|
export const then = (p) => (f) => p.then(f);
|
|
31
|
-
export const dispose = (p) =>
|
|
31
|
+
export const dispose = (p) => {
|
|
32
|
+
let dispose = noop;
|
|
33
|
+
let disposed = false;
|
|
34
|
+
p.then(d => {
|
|
35
|
+
if (disposed)
|
|
36
|
+
d();
|
|
37
|
+
else
|
|
38
|
+
dispose = d;
|
|
39
|
+
});
|
|
40
|
+
return () => {
|
|
41
|
+
disposed = true;
|
|
42
|
+
dispose();
|
|
43
|
+
};
|
|
44
|
+
};
|
package/buffer.d.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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;
|
|
1
|
+
import type { Maybe } from "./types.js";
|
|
2
|
+
export declare const fifo: <T>(n?: number) => {
|
|
3
|
+
put(x: T): Promise<number>;
|
|
4
|
+
take(): Promise<T>;
|
|
5
|
+
readonly size: number;
|
|
6
|
+
peek(): Maybe<T>;
|
|
7
|
+
};
|
package/buffer.js
CHANGED
|
@@ -1,54 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
1
|
+
import { isDefined } from "./checks.js";
|
|
2
|
+
export const fifo = (n = 1) => {
|
|
3
|
+
const data = [];
|
|
4
|
+
const pushes = [];
|
|
5
|
+
const takes = [];
|
|
5
6
|
return {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
},
|
|
7
|
+
async put(x) {
|
|
8
|
+
if (data.length >= n)
|
|
9
|
+
return new Promise(resolve => pushes.push(() => (data.push(x), resolve(data.length))));
|
|
10
|
+
const len = data.push(x);
|
|
11
|
+
const f = takes.shift();
|
|
12
|
+
if (isDefined(f))
|
|
13
|
+
f(x);
|
|
14
|
+
return Promise.resolve(len);
|
|
15
|
+
},
|
|
16
|
+
async take() {
|
|
17
|
+
if (data.length == 0)
|
|
18
|
+
return new Promise(resolve => takes.push(resolve));
|
|
19
|
+
const x = data.shift();
|
|
20
|
+
const f = pushes.shift();
|
|
21
|
+
if (isDefined(f))
|
|
22
|
+
f();
|
|
23
|
+
return Promise.resolve(x);
|
|
24
|
+
},
|
|
25
|
+
get size() { return data.length; },
|
|
26
|
+
peek() { return data[0]; }
|
|
53
27
|
};
|
|
54
28
|
};
|
package/channel.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Fn, FnV, Dispose, Structure, Pred } from "./types.js";
|
|
2
|
+
export interface Channel<T> {
|
|
3
|
+
next: Fn<T>;
|
|
4
|
+
on(next: Fn<T>, done?: Dispose): Dispose;
|
|
5
|
+
done: Dispose;
|
|
6
|
+
stateful(f: Fn<T, Dispose>, done?: Dispose): Dispose;
|
|
7
|
+
to<S>(f: Fn<Channel<T>, Channel<S>>): Channel<S>;
|
|
8
|
+
}
|
|
9
|
+
export declare const create: <T>(subs?: Set<{
|
|
10
|
+
next: Fn<T>;
|
|
11
|
+
done: Dispose;
|
|
12
|
+
}>) => Channel<T>;
|
|
13
|
+
export declare const cached: <T>(ch?: Channel<T>) => Channel<T>;
|
|
14
|
+
export interface Value<T> extends Channel<T> {
|
|
15
|
+
value: T;
|
|
16
|
+
}
|
|
17
|
+
export declare const value: <T>(x: T, ch?: Channel<T>) => Value<T>;
|
|
18
|
+
export declare const pipe: <T, S>(f: Fn<Fn<S>, Fn<T>>, ch?: Channel<S>) => Fn<Channel<T>, Channel<S>>;
|
|
19
|
+
export declare const map: <T, S>(f: Fn<T, S>, ch?: Channel<S>) => Fn<Channel<T>, Channel<S>>;
|
|
20
|
+
export declare const filter: <T>(p: Pred<T>, ch?: Channel<T>) => Fn<Channel<T>, Channel<T>>;
|
|
21
|
+
export declare const then: <T>(ch?: Channel<T>) => Fn<Channel<Promise<T>>, Channel<T>>;
|
|
22
|
+
export declare const dedup: <T, S = T>(key?: Fn<T, S>, ch?: Channel<T>) => Fn<Channel<T>, Channel<T>>;
|
|
23
|
+
export declare const gen: <T>(f: FnV<[Fn<T>, Dispose], Dispose | void>, ch?: Channel<T>) => Channel<T>;
|
|
24
|
+
export declare const reduce: <T, S>(f: FnV<[S, T], S>, a: S, ch?: Channel<S>) => (c: Channel<T>) => Channel<S>;
|
|
25
|
+
export declare const flatten: <T>(ch?: Channel<T>) => (cs: Channel<Channel<T>>) => Channel<T>;
|
|
26
|
+
export declare const latest: <T>(ch?: Channel<T>) => (cs: Channel<Channel<T>>) => Channel<T>;
|
|
27
|
+
export declare const microOn: <T>(ch?: Channel<T>) => Channel<T>;
|
|
28
|
+
type F2C<T> = T extends Fn<infer U> ? Channel<U> : T extends readonly (infer U)[] ? readonly F2C<U>[] : {
|
|
29
|
+
[K in keyof T]: F2C<T[K]>;
|
|
30
|
+
};
|
|
31
|
+
export declare const derive: <T, F extends Structure<Fn<any>>>(cs: F2C<F>, f: Fn<Fn<T>, F>, ch?: Channel<T>) => Channel<T>;
|
|
32
|
+
export interface Reactive<T> extends Value<T> {
|
|
33
|
+
set: Fn<T | Fn<T, T>>;
|
|
34
|
+
update: Fn<Fn<T>>;
|
|
35
|
+
}
|
|
36
|
+
export declare const reactive: <T>(x: T) => Reactive<T>;
|
|
37
|
+
export {};
|
package/channel.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import * as F from "./fn.js";
|
|
2
|
+
import { isFunction } from "./checks.js";
|
|
3
|
+
import { add } from "./set.js";
|
|
4
|
+
const channel = (next, on, done = F.noop) => ({
|
|
5
|
+
next, on, done,
|
|
6
|
+
to(f) { return f(this); },
|
|
7
|
+
stateful(next, done = F.noop) {
|
|
8
|
+
const swap = F.swapping();
|
|
9
|
+
return on(swap(next), swap(done));
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
export const create = (subs = new Set()) => channel((x) => subs.forEach(s => s.next(x)), (next, done = F.noop) => {
|
|
13
|
+
const del = add(subs, { next, done });
|
|
14
|
+
return () => del() && done();
|
|
15
|
+
}, () => (subs.forEach(s => s.done()), subs.clear()));
|
|
16
|
+
const None = Symbol('None');
|
|
17
|
+
export const cached = (ch = create()) => {
|
|
18
|
+
let x = None;
|
|
19
|
+
return channel(y => ch.next(x = y), (next, done) => {
|
|
20
|
+
if (x !== None)
|
|
21
|
+
next(x);
|
|
22
|
+
return ch.on(next, done);
|
|
23
|
+
}, ch.done);
|
|
24
|
+
};
|
|
25
|
+
export const value = (x, ch = create()) => Object.defineProperties(channel(y => ch.next(x = y), (next, done) => (next(x), ch.on(next, done)), ch.done), {
|
|
26
|
+
value: {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: () => x,
|
|
29
|
+
set: (y) => x = y
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
export const pipe = (f, ch = create()) => c => channel(ch.next, ch.on, c.on(f(ch.next), ch.done));
|
|
33
|
+
export const map = (f, ch) => pipe(F.bind(F.comp, f), ch);
|
|
34
|
+
export const filter = (p, ch) => pipe(F.bind(F.filter, p), ch);
|
|
35
|
+
export const then = (ch) => pipe(next => p => p.then(next), ch);
|
|
36
|
+
export const dedup = (key = F.identity, ch) => pipe(next => {
|
|
37
|
+
let x = None;
|
|
38
|
+
return (y) => {
|
|
39
|
+
const k = key(y);
|
|
40
|
+
if (k !== x)
|
|
41
|
+
next(y);
|
|
42
|
+
x = k;
|
|
43
|
+
};
|
|
44
|
+
}, ch);
|
|
45
|
+
export const gen = (f, ch = create()) => {
|
|
46
|
+
let ended = false;
|
|
47
|
+
let done = () => { ended = true; };
|
|
48
|
+
done = F.forEach(f(ch.next, () => done()) ?? F.noop, ch.done);
|
|
49
|
+
return ended ? channel(F.noop, F.constantly(F.noop)) : channel(ch.next, ch.on, done);
|
|
50
|
+
};
|
|
51
|
+
export const reduce = (f, a, ch = create()) => (c) => channel(F.noop, ch.on, c.on(F.scan(f, a), F.forEach(() => ch.next(a), ch.done)));
|
|
52
|
+
export const flatten = (ch = create()) => (cs) => {
|
|
53
|
+
const ds = new Set();
|
|
54
|
+
return channel(ch.next, ch.on, cs.on(c => (ds.add(c.done), c.on(ch.next)), F.forEach(ch.done, () => ds.forEach(d => d()))));
|
|
55
|
+
};
|
|
56
|
+
export const latest = (ch = create()) => (cs) => channel(ch.next, ch.on, cs.stateful(c => c.on(ch.next, c.done), ch.done));
|
|
57
|
+
export const microOn = (ch = create()) => channel(ch.next, (next, done = F.noop) => {
|
|
58
|
+
const ctrl = new AbortController();
|
|
59
|
+
let off = () => (ctrl.abort(), done());
|
|
60
|
+
queueMicrotask(() => off = ctrl.signal.aborted ? F.noop : ch.on(next, done));
|
|
61
|
+
return () => off();
|
|
62
|
+
}, ch.done);
|
|
63
|
+
const isChannel = (c) => Object.hasOwn(c, "next") && isFunction(c.next) && Object.hasOwn(c, "on") && isFunction(c.on) && Object.hasOwn(c, "next") && isFunction(c.next);
|
|
64
|
+
export const derive = (cs, f, ch = create()) => {
|
|
65
|
+
let done = F.noop;
|
|
66
|
+
const rec = (cs, fs) => {
|
|
67
|
+
if (isFunction(fs) && isChannel(cs))
|
|
68
|
+
return cs.on(fs, () => done());
|
|
69
|
+
else if (Array.isArray(cs) && Array.isArray(fs))
|
|
70
|
+
return F.forEach(...cs.map((c, i) => rec(c, fs[i])));
|
|
71
|
+
return F.forEach(...Object.entries(cs).map(([k, v]) => rec(v, fs[k])));
|
|
72
|
+
};
|
|
73
|
+
done = F.forEach(rec(cs, f(ch.next)), ch.done);
|
|
74
|
+
return channel(ch.next, ch.on, done);
|
|
75
|
+
};
|
|
76
|
+
export const reactive = (x) => {
|
|
77
|
+
const ch = value(x);
|
|
78
|
+
return Object.assign(ch, {
|
|
79
|
+
set: (y) => ch.next(isFunction(y) ? y(ch.value) : y),
|
|
80
|
+
update: (f) => (f(ch.value), ch.next(ch.value)),
|
|
81
|
+
});
|
|
82
|
+
};
|
package/checks.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { TypedArray, NumberArray, BigIntArray, Pred, TPred, Maybe } from ".
|
|
|
2
2
|
export declare const isTypedArray: (x: any) => x is TypedArray;
|
|
3
3
|
export declare const isNumberArray: (x: any) => x is NumberArray;
|
|
4
4
|
export declare const isBigIntArray: (x: any) => x is BigIntArray;
|
|
5
|
+
export declare const isBoolean: (x: any) => x is boolean;
|
|
5
6
|
export declare const isUndefined: (x: any) => x is undefined;
|
|
6
7
|
export declare const isNull: (x: any) => x is null;
|
|
7
8
|
export declare const isDefined: <T>(x: Maybe<T>) => x is T;
|
package/checks.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { comp, not, apply, and } from "./fn.js";
|
|
2
|
-
import {
|
|
2
|
+
import { sliding } from "./array.js";
|
|
3
3
|
export const isTypedArray = (x) => x && (x instanceof Uint8Array || x instanceof Uint8ClampedArray ||
|
|
4
4
|
x instanceof Uint16Array || x instanceof Uint32Array ||
|
|
5
5
|
x instanceof Int8Array || x instanceof Int16Array || x instanceof Int32Array ||
|
|
@@ -10,6 +10,7 @@ export const isNumberArray = (x) => x && (x instanceof Uint8Array || x instanceo
|
|
|
10
10
|
x instanceof Int8Array || x instanceof Int16Array || x instanceof Int32Array ||
|
|
11
11
|
x instanceof Float16Array || x instanceof Float32Array || x instanceof Float64Array);
|
|
12
12
|
export const isBigIntArray = (x) => x && (x instanceof BigUint64Array || x instanceof BigInt64Array);
|
|
13
|
+
export const isBoolean = (x) => typeof x === "boolean";
|
|
13
14
|
export const isUndefined = (x) => x === undefined;
|
|
14
15
|
export const isNull = (x) => x === null;
|
|
15
16
|
export const isDefined = (x) => !isUndefined(x);
|
|
@@ -28,8 +29,8 @@ export const isclose = (a, b, eps = Number.EPSILON) => Math.abs(a - b) < eps;
|
|
|
28
29
|
export const allclose = (a, b, eps) => a.length == b.length && a.every((x, i) => isclose(x, b[i], eps));
|
|
29
30
|
export const equal = (a, b) => a == b;
|
|
30
31
|
export const strictEqual = (a, b) => a === b;
|
|
31
|
-
export const isDiffPrev = (eq = (strictEqual)) => comp(
|
|
32
|
-
export const isLonger = (delta) => comp((x) => [performance.now(), x],
|
|
32
|
+
export const isDiffPrev = (eq = (strictEqual)) => comp(sliding(2), not(apply(eq)));
|
|
33
|
+
export const isLonger = (delta) => comp((x) => [performance.now(), x], sliding(2), (xs) => xs.length < 2 || xs[1][0] - xs[0][0] > delta);
|
|
33
34
|
export const isFunction = (x) => typeof x === "function";
|
|
34
35
|
export const isAborted = (signal) => () => signal.aborted;
|
|
35
36
|
export const isArrayBufferLike = (x) => x instanceof ArrayBuffer || x instanceof SharedArrayBuffer;
|
package/dom.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Fn, Dispose, RecordOf, Maybe } from "./types.js";
|
|
1
|
+
import type { Fn, FnV, Dispose, RecordOf, Maybe } from "./types.js";
|
|
2
2
|
export declare const customEvent: <T>(type: string, detail: T, { composed, bubbles, cancelable }?: EventInit) => CustomEvent<T>;
|
|
3
3
|
type ListenOpts = boolean | AddEventListenerOptions;
|
|
4
4
|
export declare const on: {
|
|
@@ -36,15 +36,34 @@ export declare const onAll: {
|
|
|
36
36
|
type DragHandlers = Record<'down' | 'move' | 'up', Fn<PointerEvent>>;
|
|
37
37
|
export declare const onDrag: {
|
|
38
38
|
(el: HTMLElement, fns: Partial<DragHandlers>): Dispose;
|
|
39
|
-
(el:
|
|
39
|
+
(el: SVGElement, fns: Partial<DragHandlers>): Dispose;
|
|
40
40
|
};
|
|
41
|
+
export declare const onDragX: (el: HTMLElement, f: Fn<number>) => Dispose;
|
|
41
42
|
export declare const onDragMove: {
|
|
42
43
|
(el: HTMLElement, f: Fn<PointerEvent>): Dispose;
|
|
43
44
|
(el: SVGGraphicsElement, f: Fn<PointerEvent>): Dispose;
|
|
44
45
|
};
|
|
45
46
|
export declare const onResize: (el: Element, f: Fn<ResizeObserverEntry[]>, options?: ResizeObserverOptions) => Dispose;
|
|
47
|
+
export declare const onSelectChange: <T extends string>(el: HTMLSelectElement, f: FnV<[T, number]>, opts?: ListenOpts) => Dispose;
|
|
46
48
|
export declare const preventDefault: <T extends Event>(e: T) => void;
|
|
47
49
|
export declare const loadImage: (img: HTMLImageElement, src: string) => Promise<HTMLImageElement>;
|
|
48
50
|
export declare const pushState: (path: string, query?: RecordOf<string>, data?: any) => void;
|
|
49
51
|
export declare const qs: (search?: string) => Maybe<RecordOf<string>>;
|
|
52
|
+
export declare const imageData: (img: HTMLImageElement) => ImageData;
|
|
53
|
+
export type Child = ChildNode | string;
|
|
54
|
+
export declare const append: (el: ParentNode, ...children: Child[]) => () => void;
|
|
55
|
+
export type Attribs = Record<string, string | number | boolean>;
|
|
56
|
+
export declare const setAttrs: (el: Element, attrs: Attribs) => void;
|
|
57
|
+
export declare const css: (styles: Record<string, {
|
|
58
|
+
toString(): string;
|
|
59
|
+
}>) => string;
|
|
60
|
+
export declare const style: <E extends HTMLElement>(el: E, props: Record<string, {
|
|
61
|
+
toString(): string;
|
|
62
|
+
}>) => E;
|
|
63
|
+
type Tags<T> = {
|
|
64
|
+
[K in keyof T]: (attrs?: Attribs | Child, ...rest: Child[]) => T[K];
|
|
65
|
+
};
|
|
66
|
+
export declare const h: Tags<HTMLElementTagNameMap>;
|
|
67
|
+
export declare const s: Tags<SVGElementTagNameMap>;
|
|
68
|
+
export declare const m: Tags<MathMLElementTagNameMap>;
|
|
50
69
|
export {};
|
package/dom.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { isDefined } from "./checks.js";
|
|
1
|
+
import * as F from "./fn.js";
|
|
2
|
+
import { isDefined, isString, isBoolean } from "./checks.js";
|
|
3
3
|
import { EMPTY } from "./constants.js";
|
|
4
4
|
export const customEvent = (type, detail, { composed = false, bubbles = false, cancelable = false } = EMPTY.OBJ) => new CustomEvent(type, { bubbles, cancelable, composed, detail });
|
|
5
5
|
export const on = (el, name, listener, opts = false) => {
|
|
6
6
|
el.addEventListener(name, listener, opts);
|
|
7
7
|
return () => el.removeEventListener(name, listener, opts);
|
|
8
8
|
};
|
|
9
|
-
export const onAll = (el, fs, opts) => forEach(...Object.keys(fs).map(k => on(el, k, fs[k], opts)));
|
|
10
|
-
export const onDrag = (el, { down = noop, move = noop, up = noop }) => on(el, 'pointerdown', e => {
|
|
9
|
+
export const onAll = (el, fs, opts) => F.forEach(...Object.keys(fs).map(k => on(el, k, fs[k], opts)));
|
|
10
|
+
export const onDrag = (el, { down = F.noop, move = F.noop, up = F.noop }) => on(el, 'pointerdown', e => {
|
|
11
11
|
const pointerId = e.pointerId;
|
|
12
12
|
el.setPointerCapture(pointerId);
|
|
13
13
|
down(e);
|
|
@@ -19,12 +19,18 @@ export const onDrag = (el, { down = noop, move = noop, up = noop }) => on(el, 'p
|
|
|
19
19
|
offUp();
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
|
+
export const onDragX = (el, f) => onDrag(el, {
|
|
23
|
+
down: e => f(e.offsetX / el.clientWidth),
|
|
24
|
+
move: e => f(e.offsetX / el.clientWidth),
|
|
25
|
+
up: e => f(e.offsetX / el.clientWidth),
|
|
26
|
+
});
|
|
22
27
|
export const onDragMove = (el, move) => onDrag(el, { move });
|
|
23
28
|
export const onResize = (el, f, options) => {
|
|
24
29
|
const ro = new ResizeObserver(f);
|
|
25
30
|
ro.observe(el, options);
|
|
26
31
|
return () => ro.disconnect();
|
|
27
32
|
};
|
|
33
|
+
export const onSelectChange = (el, f, opts) => on(el, "change", () => f(el.value, el.selectedIndex), opts);
|
|
28
34
|
export const preventDefault = (e) => e.preventDefault();
|
|
29
35
|
export const loadImage = (img, src) => new Promise((resolve) => {
|
|
30
36
|
const off = on(img, "load", () => (off(), resolve(img)));
|
|
@@ -34,3 +40,60 @@ export const pushState = (path, query, data) => history.pushState(data, EMPTY.ST
|
|
|
34
40
|
export const qs = (search = location.search) => search.length > 0
|
|
35
41
|
? Object.fromEntries(new URLSearchParams(search).entries())
|
|
36
42
|
: undefined;
|
|
43
|
+
export const imageData = (img) => {
|
|
44
|
+
const ctx = new OffscreenCanvas(img.width, img.height).getContext("2d");
|
|
45
|
+
ctx.drawImage(img, 0, 0);
|
|
46
|
+
return ctx.getImageData(0, 0, img.width, img.height);
|
|
47
|
+
};
|
|
48
|
+
export const append = (el, ...children) => {
|
|
49
|
+
const els = children.map(x => isString(x) ? document.createTextNode(x) : x);
|
|
50
|
+
el.append(...els);
|
|
51
|
+
return () => els.forEach(x => x.remove());
|
|
52
|
+
};
|
|
53
|
+
export const setAttrs = (el, attrs) => Object.entries(attrs).forEach(([k, v]) => {
|
|
54
|
+
if (isBoolean(v)) {
|
|
55
|
+
if (el instanceof HTMLInputElement) {
|
|
56
|
+
if (k === "checked")
|
|
57
|
+
el.checked = v;
|
|
58
|
+
if (k === "disabled")
|
|
59
|
+
el.disabled = v;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
el.toggleAttribute(k, v);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else
|
|
66
|
+
el.setAttribute(k, v.toString());
|
|
67
|
+
});
|
|
68
|
+
export const css = (styles) => Object.entries(styles).map(([prop, value]) => `${prop}: ${value};`).join('');
|
|
69
|
+
export const style = (el, props) => (Object.entries(props).forEach(([k, v]) => el.style.setProperty(k, v.toString())), el);
|
|
70
|
+
const isAttrs = (obj) => (obj !== null &&
|
|
71
|
+
typeof obj === 'object' &&
|
|
72
|
+
!(obj instanceof Node) &&
|
|
73
|
+
!Array.isArray(obj));
|
|
74
|
+
const element = (el, attrs, ...rest) => {
|
|
75
|
+
if (isDefined(attrs)) {
|
|
76
|
+
let children = rest;
|
|
77
|
+
if (isAttrs(attrs))
|
|
78
|
+
setAttrs(el, attrs);
|
|
79
|
+
else
|
|
80
|
+
children = [attrs, ...children];
|
|
81
|
+
append(el, ...children);
|
|
82
|
+
}
|
|
83
|
+
return el;
|
|
84
|
+
};
|
|
85
|
+
const NS = {
|
|
86
|
+
HTML: "http://www.w3.org/1999/xhtml",
|
|
87
|
+
SVG: "http://www.w3.org/2000/svg",
|
|
88
|
+
MathML: "http://www.w3.org/1998/Math/MathML",
|
|
89
|
+
};
|
|
90
|
+
const tag = (ns, name) => document.createElementNS(NS[ns], name);
|
|
91
|
+
export const h = new Proxy({}, ({
|
|
92
|
+
get: (_, name) => F.bind((element), tag("HTML", name))
|
|
93
|
+
}));
|
|
94
|
+
export const s = new Proxy({}, {
|
|
95
|
+
get: (_, name) => F.bind((element), tag("SVG", name))
|
|
96
|
+
});
|
|
97
|
+
export const m = new Proxy({}, {
|
|
98
|
+
get: (_, name) => F.bind((element), tag("MathML", name))
|
|
99
|
+
});
|
package/fn.d.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
import type { Fn, FnV, FnF, Pred, TPred,
|
|
1
|
+
import type { Fn, FnV, FnF, Pred, TPred, Last, Lazy, Dispose, Reactive } from "./types.js";
|
|
2
2
|
export declare const tee: <T>(f: Fn<T, void>) => Fn<T, T>;
|
|
3
|
-
export declare const comp: <Fs extends Fn[]>(...fs: Fs) => (x:
|
|
3
|
+
export declare const comp: <Fs extends Fn[]>(...fs: Fs) => (x: Fs[0] extends Fn<infer T> ? T : never) => 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;
|
|
5
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
6
|
export declare const and: <T>(...fs: Pred<T>[]) => (x: T) => boolean;
|
|
8
7
|
export declare const or: <T>(...fs: Pred<T>[]) => (x: T) => boolean;
|
|
9
8
|
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
9
|
export declare const noop: () => void;
|
|
12
10
|
export declare const identity: <S, T = S>(x: S) => T;
|
|
13
|
-
export declare const constantly: <T>(x: T) =>
|
|
11
|
+
export declare const constantly: <T>(x: T) => Lazy<T>;
|
|
14
12
|
export declare const apply: <F extends FnV>(f: F) => (x: Parameters<F>) => ReturnType<F>;
|
|
15
13
|
export declare const call: <T extends unknown[]>(f: Fn<T>) => (...xs: T) => ReturnType<typeof f>;
|
|
16
14
|
export declare const bind: <A extends unknown[], B extends unknown[], R>(f: (...args: [...A, ...B]) => R, ...args: A) => (...args: B) => R;
|
|
@@ -18,16 +16,14 @@ export declare const lazy: <F extends FnV>(f: F, ...xs: Parameters<F>) => Fn<voi
|
|
|
18
16
|
export declare const where: <T, U, V>(p: Pred<T>, f: Fn<T, U>, g: Fn<T, V>) => (x: T) => U | V;
|
|
19
17
|
export declare const before: <F extends Fn<void>>(f: F) => FnF;
|
|
20
18
|
export declare const after: <F extends Fn<void>>(f: F) => FnF;
|
|
21
|
-
export declare const mapForEach: <T, R>(...fs: Fn<T, Fn<R>>[]) => (x: T) => (x: R) => void;
|
|
22
19
|
export declare const filter: <T>(p: Pred<T>, f: Fn<T>) => (x: T) => void;
|
|
23
20
|
export declare const filterT: <T>(p: TPred<T>, f: Fn<T>) => <S>(x: S) => void;
|
|
24
21
|
export declare const scan: <T, S>(f: FnV<[S, T], S>, a: S) => Fn<T, S>;
|
|
25
22
|
export declare const memoize: <T extends any[], K, V>(f: FnV<T, V>, key?: FnV<T, K>, cache?: Map<K, V>) => (...xs: T) => NonNullable<V>;
|
|
26
23
|
export declare const abortable: <T = any>(f: Fn<AbortSignal>) => (reason?: T) => void;
|
|
27
24
|
export declare const disposable: (f: Fn<Fn<Dispose, Dispose>>, disposes?: Set<Dispose>) => Dispose;
|
|
28
|
-
export declare const
|
|
29
|
-
export declare const disposeWith: <T>(f: Fn<T, Dispose>) => (g: Fn<Fn<T>>) => Dispose;
|
|
25
|
+
export declare const swapping: () => <F extends FnV<any[], Dispose | void>>(f: F) => FnV<Parameters<F>, Dispose>;
|
|
30
26
|
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>) =>
|
|
27
|
+
export declare const toggler: (f: Fn<void, Dispose>) => Fn<boolean, Dispose>;
|
|
32
28
|
export declare const voidify: <F extends FnV>(f: F) => (...xs: Parameters<F>) => void;
|
|
33
29
|
export declare const effectual: <T>(f: Reactive<T>, g: Fn<T>) => Dispose;
|
package/fn.js
CHANGED
|
@@ -3,11 +3,9 @@ export const tee = f => x => (f(x), x);
|
|
|
3
3
|
export const comp = (...fs) => (x) => fs.reduce((x, f) => f(x), x);
|
|
4
4
|
export const pipe = (x, ...fs) => fs.reduce((x, f) => f(x), x);
|
|
5
5
|
export const forEach = (...fs) => (x) => fs.forEach(f => f(x));
|
|
6
|
-
export const map = (...fs) => (x) => fs.map(f => f(x));
|
|
7
6
|
export const and = (...fs) => (x) => fs.reduce((a, f) => a && f(x), true);
|
|
8
7
|
export const or = (...fs) => (x) => fs.reduce((a, f) => a || f(x), false);
|
|
9
8
|
export const not = (p) => (x) => !p(x);
|
|
10
|
-
export const comb = (c) => (...fs) => (x) => c(...fs.map(f => f(x)));
|
|
11
9
|
export const noop = () => { };
|
|
12
10
|
export const identity = (x) => x;
|
|
13
11
|
export const constantly = (x) => () => x;
|
|
@@ -22,7 +20,6 @@ export const after = (f) => g => (...xs) => {
|
|
|
22
20
|
f();
|
|
23
21
|
return r;
|
|
24
22
|
};
|
|
25
|
-
export const mapForEach = (...fs) => comp(map(...fs), apply((forEach)));
|
|
26
23
|
export const filter = (p, f) => (x) => { if (p(x))
|
|
27
24
|
f(x); };
|
|
28
25
|
export const filterT = (p, f) => (x) => { if (p(x))
|
|
@@ -43,19 +40,19 @@ export const disposable = (f, disposes = new Set()) => {
|
|
|
43
40
|
f(bind(add, disposes));
|
|
44
41
|
return () => (disposes.forEach(g => g()), disposes.clear());
|
|
45
42
|
};
|
|
46
|
-
export const
|
|
43
|
+
export const swapping = () => {
|
|
47
44
|
let dispose = noop;
|
|
48
45
|
const stop = () => dispose();
|
|
49
46
|
return (f) => (...xs) => (stop(), dispose = f(...xs) ?? noop, stop);
|
|
50
47
|
};
|
|
51
|
-
export const
|
|
52
|
-
export const idemp = (d, f) => disposing()((...xs) => {
|
|
48
|
+
export const idemp = (d, f) => swapping()((...xs) => {
|
|
53
49
|
const cleanup = f(...xs) ?? noop;
|
|
54
50
|
return forEach(cleanup, d(cleanup));
|
|
55
51
|
});
|
|
56
52
|
export const toggler = (f) => {
|
|
57
53
|
let cancel = noop;
|
|
58
|
-
|
|
54
|
+
const stop = () => cancel();
|
|
55
|
+
return (x) => (stop(), (cancel = x ? f() : noop), stop);
|
|
59
56
|
};
|
|
60
57
|
export const voidify = (f) => (...xs) => { f(...xs); };
|
|
61
58
|
export const effectual = (f, g) => disposable(d => d(f(idemp(d, g))));
|
package/math.d.ts
CHANGED
|
@@ -32,3 +32,5 @@ export declare const zoom: (x: number, s: number, p: number) => number;
|
|
|
32
32
|
export declare const align: (x: number, b: number) => number;
|
|
33
33
|
export declare const sign11: (x: number) => 1 | -1;
|
|
34
34
|
export declare const nudge: (x: number, v?: number, eps?: number) => number;
|
|
35
|
+
export declare const divCeil: (a: number, b: number) => number;
|
|
36
|
+
export declare const trunc: (num: number, precision: number) => number;
|
package/math.js
CHANGED
|
@@ -41,3 +41,8 @@ export const zoom = (x, s, p) => (x - p) * s + p;
|
|
|
41
41
|
export const align = (x, b) => (x + b - 1) & -b;
|
|
42
42
|
export const sign11 = (x) => x >= 0 ? 1 : -1;
|
|
43
43
|
export const nudge = (x, v = 0, eps = EPS) => isclose(x, v, eps) ? sign11(x - v) * eps + v : x;
|
|
44
|
+
export const divCeil = (a, b) => Math.ceil(a / b);
|
|
45
|
+
export const trunc = (num, precision) => {
|
|
46
|
+
const factor = Math.pow(10, precision);
|
|
47
|
+
return Math.trunc(num * factor) / factor;
|
|
48
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cgtk/std",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.196",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"exports": {
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
"./checks": "./checks.js",
|
|
10
10
|
"./fn": "./fn.js",
|
|
11
11
|
"./object": "./object.js",
|
|
12
|
-
"./signal": "./signal.js",
|
|
13
12
|
"./assign": "./assign.js",
|
|
14
13
|
"./map": "./map.js",
|
|
15
14
|
"./set": "./set.js",
|
|
@@ -25,6 +24,7 @@
|
|
|
25
24
|
"./stream": "./stream.js",
|
|
26
25
|
"./progress": "./progress.js",
|
|
27
26
|
"./constants": "./constants.js",
|
|
27
|
+
"./channel": "./channel.js",
|
|
28
28
|
"./http": "./http.js",
|
|
29
29
|
"./json": "./json.js",
|
|
30
30
|
"./dom": "./dom.js",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"./math": "./math.js",
|
|
33
33
|
"./basen": "./basen.js",
|
|
34
34
|
"./struct": "./struct.js",
|
|
35
|
-
"./
|
|
35
|
+
"./bytes": "./bytes.js",
|
|
36
36
|
"./base64": "./base64.js",
|
|
37
37
|
"./npy": "./npy.js",
|
|
38
38
|
"./port": "./port.js",
|
|
@@ -40,9 +40,6 @@
|
|
|
40
40
|
"./resource": "./resource.js",
|
|
41
41
|
"./utils": "./utils.js"
|
|
42
42
|
},
|
|
43
|
-
"devDependencies": {
|
|
44
|
-
"typescript": "^6.0.2"
|
|
45
|
-
},
|
|
46
43
|
"publishConfig": {
|
|
47
44
|
"access": "public"
|
|
48
45
|
}
|
package/resource.d.ts
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import type { Fn, Dispose, Structure, Reactive } from "./types.js";
|
|
1
|
+
import type { Fn, FnV, Dispose, Structure, Reactive, RecordOf } from "./types.js";
|
|
2
2
|
export type Resource<T> = Reactive<T, Dispose | void> & {
|
|
3
3
|
value: T;
|
|
4
4
|
dispose: Dispose;
|
|
5
5
|
map<S>(f: Fn<T, S>): Resource<S>;
|
|
6
|
+
flatMap<S>(f: Fn<T, Resource<S>>): Resource<S>;
|
|
7
|
+
promise(f: Fn<T, Promise<Dispose | void>>): Promise<Dispose>;
|
|
6
8
|
};
|
|
7
9
|
export declare const resource: <T>(value: T, dispose?: Dispose) => Resource<T>;
|
|
10
|
+
export declare const awaited: <T>({ value, dispose }: Resource<Promise<T>>) => Promise<Resource<Awaited<T>>>;
|
|
8
11
|
type Unwrapped<T> = T extends Resource<infer U> ? U : {
|
|
9
12
|
[K in keyof T]: Unwrapped<T[K]>;
|
|
10
13
|
};
|
|
11
14
|
export declare const compose: <R extends Structure<Resource<any>>>(rs: R) => Resource<Unwrapped<R>>;
|
|
15
|
+
export declare const of: <T>(x: T, f: Fn<T, Dispose>) => Resource<T>;
|
|
16
|
+
export declare const gen: <R>(f: FnV<[<T>(r: Resource<T>) => T, Fn<Dispose, Dispose>], R>) => Resource<R>;
|
|
17
|
+
export declare const consume: (f: Fn<(<T>(r: Resource<T>) => T), Dispose | void>) => Dispose;
|
|
18
|
+
export declare const swapping: <F extends RecordOf<FnV<any, Dispose | void>>>(fs: F) => Resource<{ [K in import("./types.js").Entry<keyof F, FnV<Parameters<import("./types.js").ValueOf<F>>, Dispose>> as K[0]]: K[1]; }>;
|
|
12
19
|
export {};
|