@cgtk/std 0.0.193 → 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 (121) 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 +1 -1
  6. package/async.js +2 -2
  7. package/basen.js +1 -1
  8. package/buffer.d.ts +1 -1
  9. package/buffer.js +1 -1
  10. package/checks.d.ts +1 -1
  11. package/checks.js +2 -2
  12. package/dom.d.ts +1 -1
  13. package/dom.js +3 -3
  14. package/fn.d.ts +1 -1
  15. package/fn.js +1 -1
  16. package/http.js +2 -2
  17. package/iterable.d.ts +1 -1
  18. package/iterable.js +3 -3
  19. package/map.d.ts +1 -1
  20. package/map.js +2 -2
  21. package/math.js +1 -1
  22. package/npy.d.ts +1 -1
  23. package/number.js +1 -1
  24. package/package.json +2 -2
  25. package/port.d.ts +1 -1
  26. package/port.js +4 -4
  27. package/progress.d.ts +1 -1
  28. package/rect.d.ts +1 -1
  29. package/rect.js +1 -1
  30. package/resource.d.ts +1 -1
  31. package/resource.js +2 -2
  32. package/schedule.d.ts +1 -1
  33. package/schedule.js +2 -2
  34. package/signal.d.ts +1 -1
  35. package/signal.js +4 -4
  36. package/src/array.d.ts +14 -0
  37. package/src/array.js +21 -0
  38. package/src/assign.d.ts +8 -0
  39. package/src/assign.js +26 -0
  40. package/src/async.d.ts +8 -0
  41. package/src/async.js +31 -0
  42. package/src/base64.d.ts +1 -0
  43. package/src/base64.js +7 -0
  44. package/src/basen.d.ts +12 -0
  45. package/src/basen.js +31 -0
  46. package/src/buffer.d.ts +14 -0
  47. package/src/buffer.js +54 -0
  48. package/src/checks.d.ts +29 -0
  49. package/src/checks.js +44 -0
  50. package/src/constants.d.ts +5 -0
  51. package/src/constants.js +1 -0
  52. package/src/dom.d.ts +50 -0
  53. package/src/dom.js +36 -0
  54. package/src/fn.d.ts +33 -0
  55. package/src/fn.js +61 -0
  56. package/src/http.d.ts +9 -0
  57. package/src/http.js +33 -0
  58. package/src/index.d.ts +1 -0
  59. package/src/index.js +1 -0
  60. package/src/iterable.d.ts +43 -0
  61. package/src/iterable.js +158 -0
  62. package/src/json.d.ts +1 -0
  63. package/src/json.js +1 -0
  64. package/src/map.d.ts +12 -0
  65. package/src/map.js +35 -0
  66. package/src/math.d.ts +34 -0
  67. package/src/math.js +43 -0
  68. package/src/npy.d.ts +10 -0
  69. package/src/npy.js +29 -0
  70. package/src/number.d.ts +4 -0
  71. package/src/number.js +13 -0
  72. package/src/object.d.ts +15 -0
  73. package/src/object.js +13 -0
  74. package/src/port.d.ts +30 -0
  75. package/src/port.js +56 -0
  76. package/src/progress.d.ts +6 -0
  77. package/src/progress.js +1 -0
  78. package/src/rect.d.ts +5 -0
  79. package/src/rect.js +16 -0
  80. package/src/resource.d.ts +12 -0
  81. package/src/resource.js +28 -0
  82. package/src/schedule.d.ts +10 -0
  83. package/src/schedule.js +33 -0
  84. package/src/set.d.ts +2 -0
  85. package/src/set.js +2 -0
  86. package/src/signal.d.ts +37 -0
  87. package/src/signal.js +89 -0
  88. package/src/stream.d.ts +6 -0
  89. package/src/stream.js +26 -0
  90. package/src/string.d.ts +8 -0
  91. package/src/string.js +29 -0
  92. package/src/struct.d.ts +42 -0
  93. package/src/struct.js +58 -0
  94. package/src/tree.d.ts +11 -0
  95. package/src/tree.js +25 -0
  96. package/src/treemap.d.ts +13 -0
  97. package/src/treemap.js +71 -0
  98. package/src/typedarray.d.ts +31 -0
  99. package/src/typedarray.js +39 -0
  100. package/src/types.d.ts +63 -0
  101. package/src/types.js +1 -0
  102. package/src/utils.d.ts +4 -0
  103. package/src/utils.js +30 -0
  104. package/src/weak.d.ts +2 -0
  105. package/src/weak.js +5 -0
  106. package/stream.d.ts +1 -1
  107. package/stream.js +2 -2
  108. package/string.d.ts +1 -1
  109. package/string.js +2 -2
  110. package/struct.d.ts +2 -2
  111. package/struct.js +2 -2
  112. package/tree.d.ts +1 -1
  113. package/tree.js +3 -3
  114. package/treemap.d.ts +1 -1
  115. package/treemap.js +3 -3
  116. package/typedarray.d.ts +1 -1
  117. package/typedarray.js +3 -3
  118. package/utils.d.ts +1 -1
  119. package/utils.js +1 -1
  120. package/weak.d.ts +1 -1
  121. package/weak.js +1 -1
package/src/dom.d.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { Fn, Dispose, RecordOf, Maybe } from "./types.js";
2
+ export declare const customEvent: <T>(type: string, detail: T, { composed, bubbles, cancelable }?: EventInit) => CustomEvent<T>;
3
+ type ListenOpts = boolean | AddEventListenerOptions;
4
+ export declare const on: {
5
+ <T extends HTMLElement, K extends keyof HTMLElementEventMap>(el: T, name: K, listener: Fn<HTMLElementEventMap[K]>, opts?: ListenOpts): Dispose;
6
+ <T extends SVGGraphicsElement, K extends keyof SVGElementEventMap>(el: T, name: K, listener: Fn<SVGElementEventMap[K]>, opts?: ListenOpts): Dispose;
7
+ <T, K extends keyof MessageEventTargetEventMap>(el: MessageEventTarget<T>, name: K, listener: Fn<MessageEventTargetEventMap[K]>, opts?: ListenOpts): Dispose;
8
+ <K extends keyof DocumentEventMap>(el: Document, name: K, listener: Fn<DocumentEventMap[K]>, opts?: ListenOpts): Dispose;
9
+ <K extends keyof WindowEventMap>(el: Window, name: K, listener: Fn<WindowEventMap[K]>, opts?: ListenOpts): Dispose;
10
+ <K extends keyof WorkerEventMap>(el: Worker, name: K, listener: Fn<WorkerEventMap[K]>, opts?: ListenOpts): Dispose;
11
+ <K extends keyof AbortSignalEventMap>(el: AbortSignal, name: K, listener: Fn<AbortSignalEventMap[K]>, opts?: ListenOpts): Dispose;
12
+ };
13
+ export declare const onAll: {
14
+ <T extends keyof HTMLElementEventMap>(el: HTMLElement, listeners: {
15
+ [k in T]: Fn<HTMLElementEventMap[k]>;
16
+ }, opts?: ListenOpts): Dispose;
17
+ <T extends keyof SVGElementEventMap>(el: SVGGraphicsElement, listeners: {
18
+ [k in T]: Fn<SVGElementEventMap[k]>;
19
+ }, opts?: ListenOpts): Dispose;
20
+ <T, K extends keyof MessageEventTargetEventMap>(el: MessageEventTarget<T>, listeners: {
21
+ [k in K]: Fn<MessageEventTargetEventMap[k]>;
22
+ }, opts?: ListenOpts): Dispose;
23
+ <T extends keyof DocumentEventMap>(el: Document, listeners: {
24
+ [k in T]: Fn<DocumentEventMap[k]>;
25
+ }, opts?: ListenOpts): Dispose;
26
+ <T extends keyof WindowEventMap>(el: Window, listeners: {
27
+ [k in T]: Fn<WindowEventMap[k]>;
28
+ }, opts?: ListenOpts): Dispose;
29
+ <T extends keyof WorkerEventMap>(el: Worker, listeners: {
30
+ [k in T]: Fn<WorkerEventMap[k]>;
31
+ }, opts?: ListenOpts): Dispose;
32
+ <T extends keyof AbortSignalEventMap>(el: AbortSignal, listeners: {
33
+ [k in T]: Fn<AbortSignalEventMap[k]>;
34
+ }, opts?: ListenOpts): Dispose;
35
+ };
36
+ type DragHandlers = Record<'down' | 'move' | 'up', Fn<PointerEvent>>;
37
+ export declare const onDrag: {
38
+ (el: HTMLElement, fns: Partial<DragHandlers>): Dispose;
39
+ (el: SVGGraphicsElement, fns: Partial<DragHandlers>): Dispose;
40
+ };
41
+ export declare const onDragMove: {
42
+ (el: HTMLElement, f: Fn<PointerEvent>): Dispose;
43
+ (el: SVGGraphicsElement, f: Fn<PointerEvent>): Dispose;
44
+ };
45
+ export declare const onResize: (el: Element, f: Fn<ResizeObserverEntry[]>, options?: ResizeObserverOptions) => Dispose;
46
+ export declare const preventDefault: <T extends Event>(e: T) => void;
47
+ export declare const loadImage: (img: HTMLImageElement, src: string) => Promise<HTMLImageElement>;
48
+ export declare const pushState: (path: string, query?: RecordOf<string>, data?: any) => void;
49
+ export declare const qs: (search?: string) => Maybe<RecordOf<string>>;
50
+ export {};
package/src/dom.js ADDED
@@ -0,0 +1,36 @@
1
+ import { forEach, noop } from "./fn.js";
2
+ import { isDefined } from "./checks.js";
3
+ import { EMPTY } from "./constants.js";
4
+ export const customEvent = (type, detail, { composed = false, bubbles = false, cancelable = false } = EMPTY.OBJ) => new CustomEvent(type, { bubbles, cancelable, composed, detail });
5
+ export const on = (el, name, listener, opts = false) => {
6
+ el.addEventListener(name, listener, opts);
7
+ return () => el.removeEventListener(name, listener, opts);
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 => {
11
+ const pointerId = e.pointerId;
12
+ el.setPointerCapture(pointerId);
13
+ down(e);
14
+ const offMove = on(el, 'pointermove', move);
15
+ const offUp = on(el, 'pointerup', e => {
16
+ up(e);
17
+ el.releasePointerCapture(pointerId);
18
+ offMove();
19
+ offUp();
20
+ });
21
+ });
22
+ export const onDragMove = (el, move) => onDrag(el, { move });
23
+ export const onResize = (el, f, options) => {
24
+ const ro = new ResizeObserver(f);
25
+ ro.observe(el, options);
26
+ return () => ro.disconnect();
27
+ };
28
+ export const preventDefault = (e) => e.preventDefault();
29
+ export const loadImage = (img, src) => new Promise((resolve) => {
30
+ const off = on(img, "load", () => (off(), resolve(img)));
31
+ img.src = src;
32
+ });
33
+ export const pushState = (path, query, data) => history.pushState(data, EMPTY.STR, isDefined(query) ? `${path}?${new URLSearchParams(query).toString()}` : path);
34
+ export const qs = (search = location.search) => search.length > 0
35
+ ? Object.fromEntries(new URLSearchParams(search).entries())
36
+ : undefined;
package/src/fn.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import type { Fn, FnV, FnF, Pred, TPred, FirstParam, Last, Constantly, Maybe, Dispose, Reactive } from "./types.js";
2
+ export declare const tee: <T>(f: Fn<T, void>) => Fn<T, T>;
3
+ export declare const comp: <Fs extends Fn[]>(...fs: Fs) => (x: FirstParam<Fs[0]>) => Last<Fs> extends Fn<any, infer R> ? R : never;
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
+ 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
+ export declare const and: <T>(...fs: Pred<T>[]) => (x: T) => boolean;
8
+ export declare const or: <T>(...fs: Pred<T>[]) => (x: T) => boolean;
9
+ 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
+ export declare const noop: () => void;
12
+ export declare const identity: <S, T = S>(x: S) => T;
13
+ export declare const constantly: <T>(x: T) => Constantly<T>;
14
+ export declare const apply: <F extends FnV>(f: F) => (x: Parameters<F>) => ReturnType<F>;
15
+ export declare const call: <T extends unknown[]>(f: Fn<T>) => (...xs: T) => ReturnType<typeof f>;
16
+ export declare const bind: <A extends unknown[], B extends unknown[], R>(f: (...args: [...A, ...B]) => R, ...args: A) => (...args: B) => R;
17
+ export declare const lazy: <F extends FnV>(f: F, ...xs: Parameters<F>) => Fn<void, ReturnType<F>>;
18
+ export declare const where: <T, U, V>(p: Pred<T>, f: Fn<T, U>, g: Fn<T, V>) => (x: T) => U | V;
19
+ export declare const before: <F extends Fn<void>>(f: F) => FnF;
20
+ 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
+ export declare const filter: <T>(p: Pred<T>, f: Fn<T>) => (x: T) => void;
23
+ export declare const filterT: <T>(p: TPred<T>, f: Fn<T>) => <S>(x: S) => void;
24
+ export declare const scan: <T, S>(f: FnV<[S, T], S>, a: S) => Fn<T, S>;
25
+ 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
+ export declare const abortable: <T = any>(f: Fn<AbortSignal>) => (reason?: T) => void;
27
+ export declare const disposable: (f: Fn<Fn<Dispose, Dispose>>, disposes?: Set<Dispose>) => Dispose;
28
+ export declare const disposing: () => <F extends FnV<any[], Maybe<Dispose>>>(f: F) => FnV<Parameters<F>, Dispose>;
29
+ export declare const disposeWith: <T>(f: Fn<T, Dispose>) => (g: Fn<Fn<T>>) => Dispose;
30
+ 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>) => (x: boolean) => Dispose;
32
+ export declare const voidify: <F extends FnV>(f: F) => (...xs: Parameters<F>) => void;
33
+ export declare const effectual: <T>(f: Reactive<T>, g: Fn<T>) => Dispose;
package/src/fn.js ADDED
@@ -0,0 +1,61 @@
1
+ import { add } from "./set.js";
2
+ export const tee = f => x => (f(x), x);
3
+ export const comp = (...fs) => (x) => fs.reduce((x, f) => f(x), x);
4
+ export const pipe = (x, ...fs) => fs.reduce((x, f) => f(x), x);
5
+ export const forEach = (...fs) => (x) => fs.forEach(f => f(x));
6
+ export const map = (...fs) => (x) => fs.map(f => f(x));
7
+ export const and = (...fs) => (x) => fs.reduce((a, f) => a && f(x), true);
8
+ export const or = (...fs) => (x) => fs.reduce((a, f) => a || f(x), false);
9
+ export const not = (p) => (x) => !p(x);
10
+ export const comb = (c) => (...fs) => (x) => c(...fs.map(f => f(x)));
11
+ export const noop = () => { };
12
+ export const identity = (x) => x;
13
+ export const constantly = (x) => () => x;
14
+ export const apply = (f) => (x) => f.apply(null, x);
15
+ export const call = (f) => (...xs) => f(xs);
16
+ export const bind = (f, ...args) => f.bind(null, ...args);
17
+ export const lazy = (f, ...xs) => () => f(...xs);
18
+ export const where = (p, f, g) => (x) => p(x) ? f(x) : g(x);
19
+ export const before = (f) => g => (...xs) => (f(), g(...xs));
20
+ export const after = (f) => g => (...xs) => {
21
+ const r = g(...xs);
22
+ f();
23
+ return r;
24
+ };
25
+ export const mapForEach = (...fs) => comp(map(...fs), apply((forEach)));
26
+ export const filter = (p, f) => (x) => { if (p(x))
27
+ f(x); };
28
+ export const filterT = (p, f) => (x) => { if (p(x))
29
+ f(x); };
30
+ export const scan = (f, a) => (x) => a = f(a, x);
31
+ export const memoize = (f, key = (...xs) => xs[0], cache = new Map()) => (...xs) => {
32
+ const k = key(...xs);
33
+ if (!cache.has(k))
34
+ cache.set(k, f(...xs));
35
+ return cache.get(k);
36
+ };
37
+ export const abortable = (f) => {
38
+ const ctrl = new AbortController();
39
+ f(ctrl.signal);
40
+ return (reason) => ctrl.abort(reason);
41
+ };
42
+ export const disposable = (f, disposes = new Set()) => {
43
+ f(bind(add, disposes));
44
+ return () => (disposes.forEach(g => g()), disposes.clear());
45
+ };
46
+ export const disposing = () => {
47
+ let dispose = noop;
48
+ const stop = () => dispose();
49
+ return (f) => (...xs) => (stop(), dispose = f(...xs) ?? noop, stop);
50
+ };
51
+ export const disposeWith = (f) => (g) => disposable((d) => g(comp(f, d)));
52
+ export const idemp = (d, f) => disposing()((...xs) => {
53
+ const cleanup = f(...xs) ?? noop;
54
+ return forEach(cleanup, d(cleanup));
55
+ });
56
+ export const toggler = (f) => {
57
+ let cancel = noop;
58
+ return (x) => (cancel(), (cancel = x ? f() : noop));
59
+ };
60
+ export const voidify = (f) => (...xs) => { f(...xs); };
61
+ export const effectual = (f, g) => disposable(d => d(f(idemp(d, g))));
package/src/http.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export declare const assertStatus: (status: number) => (resp: Response) => void;
2
+ export declare const contentLength: (response: Response) => number;
3
+ export declare const fetchBuffer: (url: string | URL | Request, init?: RequestInit) => Promise<ArrayBuffer>;
4
+ export declare const fetchText: (url: string | URL | Request, init?: RequestInit) => Promise<string>;
5
+ export declare const fetchDOM: (url: string | URL | Request, init?: RequestInit) => Promise<HTMLElement>;
6
+ export declare const fetchImage: (src: string) => Promise<HTMLImageElement>;
7
+ export declare const fetchImageBitmap: (url: string | URL | Request, init?: RequestInit) => Promise<ImageBitmap>;
8
+ export declare const fetchJSON: (url: string | URL | Request, init?: RequestInit) => Promise<any>;
9
+ export declare const fetchDataUrl: (url: string | URL | Request, init?: RequestInit) => Promise<string>;
package/src/http.js ADDED
@@ -0,0 +1,33 @@
1
+ import { tee } from "./fn.js";
2
+ import { assert } from "./checks.js";
3
+ export const assertStatus = (status) => (resp) => assert(resp.status == status, `Invalid HTTP Status ${resp.status}: ${resp.url}`);
4
+ export const contentLength = (response) => {
5
+ const cl = response.headers.get("content-length");
6
+ return cl ? parseInt(cl) : -1;
7
+ };
8
+ export const fetchBuffer = (url, init) => fetch(url, init)
9
+ .then(tee(assertStatus(200)))
10
+ .then(resp => resp.blob())
11
+ .then(blob => blob.arrayBuffer());
12
+ export const fetchText = (url, init) => fetchBuffer(url, init).then(buf => new TextDecoder("utf-8").decode(buf));
13
+ export const fetchDOM = (url, init) => fetchText(url, init).then(txt => new DOMParser().parseFromString(txt, "image/svg+xml").documentElement);
14
+ export const fetchImage = (src) => new Promise((resolve, reject) => {
15
+ const image = new Image();
16
+ image.crossOrigin = "anonymous";
17
+ image.src = src;
18
+ image.onload = () => resolve(image);
19
+ image.onerror = reject;
20
+ });
21
+ export const fetchImageBitmap = async (url, init) => fetch(url, init)
22
+ .then(tee(assertStatus(200)))
23
+ .then(resp => resp.blob())
24
+ .then(blob => createImageBitmap(blob, { colorSpaceConversion: "none" }));
25
+ export const fetchJSON = (url, init) => fetchText(url, init).then(JSON.parse);
26
+ export const fetchDataUrl = (url, init) => fetch(url, init)
27
+ .then(tee(assertStatus(200)))
28
+ .then(resp => resp.blob())
29
+ .then(blob => new Promise(resolve => {
30
+ const f = new FileReader();
31
+ f.addEventListener("load", () => resolve(f.result));
32
+ f.readAsDataURL(blob);
33
+ }));
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/src/index.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,43 @@
1
+ import type { Fn, FnV, Indexed, Pred } from "./types.js";
2
+ export declare const next: <T>(xs: Iterator<T>) => T;
3
+ export declare const repeat: (n: number) => <A>(x: A) => Iterator<A>;
4
+ export declare const just: <T>(x: T) => IteratorObject<T>;
5
+ export declare const enumerate: (n?: number) => <T>(x: T) => Indexed<T>;
6
+ export declare function empty(): Generator<never, void, unknown>;
7
+ export declare function concat<T>(...xxs: ReadonlyArray<IteratorObject<T>>): Generator<T, void, unknown>;
8
+ export declare const toArray: <T>(xs: IteratorObject<T>) => T[];
9
+ export declare const map: <T, U = T>(f: Fn<T, U>) => (xs: IteratorObject<T>) => IteratorObject<U, undefined, unknown>;
10
+ export declare const filter: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => IteratorObject<T, undefined, unknown>;
11
+ export declare const drop: (n: number) => <T>(xs: IteratorObject<T>) => IteratorObject<T, undefined, unknown>;
12
+ export declare const flatMap: <T, U = Iterable<T>>(f: Fn<T, Iterator<U> | Iterable<U>>) => (xs: IteratorObject<T>) => IteratorObject<U, undefined, unknown>;
13
+ export declare const forEach: <T>(f?: Fn<T>) => (xs: IteratorObject<T>) => void;
14
+ export declare const some: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => boolean;
15
+ export declare const every: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => boolean;
16
+ export declare const find: <T>(f: Pred<T>) => (xs: IteratorObject<T>) => T | undefined;
17
+ export declare const flat: <T>(xs: IteratorObject<IteratorObject<T>>) => IteratorObject<T, undefined, unknown>;
18
+ export declare const prepend: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
19
+ export declare const append: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
20
+ export declare const first: <T>(xs: IteratorObject<T>) => T | undefined;
21
+ export declare const reduce: <T, S>(f: FnV<[S, T, number], S>, a: S) => (xs: IteratorObject<T>) => S;
22
+ export declare const reduceRec: <T, A, R>(xs: IteratorObject<T>, f: FnV<[A, T, Fn<A>], R>, o: A, result: Fn<A, R>) => R;
23
+ export declare const traverse: <T>(children: Fn<T, Iterable<T>>, f: Fn<T>) => Fn<T>;
24
+ export declare const take: (n: number) => <T>(xs: IteratorObject<T>) => Generator<T>;
25
+ export declare function arange(a: number, b?: number, s?: number): Generator<number, void, unknown>;
26
+ export declare const linspace: (a: number, b: number, n: number) => Generator<number, void, unknown>;
27
+ export declare const steps: (a: number, b: number, s?: number, o?: number, eps?: number) => Generator<number, void, unknown>;
28
+ export declare const strided: (n: number, s: number) => <T>(xs: Iterable<T>) => Generator<T[], void, unknown>;
29
+ export declare const stopAt: <T>(p: Pred<T>) => (xs: IteratorObject<T>) => Generator<T, void, unknown>;
30
+ export declare const until: (p: Pred<void>) => <A>(xs: IteratorObject<A>) => Generator<A, void, unknown>;
31
+ export type Iterators<T extends any[]> = {
32
+ [k in keyof T]: IteratorObject<T[k]>;
33
+ };
34
+ export declare function zip<T extends unknown[]>(...xs: Iterators<T>): Generator<T>;
35
+ export declare const iterA: <T>(xs: Promise<T>[]) => AsyncGenerator<Awaited<T>, void, unknown>;
36
+ export declare const forEachA: <T>(f?: Fn<T>) => (xs: AsyncIterable<T>) => Promise<void>;
37
+ export declare const mapA: <A, B>(f: Fn<A, B>) => Fn<AsyncIterator<A>, AsyncIterableIterator<B>>;
38
+ export declare const flatMapA: <A, B>(f: Fn<A, Iterable<B>>) => (xs: AsyncIterator<A>) => AsyncGenerator<Awaited<B>, void, any>;
39
+ export declare const filterA: <A>(f: Pred<A>) => Fn<AsyncIterator<A>, AsyncIterableIterator<A>>;
40
+ export declare const untilA: (p: Pred<void>) => <A>(xs: AsyncIterator<A>) => AsyncGenerator<Awaited<A>, void, unknown>;
41
+ export declare const reduceA: <T, S>(f: FnV<[S, T], S>, a: S) => (xs: AsyncIterator<T>) => Promise<S>;
42
+ export declare const race: (n: number) => <T>(xs: IteratorObject<Promise<T>>) => AsyncGenerator<Awaited<T>, void, unknown>;
43
+ export declare const zmap: <T extends unknown[]>(f: FnV<T>) => (...xs: Iterators<T>) => void;
@@ -0,0 +1,158 @@
1
+ import { assert, isDefined } from "./checks.js";
2
+ import { pipe, comp, scan, apply, noop, identity, constantly } from "./fn.js";
3
+ import { set } from "./map.js";
4
+ export const next = (xs) => {
5
+ const { done, value } = xs.next();
6
+ assert(isDefined(done) && !done, "iterator done");
7
+ return value;
8
+ };
9
+ export const repeat = n => function* (x) { for (let i = 0; i < n; i++)
10
+ yield x; };
11
+ export const just = function* (x) { yield x; };
12
+ export const enumerate = (n = 0) => x => [n++, x];
13
+ export function* empty() { }
14
+ ;
15
+ export function* concat(...xxs) { for (const xs of xxs)
16
+ yield* xs; }
17
+ ;
18
+ export const toArray = (xs) => xs.toArray();
19
+ export const map = (f) => (xs) => xs.map(f);
20
+ export const filter = (f) => (xs) => xs.filter(f);
21
+ export const drop = (n) => (xs) => xs.drop(n);
22
+ export const flatMap = (f) => (xs) => xs.flatMap(f);
23
+ export const forEach = (f = noop) => (xs) => xs.forEach(f);
24
+ export const some = (f) => (xs) => xs.some(f);
25
+ export const every = (f) => (xs) => xs.every(f);
26
+ export const find = (f) => (xs) => xs.find(f);
27
+ export const flat = (xs) => xs.flatMap(identity);
28
+ export const prepend = (ys) => (xs) => concat(ys, xs);
29
+ export const append = (ys) => (xs) => concat(xs, ys);
30
+ export const first = (xs) => xs.find(constantly(true));
31
+ export const reduce = (f, a) => (xs) => xs.reduce(f, a);
32
+ export const reduceRec = (xs, f, o, result) => {
33
+ const next = (o) => {
34
+ const { done, value } = xs.next();
35
+ if (done)
36
+ return result(o);
37
+ else
38
+ return f(o, value, next);
39
+ };
40
+ return next(o);
41
+ };
42
+ export const traverse = (children, f) => (x) => (f(x), forEach(traverse(children, f))(Iterator.from(children(x))));
43
+ export const take = n => function* (xs) {
44
+ for (let i = 0; i < n; i++) {
45
+ const { done, value } = xs.next();
46
+ if (done)
47
+ break;
48
+ yield value;
49
+ }
50
+ };
51
+ export function* arange(a, b = Number.NaN, s = 1) {
52
+ if (Number.isNaN(b))
53
+ b = a, a = 0;
54
+ for (let i = a; i < b; i += s)
55
+ yield i;
56
+ }
57
+ export const linspace = (a, b, n) => arange(a, b + 1e-6, (b - a) / (n - 1));
58
+ export const steps = (a, b, s = 1, o = 0, eps = 1e-8) => {
59
+ const start = Math.floor((a - o + s - eps) / s) * s + o;
60
+ const end = Math.floor((b - o) / s) * s + o;
61
+ return arange(start, end + s - eps, s);
62
+ };
63
+ export const strided = (n, s) => function* (xs) {
64
+ const cur = [];
65
+ for (const x of xs) {
66
+ cur.push(x);
67
+ if (cur.length == n) {
68
+ yield cur;
69
+ for (let i = 0; i < s; i++)
70
+ cur.shift();
71
+ }
72
+ }
73
+ };
74
+ export const stopAt = (p) => function* (xs) {
75
+ let x = xs.next();
76
+ while (!x.done) {
77
+ yield x.value;
78
+ if (p(x.value))
79
+ return;
80
+ x = xs.next();
81
+ }
82
+ };
83
+ export const until = (p) => function* (xs) {
84
+ if (p())
85
+ return;
86
+ let x = xs.next();
87
+ while (!x.done) {
88
+ yield x.value;
89
+ if (p())
90
+ return;
91
+ x = xs.next();
92
+ }
93
+ };
94
+ export function* zip(...xs) {
95
+ let val = xs.map(x => x.next());
96
+ while (val.every(x => !x.done)) {
97
+ yield val.map(x => x.value);
98
+ val = xs.map(x => x.next());
99
+ }
100
+ }
101
+ ;
102
+ export const iterA = async function* (xs) {
103
+ for await (const x of xs)
104
+ yield x;
105
+ };
106
+ export const forEachA = (f = noop) => async (xs) => {
107
+ for await (const x of xs)
108
+ f(x);
109
+ };
110
+ export const mapA = f => async function* (xs) {
111
+ let x = await xs.next();
112
+ while (!x.done)
113
+ yield f(x.value), x = await xs.next();
114
+ };
115
+ export const flatMapA = (f) => async function* (xs) {
116
+ let x = await xs.next();
117
+ while (!x.done) {
118
+ yield* f(x.value);
119
+ x = await xs.next();
120
+ }
121
+ };
122
+ export const filterA = f => async function* (xs) {
123
+ let x = await xs.next();
124
+ while (!x.done) {
125
+ if (f(x.value))
126
+ yield x.value;
127
+ x = await xs.next();
128
+ }
129
+ };
130
+ export const untilA = (p) => async function* (xs) {
131
+ if (p())
132
+ return;
133
+ let x = await xs.next();
134
+ while (!x.done) {
135
+ yield x.value;
136
+ if (p())
137
+ return;
138
+ x = await xs.next();
139
+ }
140
+ };
141
+ export const reduceA = (f, a) => async (xs) => {
142
+ const g = scan(f, a);
143
+ await forEachA()(mapA((x) => a = g(x))(xs));
144
+ return a;
145
+ };
146
+ export const race = (n) => async function* (xs) {
147
+ const it = pipe(xs, map(enumerate()), map(([i, x]) => [i, x.then(y => [i, y])]));
148
+ const accum = reduce((set), new Map());
149
+ const tasks = pipe(it, (take(n)), accum);
150
+ const next = comp((take(1)), accum);
151
+ while (tasks.size > 0) {
152
+ const [i, x] = await Promise.race(tasks.values());
153
+ tasks.delete(i);
154
+ yield x;
155
+ next(it);
156
+ }
157
+ };
158
+ export const zmap = (f) => (...xs) => forEach(apply(f))(zip(...xs));
package/src/json.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const fromBytes: (data: AllowSharedBufferSource, options?: TextDecodeOptions) => any;
package/src/json.js ADDED
@@ -0,0 +1 @@
1
+ export const fromBytes = (data, options) => JSON.parse(new TextDecoder("utf-8").decode(data, options));
package/src/map.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { FnV, Maybe } from "./types.js";
2
+ export declare const transform: <K, S, V>({ create, update, remove }: {
3
+ create: FnV<[S, K, Map<K, V>], V>;
4
+ update: FnV<[V, S, K, Map<K, V>], V>;
5
+ remove?: FnV<[V, K, Map<K, V>]>;
6
+ }, dst?: Map<K, V>) => (src: Map<K, S>) => Map<K, V>;
7
+ export declare const set: <K, V>(m: Map<K, V>, [i, x]: [K, V]) => Map<K, V>;
8
+ export declare const get: <K, V>(m: Map<K, V>, k: K, d: V) => V;
9
+ export declare const update: <K, V>(m: Map<K, V>, k: K, f: FnV<[V | undefined, K, Map<K, V>], V>) => Map<K, V>;
10
+ export declare const first: <K, V>(m: Map<K, V>) => Maybe<V>;
11
+ export declare const shift: <K, V>(m: Map<K, V>) => Maybe<[K, V]>;
12
+ export declare const reduce: <K, V, T>(m: Map<K, V>, f: FnV<[T, V, K, Map<K, V>], T>, a: T) => T;
package/src/map.js ADDED
@@ -0,0 +1,35 @@
1
+ import { isDefined } from "./checks.js";
2
+ import * as I from "./iterable.js";
3
+ export const transform = ({ create, update, remove }, dst = new Map()) => (src) => {
4
+ for (const k of dst.keys())
5
+ if (!src.has(k)) {
6
+ remove && remove(dst.get(k), k, dst);
7
+ dst.delete(k);
8
+ }
9
+ src.forEach((v, k) => {
10
+ if (!dst.has(k))
11
+ dst.set(k, create(v, k, dst));
12
+ else
13
+ dst.set(k, update(dst.get(k), v, k, dst));
14
+ });
15
+ return dst;
16
+ };
17
+ export const set = (m, [i, x]) => (m.set(i, x), m);
18
+ export const get = (m, k, d) => m.has(k) ? m.get(k) : d;
19
+ export const update = (m, k, f) => set(m, [k, f(m.get(k), k, m)]);
20
+ export const first = (m) => {
21
+ const k = I.first(m.keys());
22
+ if (isDefined(k))
23
+ return m.get(k);
24
+ return undefined;
25
+ };
26
+ export const shift = (m) => {
27
+ const k = I.first(m.keys());
28
+ if (isDefined(k)) {
29
+ const v = m.get(k);
30
+ m.delete(k);
31
+ return [k, v];
32
+ }
33
+ return undefined;
34
+ };
35
+ export const reduce = (m, f, a) => (m.forEach((v, k) => a = f(a, v, k, m)), a);
package/src/math.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ export declare const PI: number;
2
+ export declare const TAU: number;
3
+ export declare const HALF_PI: number;
4
+ export declare const THIRD_PI: number;
5
+ export declare const QUARTER_PI: number;
6
+ export declare const SIXTH_PI: number;
7
+ export declare const SQRT2: number;
8
+ export declare const GOLDEN: number;
9
+ export declare const FOUR_OVER_PI: number;
10
+ export declare const INV_PI: number;
11
+ export declare const INV_TAU: number;
12
+ export declare const INV_HALF_PI: number;
13
+ export declare const U32_MAX = 4294967295;
14
+ export declare const EPS: number;
15
+ export declare const radians: (x: number) => number;
16
+ export declare const degrees: (x: number) => number;
17
+ export declare const safeSqrt: (x: number) => number;
18
+ export declare const lerp: (a: number, b: number, t: number) => number;
19
+ export declare const clamp: (a: number, b: number, x: number) => number;
20
+ export declare const saturate: (x: number) => number;
21
+ export declare const norm: (a: number, b: number, x: number) => number;
22
+ export declare const fit: (a: number, b: number, c: number, d: number, x: number) => number;
23
+ export declare const efit: (a: number, b: number, c: number, d: number, x: number) => number;
24
+ export declare const fit01: (a: number, b: number, x: number) => number;
25
+ export declare const ewmabc: (beta?: number, t?: number) => (x: number) => number;
26
+ export declare const sinc: (x: number) => number;
27
+ export declare const nextpow2: (x: number) => number;
28
+ export declare const mod: (x: number, m: number) => number;
29
+ export declare const logn: (n: number, x: number) => number;
30
+ export declare const floordiv: (x: number, b: number) => number;
31
+ export declare const zoom: (x: number, s: number, p: number) => number;
32
+ export declare const align: (x: number, b: number) => number;
33
+ export declare const sign11: (x: number) => 1 | -1;
34
+ export declare const nudge: (x: number, v?: number, eps?: number) => number;
package/src/math.js ADDED
@@ -0,0 +1,43 @@
1
+ import { isclose } from "./checks.js";
2
+ export const PI = Math.PI;
3
+ export const TAU = PI * 2;
4
+ export const HALF_PI = PI / 2;
5
+ export const THIRD_PI = PI / 3;
6
+ export const QUARTER_PI = PI / 4;
7
+ export const SIXTH_PI = PI / 6;
8
+ export const SQRT2 = Math.sqrt(2);
9
+ export const GOLDEN = (1 + 5 ** .5) * .5;
10
+ export const FOUR_OVER_PI = 4 / Math.PI;
11
+ export const INV_PI = 1 / PI;
12
+ export const INV_TAU = 1 / TAU;
13
+ export const INV_HALF_PI = 1 / HALF_PI;
14
+ export const U32_MAX = 0xFFFFFFFF;
15
+ export const EPS = Number.EPSILON;
16
+ const DEG2RAD = PI / 180, RAD2DEG = 180 / PI;
17
+ export const radians = (x) => x * DEG2RAD;
18
+ export const degrees = (x) => x * RAD2DEG;
19
+ export const safeSqrt = (x) => Math.sqrt(Math.max(x, 0));
20
+ export const lerp = (a, b, t) => a * (1 - t) + b * t;
21
+ export const clamp = (a, b, x) => Math.max(a, Math.min(b, x));
22
+ export const saturate = (x) => clamp(0, 1, x);
23
+ export const norm = (a, b, x) => (x - a) / ((b - a));
24
+ // https://www.sidefx.com/docs/houdini/expressions/fit.html
25
+ export const fit = (a, b, c, d, x) => lerp(c, d, saturate(norm(a, b, x)));
26
+ // https://www.sidefx.com/docs/houdini/expressions/efit.html
27
+ export const efit = (a, b, c, d, x) => lerp(c, d, norm(a, b, x));
28
+ // https://www.sidefx.com/docs/houdini/expressions/fit01.html
29
+ export const fit01 = (a, b, x) => lerp(a, b, saturate(x));
30
+ export const ewmabc = (beta = .8, t = 1) => {
31
+ let z = 0, bc = beta ** (t - 1);
32
+ return (x) => (z = lerp(x, z, beta)) / (1 - (bc *= beta));
33
+ };
34
+ export const sinc = (x) => x < Number.EPSILON ? 1.0 : Math.sin(x) / x;
35
+ export const nextpow2 = (x) => Math.ceil(Math.log2(Math.abs(x)));
36
+ export const mod = (x, m) => ((x % m) + m) % m;
37
+ export const logn = (n, x) => Math.log(x) / Math.log(n);
38
+ // https://www.desmos.com/calculator/ybbuznxbcc
39
+ export const floordiv = (x, b) => Math.floor(x / b + (x < 0 ? 1 : 0));
40
+ export const zoom = (x, s, p) => (x - p) * s + p;
41
+ export const align = (x, b) => (x + b - 1) & -b;
42
+ export const sign11 = (x) => x >= 0 ? 1 : -1;
43
+ export const nudge = (x, v = 0, eps = EPS) => isclose(x, v, eps) ? sign11(x - v) * eps + v : x;
package/src/npy.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { TypedArray, Elements } from "./types.js";
2
+ declare const DTYPES: readonly ["|u1", "<u2", "<u4", "|i1", "<i2", "<i4", "<f4", "<f8"];
3
+ type DType = Elements<typeof DTYPES>;
4
+ export interface NDArray {
5
+ data: TypedArray;
6
+ dtype: DType;
7
+ shape: number[];
8
+ }
9
+ export declare const read: (buf: ArrayBuffer) => NDArray;
10
+ export {};
package/src/npy.js ADDED
@@ -0,0 +1,29 @@
1
+ const DTYPES = [
2
+ "|u1", "<u2", "<u4",
3
+ "|i1", "<i2", "<i4",
4
+ "<f4", "<f8",
5
+ // "<u8", "<i8",
6
+ ];
7
+ const TYPED_ARRAY = {
8
+ "|u1": Uint8Array,
9
+ "<u2": Uint16Array,
10
+ "<u4": Uint32Array,
11
+ "|i1": Int8Array,
12
+ "<i2": Int16Array,
13
+ "<i4": Int32Array,
14
+ "<f4": Float32Array,
15
+ "<f8": Float64Array,
16
+ // "<u8": BigUint64Array,
17
+ // "<i8": BigInt64Array,
18
+ };
19
+ export const read = (buf) => {
20
+ const headerLength = new DataView(buf.slice(8, 10)).getUint8(0);
21
+ const hcontents = new TextDecoder("utf-8").decode(buf.slice(10, 10 + headerLength));
22
+ const { descr, shape } = JSON.parse(hcontents
23
+ .replace(/'/g, '"')
24
+ .replace("False", "false")
25
+ .replace("(", "[")
26
+ .replace(/,*\),*/g, "]"));
27
+ const data = new TYPED_ARRAY[descr](buf, headerLength + 10);
28
+ return { data, shape, dtype: descr };
29
+ };
@@ -0,0 +1,4 @@
1
+ export declare const add: (a: number, x: number) => number;
2
+ export declare const toFixed: (x: number | bigint, n?: number) => string | bigint;
3
+ export declare const toFixedMax: (x: number, n?: number) => string;
4
+ export declare const roundTo: (x: number, n: number) => number;
package/src/number.js ADDED
@@ -0,0 +1,13 @@
1
+ import { isBigInt } from "./checks.js";
2
+ export const add = (a, x) => a + x;
3
+ export const toFixed = (x, n = 0) => isBigInt(x) ? x : x.toFixed(n);
4
+ export const toFixedMax = (x, n = 0) => {
5
+ const y = Number(x.toFixed(n));
6
+ return `${Math.sign(y) == -1 ? '-' : ''}${Math.abs(y)}`;
7
+ };
8
+ export const roundTo = (x, n) => {
9
+ const inv = 1. / n;
10
+ const result = Math.round(x * inv) / inv;
11
+ const decimals = n.toString().split(".")[1]?.length || 0;
12
+ return parseFloat(result.toFixed(decimals));
13
+ };
@@ -0,0 +1,15 @@
1
+ import type { RecordOf, Elements, Maybe, FnV, ValueOf, EntriesTuple, UnionToTuple, Entry } from "./types.js";
2
+ export declare const entries: <T extends RecordOf<any>>(o: T) => EntriesTuple<T>;
3
+ export declare const keys: <T extends RecordOf<any>>(o: T) => UnionToTuple<keyof T>;
4
+ export declare const entry: <K extends PropertyKey, V>(k: K, v: V) => Entry<K, V>;
5
+ export declare const fromEntries: <T extends Entry<PropertyKey, any>[]>(xs: T) => { [K in T[number] as K[0]]: K[1]; };
6
+ export declare const mapEntries: <T extends RecordOf<any>, S>(o: T, f: FnV<[Entry<keyof T, ValueOf<T>>, number], Entry<keyof T, S>>) => { [K in Entry<keyof T, S> as K[0]]: K[1]; };
7
+ export declare const mapValues: <T extends RecordOf<any>, S>(o: T, f: FnV<[ValueOf<T>, keyof T], S>) => { [K in Entry<keyof T, S> as K[0]]: K[1]; };
8
+ export declare const pick: <T extends RecordOf<any>, R extends Array<keyof T>>(obj: T, keys: R) => Pick<T, Elements<R>>;
9
+ export declare const omit: <T extends RecordOf<any>, R extends Array<keyof T>>(obj: T, keys: R) => Omit<T, Elements<R>>;
10
+ export declare const assoc: <T extends RecordOf<any>>(a: T, [k, v]: [keyof T, ValueOf<T>]) => T;
11
+ type Merge<T extends RecordOf<unknown>, S extends RecordOf<unknown>, V> = {
12
+ [K in keyof (T & S)]: V;
13
+ };
14
+ export declare const merge: <T, S = T, V = T>(f: FnV<[Maybe<T>, Maybe<S>], V>) => <X extends RecordOf<T>, Y extends RecordOf<T>>(x: X, y: Y) => Merge<X, Y, V>;
15
+ export {};