@cgtk/std 0.0.182

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/array.d.ts +12 -0
  2. package/array.js +19 -0
  3. package/assign.d.ts +8 -0
  4. package/assign.js +26 -0
  5. package/async.d.ts +5 -0
  6. package/async.js +27 -0
  7. package/basen.d.ts +12 -0
  8. package/basen.js +31 -0
  9. package/buffer.d.ts +14 -0
  10. package/buffer.js +54 -0
  11. package/checks.d.ts +26 -0
  12. package/checks.js +37 -0
  13. package/constants.d.ts +5 -0
  14. package/constants.js +1 -0
  15. package/dom.d.ts +39 -0
  16. package/dom.js +31 -0
  17. package/fn.d.ts +29 -0
  18. package/fn.js +56 -0
  19. package/http.d.ts +9 -0
  20. package/http.js +32 -0
  21. package/index.d.ts +1 -0
  22. package/index.js +1 -0
  23. package/iterable.d.ts +41 -0
  24. package/iterable.js +142 -0
  25. package/json.d.ts +1 -0
  26. package/json.js +1 -0
  27. package/map.d.ts +13 -0
  28. package/map.js +36 -0
  29. package/math.d.ts +31 -0
  30. package/math.js +39 -0
  31. package/number.d.ts +2 -0
  32. package/number.js +5 -0
  33. package/object.d.ts +13 -0
  34. package/object.js +11 -0
  35. package/package.json +44 -0
  36. package/progress.d.ts +6 -0
  37. package/progress.js +1 -0
  38. package/schedule.d.ts +8 -0
  39. package/schedule.js +44 -0
  40. package/signal.d.ts +19 -0
  41. package/signal.js +25 -0
  42. package/stream.d.ts +6 -0
  43. package/stream.js +26 -0
  44. package/string.d.ts +5 -0
  45. package/string.js +22 -0
  46. package/struct.d.ts +25 -0
  47. package/struct.js +58 -0
  48. package/tree.d.ts +20 -0
  49. package/tree.js +33 -0
  50. package/treemap.d.ts +13 -0
  51. package/treemap.js +68 -0
  52. package/typedarray.d.ts +23 -0
  53. package/typedarray.js +44 -0
  54. package/types.d.ts +42 -0
  55. package/types.js +1 -0
  56. package/utils.d.ts +4 -0
  57. package/utils.js +30 -0
  58. package/weak.d.ts +2 -0
  59. package/weak.js +5 -0
package/tree.js ADDED
@@ -0,0 +1,33 @@
1
+ import { identity } from './fn.js';
2
+ import { EMPTY } from './constants.js';
3
+ import { isString } from './checks.js';
4
+ import { zip as zipI } from './iterable.js';
5
+ export const tree = (value, children = EMPTY.LIST) => [value, children];
6
+ export const isTree = (x) => Array.isArray(x) && x.length == 2 && Array.isArray(x[1]);
7
+ export const children = (p = isTree) => (x) => p(x) ? x[1] : EMPTY.LIST;
8
+ export const value = (p = isTree) => (x) => p(x) ? x[0] : x;
9
+ export const transform = (node, leaf, p = isTree) => {
10
+ const f = (c) => (x) => p(x) ? node(x, f, c) : leaf(x, c);
11
+ return f;
12
+ };
13
+ 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);
14
+ 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);
15
+ export const toggleChildren = (g, update = identity, p = isTree) => transform((x, f, c) => tree(x[0], g(x, c) ? x[1].map(f(update(c, x[0]))) : EMPTY.LIST), identity, p);
16
+ export const asObject = (items, f = identity, update = identity, context = "") => Object.fromEntries(items.map((x) => isString(x) ? [x, f(x, context)] : [x[0], {
17
+ value: f(x[0], context),
18
+ children: asObject(x[1], f, update, update(context, x[0]))
19
+ }]));
20
+ export const zip = (na, nb) => {
21
+ const res = [];
22
+ const va = value(), vb = value();
23
+ const ca = children(), cb = children();
24
+ for (const [a, b] of zipI(Iterator.from(na), Iterator.from(nb))) {
25
+ if (!isTree(a) && !isTree(a))
26
+ res.push([va(a), vb(b)]);
27
+ else if ((isTree(a) && isTree(a)))
28
+ res.push([[va(a), vb(b)], zip(ca(a), cb(b))]);
29
+ else
30
+ throw new Error("[tree] a and b should be both tree or node");
31
+ }
32
+ return res;
33
+ };
package/treemap.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export declare class Treemap<T = number> {
2
+ #private;
3
+ static from<T>(iter: Iterable<[T, T?]>): Treemap<T>;
4
+ nodes: Set<T>;
5
+ parents: Map<T, T>;
6
+ changed: boolean;
7
+ add(node: T, parent?: T): T;
8
+ remove(key: T): void;
9
+ parent(key: T): T | undefined;
10
+ get size(): number;
11
+ children(key: T): T[] | undefined;
12
+ [Symbol.iterator](): Generator<NonNullable<T>, void, unknown>;
13
+ }
package/treemap.js ADDED
@@ -0,0 +1,68 @@
1
+ import { isUndefined } from "./checks.js";
2
+ export class Treemap {
3
+ static from(iter) {
4
+ const tree = new Treemap();
5
+ for (const [n, p] of iter)
6
+ tree.add(n, p);
7
+ return tree;
8
+ }
9
+ nodes = new Set();
10
+ parents = new Map();
11
+ #children = new Map();
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;
32
+ children.clear();
33
+ for (const k of nodes) {
34
+ const p = parents.get(k);
35
+ if (!isUndefined(p)) {
36
+ if (!children.has(p))
37
+ children.set(p, []);
38
+ children.get(p).push(k);
39
+ }
40
+ }
41
+ this.changed = false;
42
+ }
43
+ return changed;
44
+ }
45
+ parent(key) {
46
+ this.#update();
47
+ return this.parents.get(key);
48
+ }
49
+ get size() {
50
+ this.#update();
51
+ return this.nodes.size;
52
+ }
53
+ children(key) {
54
+ this.#update();
55
+ return this.#children.get(key);
56
+ }
57
+ *[Symbol.iterator]() {
58
+ this.#update();
59
+ const { nodes, parents } = this;
60
+ const queue = [], children = this.#children;
61
+ nodes.forEach(k => !parents.has(k) && queue.push(k));
62
+ for (let node; queue.length > 0;) {
63
+ yield node = queue.shift();
64
+ if (children.has(node))
65
+ queue.push(...children.get(node));
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,23 @@
1
+ import type { Fn, FnV, TypedArray, Numbers } from "./types";
2
+ export type Constructor<T extends TypedArray> = T extends Uint8Array ? Uint8ArrayConstructor : T extends Uint8ClampedArray ? Uint8ClampedArrayConstructor : T extends Uint16Array ? Uint16ArrayConstructor : T extends Uint32Array ? Uint32ArrayConstructor : T extends Int8Array ? Int8ArrayConstructor : T extends Int16Array ? Int16ArrayConstructor : T extends Int32Array ? Int32ArrayConstructor : T extends Float32Array ? Float32ArrayConstructor : T extends Float64Array ? Float64ArrayConstructor : never;
3
+ export type View<T extends TypedArray> = FnV<[ArrayBuffer?, number?], T>;
4
+ export declare const view: <T extends TypedArray>(type: Constructor<T>, length: number) => View<T>;
5
+ export declare const u8: (length: number) => View<Uint8Array<ArrayBufferLike>>;
6
+ export declare const u16: (length: number) => View<Uint16Array<ArrayBufferLike>>;
7
+ export declare const u32: (length: number) => View<Uint32Array<ArrayBufferLike>>;
8
+ export declare const i8: (length: number) => View<Int8Array<ArrayBufferLike>>;
9
+ export declare const i16: (length: number) => View<Int16Array<ArrayBufferLike>>;
10
+ export declare const i32: (length: number) => View<Int32Array<ArrayBufferLike>>;
11
+ export declare const f32: (length: number) => View<Float32Array<ArrayBufferLike>>;
12
+ export declare const f64: (length: number) => View<Float64Array<ArrayBufferLike>>;
13
+ export declare const UintArray: (n: number) => Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
14
+ export declare const ctor: <T extends TypedArray>(x: T) => Constructor<T>;
15
+ export declare const emptyLike: <T extends TypedArray>(x: T) => T;
16
+ export declare const set: <T extends TypedArray>(xs: T, v: Numbers, i: number) => T;
17
+ export declare const concat: <T extends TypedArray>(chunks: T[]) => T;
18
+ export declare const iter: (size: number, stride?: number) => <T extends TypedArray>(data: T) => Generator<T>;
19
+ export declare const tile: (n: number) => (out: TypedArray, x: TypedArray) => TypedArray;
20
+ export declare const tileEvery: (m: number, n: number) => <T extends TypedArray>(xs: T, out?: T) => T;
21
+ export declare const map: <T extends TypedArray>(it: Fn<T, IteratorObject<T>>, f: FnV<[T, T]>) => (data: T, out?: T) => T;
22
+ export declare const unweld: <T extends TypedArray>(data: T, indices: Numbers, size: number, out?: T) => T;
23
+ export declare const toFixed: (n: number, sep?: string) => (xs: TypedArray) => string;
package/typedarray.js ADDED
@@ -0,0 +1,44 @@
1
+ import { bind } from "./fn";
2
+ import { zmap, map as mapI, reduce } from "./iterable";
3
+ import { push } from "./array";
4
+ export const view = (type, length) => (buf = new ArrayBuffer(type.BYTES_PER_ELEMENT * length), offset = 0) => new type(buf, offset, length);
5
+ export const u8 = bind((view), Uint8Array);
6
+ export const u16 = bind((view), Uint16Array);
7
+ export const u32 = bind((view), Uint32Array);
8
+ export const i8 = bind((view), Int8Array);
9
+ export const i16 = bind((view), Int16Array);
10
+ export const i32 = bind((view), Int32Array);
11
+ export const f32 = bind((view), Float32Array);
12
+ export const f64 = bind((view), Float64Array);
13
+ export const UintArray = (n) => n < 256 ? Uint8Array : n < 65536 ? Uint16Array : Uint32Array;
14
+ export const ctor = (x) => x.constructor;
15
+ export const emptyLike = (x) => new (ctor(x))(x.length);
16
+ export const set = (xs, v, i) => (xs.set(v, i), xs);
17
+ export const concat = (chunks) => {
18
+ const out = new (ctor(chunks[0]))(chunks.reduce((a, x) => a + x.length, 0));
19
+ chunks.reduce((offset, chunk) => (out.set(chunk, offset), offset + chunk.length), 0);
20
+ return out;
21
+ };
22
+ export const iter = (size, stride = size) => function* (data) {
23
+ const C = ctor(data);
24
+ for (let i = 0, n = data.length; i < n - size + 1; i += stride) {
25
+ const offset = data.byteOffset + i * C.BYTES_PER_ELEMENT;
26
+ // https://github.com/microsoft/TypeScript/pull/59417
27
+ yield new C(data.buffer, offset, size);
28
+ }
29
+ };
30
+ export const tile = (n) => (out, x) => {
31
+ for (let i = 0; i < n; i++)
32
+ out.set(x, i * x.length);
33
+ return out;
34
+ };
35
+ export const tileEvery = (m, n) => (xs, out = new (ctor(xs))(xs.length * n)) => (zmap(tile(n))(iter(m * n)(out), iter(m)(xs)), out);
36
+ export const map = (it, f) => (data, out = emptyLike(data)) => (zmap(f)(it(out), it(data)), out);
37
+ export const unweld = (data, indices, size, out = new (ctor(data))(indices.length * size)) => {
38
+ indices.forEach((i, j) => {
39
+ for (let k = 0; k < size; k++)
40
+ out[j * size + k] = data[i * size + k];
41
+ });
42
+ return out;
43
+ };
44
+ export const toFixed = (n, sep = ',') => (xs) => reduce((push), [])(mapI(x => x.toFixed(n))(Iterator.from(xs))).join(sep);
package/types.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ export type Key = string | number | symbol;
2
+ export interface RecordOf<T> {
3
+ [id: string | number | symbol]: T;
4
+ }
5
+ export type ValueOf<T> = T[keyof T];
6
+ export type Tuple<T, N extends number, R extends T[] = []> = R['length'] extends N ? R : Tuple<T, N, [T, ...R]>;
7
+ export type Elements<A extends readonly unknown[]> = A extends readonly (infer T)[] ? T : never;
8
+ export type MaybeFirst<T extends unknown[], S = void> = T extends {
9
+ length: 0;
10
+ } ? S : T[0];
11
+ export type Fn<T = any, R = any> = (x: T) => R;
12
+ export type FnP<T = any, R = any> = (x: T) => Promise<R>;
13
+ export type FnV<T extends any[] = any[], R = any> = (...x: T) => R;
14
+ export type FnF = <F extends FnV>(f: F) => (...xs: Parameters<F>) => ReturnType<F>;
15
+ export type Reactive<T = unknown> = Fn<Fn<T>, Fn<void>>;
16
+ export type Constantly<T> = () => T;
17
+ export type FirstParam<F extends FnV> = MaybeFirst<Parameters<F>>;
18
+ export type Nullable<T> = T | null;
19
+ export type Last<T extends any[]> = T extends [...any, infer Rest] ? Rest : never;
20
+ export type Tail<T extends any[]> = T extends [any, ...(infer Rest)] ? Rest : never;
21
+ export type Pop<T extends any[]> = T extends [...infer head, any] ? head : never;
22
+ export type MinusOne<T extends number, A extends any[] = []> = A['length'] extends T ? Pop<A>['length'] : MinusOne<T, [...A, 0]>;
23
+ export type Take<N extends number, T extends any[], R extends any[] = []> = R['length'] extends N ? R : Take<N, Tail<T>, [...R, T[0]]>;
24
+ export type Skip<N extends number, T extends any[]> = N extends 0 ? T : Skip<MinusOne<N>, Tail<T>>;
25
+ export type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array;
26
+ export type TypedArrayConstructor = Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor | Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor;
27
+ export type UintArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;
28
+ export type IntArray = Int8Array | Int16Array | Int32Array;
29
+ export type Numbers = Array<number> | TypedArray;
30
+ export type Eq<A, B = A> = FnV<[A, B], boolean>;
31
+ export type Pred<T> = Fn<T, boolean>;
32
+ export type PredV<T extends any[]> = FnV<T, boolean>;
33
+ export type TPred<T extends S, S = any> = (x: S) => x is T;
34
+ export type Optional<T> = T | undefined;
35
+ export type Indexed<T> = [number, T];
36
+ export type Required<T, K extends keyof T> = T & {
37
+ [P in K]-?: T[P];
38
+ };
39
+ export type Partials<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
40
+ export type KeyMap<T extends string> = {
41
+ [K in T]: Extract<K, T>;
42
+ };
package/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/utils.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Tuple } from "./types.js";
2
+ export declare function toposort(nodes: number[]): Generator<number | undefined, void, unknown>;
3
+ export declare const hex2rgb: (hex: string) => number[];
4
+ export declare const rgb2hex: (rgb: Tuple<number, 3>) => string;
package/utils.js ADDED
@@ -0,0 +1,30 @@
1
+ import { assert } from "./checks.js";
2
+ export function* toposort(nodes) {
3
+ const n = nodes.length;
4
+ const queue = [], children = new Map();
5
+ nodes.forEach((p, i) => {
6
+ if (p < 0)
7
+ queue.push(i);
8
+ else {
9
+ assert(p < n, `[toposort] Invalid parent: ${p}`);
10
+ if (!children.has(p))
11
+ children.set(p, []);
12
+ children.get(p).push(i);
13
+ }
14
+ });
15
+ while (queue.length > 0) {
16
+ const p = queue.shift();
17
+ yield p;
18
+ if (children.has(p)) {
19
+ queue.push(...children.get(p));
20
+ children.delete(p);
21
+ }
22
+ }
23
+ }
24
+ export const hex2rgb = (hex) => {
25
+ const r = parseInt(hex.slice(1, 3), 16);
26
+ const g = parseInt(hex.slice(3, 5), 16);
27
+ const b = parseInt(hex.slice(5, 7), 16);
28
+ return [r, g, b];
29
+ };
30
+ export const rgb2hex = (rgb) => "#" + rgb.map(x => Math.round(x).toString(16).padStart(2, '0')).join("");
package/weak.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { Fn, Optional } from "./types.js";
2
+ export declare const deref: <T extends WeakKey, F extends Fn<T>>(r: WeakRef<T>, f: F) => Optional<ReturnType<F>>;
package/weak.js ADDED
@@ -0,0 +1,5 @@
1
+ import { isDefined } from "./checks.js";
2
+ export const deref = (r, f) => {
3
+ const x = r.deref();
4
+ return isDefined(x) ? f(x) : undefined;
5
+ };