@cgtk/std 0.0.192 → 0.0.194
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/array.d.ts +1 -1
- package/array.js +1 -1
- package/assign.d.ts +1 -1
- package/assign.js +1 -1
- package/async.d.ts +3 -3
- package/async.js +5 -4
- package/basen.js +1 -1
- package/buffer.d.ts +1 -1
- package/buffer.js +1 -1
- package/checks.d.ts +2 -2
- package/checks.js +2 -2
- package/dom.d.ts +2 -2
- package/dom.js +3 -3
- package/fn.d.ts +3 -3
- package/fn.js +3 -3
- package/http.d.ts +6 -7
- package/http.js +11 -15
- package/iterable.d.ts +1 -1
- package/iterable.js +3 -3
- package/json.d.ts +1 -1
- package/json.js +1 -1
- package/map.d.ts +3 -3
- package/map.js +2 -2
- package/math.js +1 -1
- package/npy.d.ts +1 -1
- package/number.js +1 -1
- package/object.d.ts +2 -2
- package/package.json +2 -2
- package/port.d.ts +2 -1
- package/port.js +7 -4
- package/progress.d.ts +1 -1
- package/rect.d.ts +1 -1
- package/rect.js +1 -1
- package/resource.d.ts +10 -8
- package/resource.js +22 -15
- package/schedule.d.ts +1 -1
- package/schedule.js +2 -2
- package/set.d.ts +1 -0
- package/set.js +1 -0
- package/signal.d.ts +18 -5
- package/signal.js +56 -15
- package/src/array.d.ts +14 -0
- package/src/array.js +21 -0
- package/src/assign.d.ts +8 -0
- package/src/assign.js +26 -0
- package/src/async.d.ts +8 -0
- package/src/async.js +31 -0
- package/src/base64.d.ts +1 -0
- package/src/base64.js +7 -0
- package/src/basen.d.ts +12 -0
- package/src/basen.js +31 -0
- package/src/buffer.d.ts +14 -0
- package/src/buffer.js +54 -0
- package/src/checks.d.ts +29 -0
- package/src/checks.js +44 -0
- package/src/constants.d.ts +5 -0
- package/src/constants.js +1 -0
- package/src/dom.d.ts +50 -0
- package/src/dom.js +36 -0
- package/src/fn.d.ts +33 -0
- package/src/fn.js +61 -0
- package/src/http.d.ts +9 -0
- package/src/http.js +33 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/iterable.d.ts +43 -0
- package/src/iterable.js +158 -0
- package/src/json.d.ts +1 -0
- package/src/json.js +1 -0
- package/src/map.d.ts +12 -0
- package/src/map.js +35 -0
- package/src/math.d.ts +34 -0
- package/src/math.js +43 -0
- package/src/npy.d.ts +10 -0
- package/src/npy.js +29 -0
- package/src/number.d.ts +4 -0
- package/src/number.js +13 -0
- package/src/object.d.ts +15 -0
- package/src/object.js +13 -0
- package/src/port.d.ts +30 -0
- package/src/port.js +56 -0
- package/src/progress.d.ts +6 -0
- package/src/progress.js +1 -0
- package/src/rect.d.ts +5 -0
- package/src/rect.js +16 -0
- package/src/resource.d.ts +12 -0
- package/src/resource.js +28 -0
- package/src/schedule.d.ts +10 -0
- package/src/schedule.js +33 -0
- package/src/set.d.ts +2 -0
- package/src/set.js +2 -0
- package/src/signal.d.ts +37 -0
- package/src/signal.js +89 -0
- package/src/stream.d.ts +6 -0
- package/src/stream.js +26 -0
- package/src/string.d.ts +8 -0
- package/src/string.js +29 -0
- package/src/struct.d.ts +42 -0
- package/src/struct.js +58 -0
- package/src/tree.d.ts +11 -0
- package/src/tree.js +25 -0
- package/src/treemap.d.ts +13 -0
- package/src/treemap.js +71 -0
- package/src/typedarray.d.ts +31 -0
- package/src/typedarray.js +39 -0
- package/src/types.d.ts +63 -0
- package/src/types.js +1 -0
- package/src/utils.d.ts +4 -0
- package/src/utils.js +30 -0
- package/src/weak.d.ts +2 -0
- package/src/weak.js +5 -0
- package/stream.d.ts +1 -1
- package/stream.js +2 -2
- package/string.d.ts +2 -1
- package/string.js +3 -2
- package/struct.d.ts +2 -2
- package/struct.js +2 -2
- package/tree.d.ts +3 -3
- package/tree.js +8 -10
- package/treemap.d.ts +12 -12
- package/treemap.js +62 -59
- package/typedarray.d.ts +1 -1
- package/typedarray.js +3 -3
- package/types.d.ts +3 -7
- package/utils.d.ts +1 -1
- package/utils.js +1 -1
- package/weak.d.ts +2 -2
- package/weak.js +1 -1
package/string.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { EMPTY } from "./constants";
|
|
2
|
-
import { seek } from "./array";
|
|
1
|
+
import { EMPTY } from "./constants.js";
|
|
2
|
+
import { seek } from "./array.js";
|
|
3
3
|
export const basename = (path) => path.split("/").at(-1) ?? "";
|
|
4
4
|
export const dirname = (path) => {
|
|
5
5
|
const parts = path.split("/");
|
|
@@ -26,3 +26,4 @@ export const readLine = (delims = new Set([0x0A])) => {
|
|
|
26
26
|
return n > 0 ? dec(data.subarray(offset, n + 1)) : EMPTY.STR;
|
|
27
27
|
};
|
|
28
28
|
};
|
|
29
|
+
export const DATA_URL = /^data:(?<mime>[\w/\-\+]+)?(;(?<params>[\w\-]+\=[^;,\s]+)*)?(;(?<encoding>base64))?,(?<data>.*)$/;
|
package/struct.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { TypedArray, Fn, NumberArray } from "./types";
|
|
2
|
-
import type { Constructor } from "./typedarray";
|
|
1
|
+
import type { TypedArray, Fn, NumberArray } from "./types.js";
|
|
2
|
+
import type { Constructor } from "./typedarray.js";
|
|
3
3
|
export interface Type {
|
|
4
4
|
size: number;
|
|
5
5
|
align: number;
|
package/struct.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { align as _align } from "./math";
|
|
2
|
-
import { isBigIntArray, isNumberArray } from "./checks";
|
|
1
|
+
import { align as _align } from "./math.js";
|
|
2
|
+
import { isBigIntArray, isNumberArray } from "./checks.js";
|
|
3
3
|
const vec = (type) => (length = 1, align = type.BYTES_PER_ELEMENT) => ({
|
|
4
4
|
type, align, length, size: type.BYTES_PER_ELEMENT * length,
|
|
5
5
|
});
|
package/tree.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { Fn, FnV, TPred, PredV } from './types';
|
|
1
|
+
import type { Fn, FnV, TPred, PredV } from './types.js';
|
|
2
2
|
export type Node<S, T = S> = T | Tree<S, T>;
|
|
3
3
|
export type Tree<S, T = S> = [S, Node<S, T>[]];
|
|
4
4
|
export declare const tree: <S, T = S>(value: S, children?: Node<S, T>[]) => Tree<S, T>;
|
|
5
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>(p?: TPred<Tree<S, T>, Node<S, T>>) =>
|
|
7
|
-
export declare const value: <S, T = S>(p?: TPred<Tree<S, T>, Node<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
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
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
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>>>;
|
package/tree.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { identity } from './fn';
|
|
2
|
-
import { EMPTY } from './constants';
|
|
3
|
-
import { zip as zipI } from './iterable';
|
|
4
|
-
export const tree = (value, children =
|
|
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
5
|
export const isTree = (x) => Array.isArray(x) && x.length == 2 && Array.isArray(x[1]);
|
|
6
|
-
export const children = (p = isTree) =>
|
|
7
|
-
export const value = (p = isTree) =>
|
|
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
8
|
export const transform = (node, leaf, p = isTree) => {
|
|
9
9
|
const f = (c) => (x) => p(x) ? node(x, f, c) : leaf(x, c);
|
|
10
10
|
return f;
|
|
@@ -13,13 +13,11 @@ export const map = (parent, child, update = identity, p = isTree) => transform((
|
|
|
13
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
14
|
export const zip = (na, nb) => {
|
|
15
15
|
const res = [];
|
|
16
|
-
const va = value(), vb = value();
|
|
17
|
-
const ca = children(), cb = children();
|
|
18
16
|
for (const [a, b] of zipI(Iterator.from(na), Iterator.from(nb))) {
|
|
19
17
|
if (!isTree(a) && !isTree(a))
|
|
20
|
-
res.push([
|
|
18
|
+
res.push([value(a), value(b)]);
|
|
21
19
|
else if ((isTree(a) && isTree(a)))
|
|
22
|
-
res.push([[
|
|
20
|
+
res.push([[value(a), value(b)], zip(children(a), children(b))]);
|
|
23
21
|
else
|
|
24
22
|
throw new Error("[tree] a and b should be both tree or node");
|
|
25
23
|
}
|
package/treemap.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
[Symbol.iterator](): Generator<NonNullable<T>, void, unknown>;
|
|
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>;
|
|
13
12
|
}
|
|
13
|
+
export declare const treemap: <T = number>() => Treemap<T>;
|
package/treemap.js
CHANGED
|
@@ -1,68 +1,71 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
changed = false;
|
|
13
|
-
add(node, parent) {
|
|
14
|
-
const { nodes, parents } = this;
|
|
15
|
-
nodes.add(node);
|
|
16
|
-
if (!isUndefined(parent))
|
|
17
|
-
parents.set(node, parent);
|
|
18
|
-
this.changed = true;
|
|
19
|
-
return node;
|
|
20
|
-
}
|
|
21
|
-
remove(key) {
|
|
22
|
-
const { nodes, parents } = this;
|
|
23
|
-
nodes.delete(key);
|
|
24
|
-
parents.delete(key);
|
|
25
|
-
this.changed = true;
|
|
26
|
-
}
|
|
27
|
-
#update() {
|
|
28
|
-
const changed = this.changed;
|
|
29
|
-
if (changed) {
|
|
30
|
-
const { nodes, parents } = this;
|
|
31
|
-
const children = this.#children;
|
|
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) {
|
|
32
12
|
children.clear();
|
|
33
13
|
for (const k of nodes) {
|
|
34
14
|
const p = parents.get(k);
|
|
35
|
-
if (
|
|
15
|
+
if (isDefined(p)) {
|
|
36
16
|
if (!children.has(p))
|
|
37
17
|
children.set(p, []);
|
|
38
18
|
children.get(p).push(k);
|
|
39
19
|
}
|
|
40
20
|
}
|
|
41
|
-
|
|
21
|
+
changed = false;
|
|
42
22
|
}
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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/typedarray.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FnV, TypedArray, NumberArray, BufferType, Number } from "./types";
|
|
1
|
+
import type { FnV, TypedArray, NumberArray, BufferType, Number } from "./types.js";
|
|
2
2
|
export interface Constructor<T extends TypedArray> {
|
|
3
3
|
new (length: number): T;
|
|
4
4
|
new (buffer: BufferType<T> | ArrayBufferLike, byteOffset?: number, length?: number): T;
|
package/typedarray.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { bind } from "./fn";
|
|
2
|
-
import { zmap, map as mapI, reduce } from "./iterable";
|
|
3
|
-
import { push } from "./array";
|
|
1
|
+
import { bind } from "./fn.js";
|
|
2
|
+
import { zmap, map as mapI, reduce } from "./iterable.js";
|
|
3
|
+
import { push } from "./array.js";
|
|
4
4
|
export const view = (type, length) => (buf = new ArrayBuffer(type.BYTES_PER_ELEMENT * length), offset = 0) => new type(buf, offset, length);
|
|
5
5
|
export const u8 = bind((view), Uint8Array);
|
|
6
6
|
export const u16 = bind((view), Uint16Array);
|
package/types.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ export interface RecordOf<T> {
|
|
|
2
2
|
[id: PropertyKey]: T;
|
|
3
3
|
}
|
|
4
4
|
export type ValueOf<T> = T[keyof T];
|
|
5
|
+
export type Structure<T> = T | readonly Structure<T>[] | RecordOf<Structure<T>>;
|
|
6
|
+
export type Optional<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> & Partial<Pick<T, K>>;
|
|
5
7
|
export type Tuple<T, N extends number, R extends T[] = []> = R['length'] extends N ? R : Tuple<T, N, [T, ...R]>;
|
|
6
8
|
export type Elements<A extends readonly unknown[]> = A extends readonly (infer T)[] ? T : never;
|
|
7
9
|
export type MaybeFirst<T extends unknown[], S = void> = T extends {
|
|
@@ -16,7 +18,6 @@ export type Fns<T> = {
|
|
|
16
18
|
export type FnF = <F extends FnV>(f: F) => (...xs: Parameters<F>) => ReturnType<F>;
|
|
17
19
|
export type Dispose = Fn<void>;
|
|
18
20
|
export type Reactive<T = unknown, R = any> = Fn<Fn<T, R>, Dispose>;
|
|
19
|
-
export type Resource<T> = Reactive<T, Dispose | void>;
|
|
20
21
|
export type Constantly<T> = () => T;
|
|
21
22
|
export type FirstParam<F extends FnV> = MaybeFirst<Parameters<F>>;
|
|
22
23
|
export type MaybePromise<T = unknown> = T | PromiseLike<T>;
|
|
@@ -50,7 +51,7 @@ export type Eq<A, B = A> = FnV<[A, B], boolean>;
|
|
|
50
51
|
export type Pred<T> = Fn<T, boolean>;
|
|
51
52
|
export type PredV<T extends any[]> = FnV<T, boolean>;
|
|
52
53
|
export type TPred<T extends S, S = any> = (x: S) => x is T;
|
|
53
|
-
export type
|
|
54
|
+
export type Maybe<T> = T | undefined;
|
|
54
55
|
export type Indexed<T> = [number, T];
|
|
55
56
|
export type Required<T, K extends keyof T> = T & {
|
|
56
57
|
[P in K]-?: T[P];
|
|
@@ -59,9 +60,4 @@ export type Partials<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
|
59
60
|
export type KeyMap<T extends string> = {
|
|
60
61
|
[K in T]: Extract<K, T>;
|
|
61
62
|
};
|
|
62
|
-
export interface BufferView<T extends ArrayBufferLike = ArrayBufferLike> {
|
|
63
|
-
readonly buffer: T;
|
|
64
|
-
readonly byteLength: number;
|
|
65
|
-
readonly byteOffset: number;
|
|
66
|
-
}
|
|
67
63
|
export {};
|
package/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Tuple } from "./types";
|
|
1
|
+
import type { Tuple } from "./types.js";
|
|
2
2
|
export declare function toposort(nodes: number[]): Generator<number | undefined, void, unknown>;
|
|
3
3
|
export declare const hex2rgb: (hex: string) => number[];
|
|
4
4
|
export declare const rgb2hex: (rgb: Tuple<number, 3>) => string;
|
package/utils.js
CHANGED
package/weak.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Fn,
|
|
2
|
-
export declare const deref: <T extends WeakKey, F extends Fn<T>>(r: WeakRef<T>, f: F) =>
|
|
1
|
+
import type { Fn, Maybe } from "./types.js";
|
|
2
|
+
export declare const deref: <T extends WeakKey, F extends Fn<T>>(r: WeakRef<T>, f: F) => Maybe<ReturnType<F>>;
|
package/weak.js
CHANGED