@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.
Files changed (128) hide show
  1. package/array.d.ts +1 -1
  2. package/array.js +1 -1
  3. package/assign.d.ts +1 -1
  4. package/assign.js +1 -1
  5. package/async.d.ts +3 -3
  6. package/async.js +5 -4
  7. package/basen.js +1 -1
  8. package/buffer.d.ts +1 -1
  9. package/buffer.js +1 -1
  10. package/checks.d.ts +2 -2
  11. package/checks.js +2 -2
  12. package/dom.d.ts +2 -2
  13. package/dom.js +3 -3
  14. package/fn.d.ts +3 -3
  15. package/fn.js +3 -3
  16. package/http.d.ts +6 -7
  17. package/http.js +11 -15
  18. package/iterable.d.ts +1 -1
  19. package/iterable.js +3 -3
  20. package/json.d.ts +1 -1
  21. package/json.js +1 -1
  22. package/map.d.ts +3 -3
  23. package/map.js +2 -2
  24. package/math.js +1 -1
  25. package/npy.d.ts +1 -1
  26. package/number.js +1 -1
  27. package/object.d.ts +2 -2
  28. package/package.json +2 -2
  29. package/port.d.ts +2 -1
  30. package/port.js +7 -4
  31. package/progress.d.ts +1 -1
  32. package/rect.d.ts +1 -1
  33. package/rect.js +1 -1
  34. package/resource.d.ts +10 -8
  35. package/resource.js +22 -15
  36. package/schedule.d.ts +1 -1
  37. package/schedule.js +2 -2
  38. package/set.d.ts +1 -0
  39. package/set.js +1 -0
  40. package/signal.d.ts +18 -5
  41. package/signal.js +56 -15
  42. package/src/array.d.ts +14 -0
  43. package/src/array.js +21 -0
  44. package/src/assign.d.ts +8 -0
  45. package/src/assign.js +26 -0
  46. package/src/async.d.ts +8 -0
  47. package/src/async.js +31 -0
  48. package/src/base64.d.ts +1 -0
  49. package/src/base64.js +7 -0
  50. package/src/basen.d.ts +12 -0
  51. package/src/basen.js +31 -0
  52. package/src/buffer.d.ts +14 -0
  53. package/src/buffer.js +54 -0
  54. package/src/checks.d.ts +29 -0
  55. package/src/checks.js +44 -0
  56. package/src/constants.d.ts +5 -0
  57. package/src/constants.js +1 -0
  58. package/src/dom.d.ts +50 -0
  59. package/src/dom.js +36 -0
  60. package/src/fn.d.ts +33 -0
  61. package/src/fn.js +61 -0
  62. package/src/http.d.ts +9 -0
  63. package/src/http.js +33 -0
  64. package/src/index.d.ts +1 -0
  65. package/src/index.js +1 -0
  66. package/src/iterable.d.ts +43 -0
  67. package/src/iterable.js +158 -0
  68. package/src/json.d.ts +1 -0
  69. package/src/json.js +1 -0
  70. package/src/map.d.ts +12 -0
  71. package/src/map.js +35 -0
  72. package/src/math.d.ts +34 -0
  73. package/src/math.js +43 -0
  74. package/src/npy.d.ts +10 -0
  75. package/src/npy.js +29 -0
  76. package/src/number.d.ts +4 -0
  77. package/src/number.js +13 -0
  78. package/src/object.d.ts +15 -0
  79. package/src/object.js +13 -0
  80. package/src/port.d.ts +30 -0
  81. package/src/port.js +56 -0
  82. package/src/progress.d.ts +6 -0
  83. package/src/progress.js +1 -0
  84. package/src/rect.d.ts +5 -0
  85. package/src/rect.js +16 -0
  86. package/src/resource.d.ts +12 -0
  87. package/src/resource.js +28 -0
  88. package/src/schedule.d.ts +10 -0
  89. package/src/schedule.js +33 -0
  90. package/src/set.d.ts +2 -0
  91. package/src/set.js +2 -0
  92. package/src/signal.d.ts +37 -0
  93. package/src/signal.js +89 -0
  94. package/src/stream.d.ts +6 -0
  95. package/src/stream.js +26 -0
  96. package/src/string.d.ts +8 -0
  97. package/src/string.js +29 -0
  98. package/src/struct.d.ts +42 -0
  99. package/src/struct.js +58 -0
  100. package/src/tree.d.ts +11 -0
  101. package/src/tree.js +25 -0
  102. package/src/treemap.d.ts +13 -0
  103. package/src/treemap.js +71 -0
  104. package/src/typedarray.d.ts +31 -0
  105. package/src/typedarray.js +39 -0
  106. package/src/types.d.ts +63 -0
  107. package/src/types.js +1 -0
  108. package/src/utils.d.ts +4 -0
  109. package/src/utils.js +30 -0
  110. package/src/weak.d.ts +2 -0
  111. package/src/weak.js +5 -0
  112. package/stream.d.ts +1 -1
  113. package/stream.js +2 -2
  114. package/string.d.ts +2 -1
  115. package/string.js +3 -2
  116. package/struct.d.ts +2 -2
  117. package/struct.js +2 -2
  118. package/tree.d.ts +3 -3
  119. package/tree.js +8 -10
  120. package/treemap.d.ts +12 -12
  121. package/treemap.js +62 -59
  122. package/typedarray.d.ts +1 -1
  123. package/typedarray.js +3 -3
  124. package/types.d.ts +3 -7
  125. package/utils.d.ts +1 -1
  126. package/utils.js +1 -1
  127. package/weak.d.ts +2 -2
  128. 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>>) => (x: Node<S, T>) => Node<S, T>[];
7
- export declare const value: <S, T = S>(p?: TPred<Tree<S, T>, Node<S, T>>) => (x: Node<S, T>) => 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 = EMPTY.LIST) => [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) => (x) => p(x) ? x[1] : EMPTY.LIST;
7
- export const value = (p = isTree) => (x) => p(x) ? x[0] : x;
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([va(a), vb(b)]);
18
+ res.push([value(a), value(b)]);
21
19
  else if ((isTree(a) && isTree(a)))
22
- res.push([[va(a), vb(b)], zip(ca(a), cb(b))]);
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
- 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>;
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 { isUndefined } from "./checks";
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;
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 (!isUndefined(p)) {
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
- this.changed = false;
21
+ changed = false;
42
22
  }
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
- }
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 Optional<T> = T | undefined;
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
@@ -1,4 +1,4 @@
1
- import { assert } from "./checks";
1
+ import { assert } from "./checks.js";
2
2
  export function* toposort(nodes) {
3
3
  const n = nodes.length;
4
4
  const queue = [], children = new Map();
package/weak.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import type { Fn, Optional } from "./types";
2
- export declare const deref: <T extends WeakKey, F extends Fn<T>>(r: WeakRef<T>, f: F) => Optional<ReturnType<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
@@ -1,4 +1,4 @@
1
- import { isDefined } from "./checks";
1
+ import { isDefined } from "./checks.js";
2
2
  export const deref = (r, f) => {
3
3
  const x = r.deref();
4
4
  return isDefined(x) ? f(x) : undefined;