@cgtk/std 0.0.185 → 0.0.187

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/port.js ADDED
@@ -0,0 +1,53 @@
1
+ import { forEach, noop } from "./fn";
2
+ import { isDefined } from "./checks";
3
+ import { on } from "./dom";
4
+ import { promise } from "./async";
5
+ ;
6
+ export const onMessages = (target, fs) => on(target, "message", ({ data: { type, payload } }) => (type in fs) && fs[type](payload));
7
+ export const onMessage = (target, name, f) => on(target, "message", ({ data: { type, payload } }) => type == name && f(payload));
8
+ export const postMessages = (port) => new Proxy({}, {
9
+ get: (_, type) => (payload, options) => port.postMessage({ type, payload }, options)
10
+ });
11
+ export const client = (target) => {
12
+ const fns = new Map();
13
+ onMessage(target, "rpc", ({ id, result }) => {
14
+ const f = fns.get(id);
15
+ if (isDefined(f))
16
+ f(result), fns.delete(id);
17
+ });
18
+ const { rpc } = postMessages(target);
19
+ let _id = 0;
20
+ return new Proxy({}, {
21
+ get(_, name) {
22
+ const g = (transfer) => (...args) => {
23
+ const id = _id++;
24
+ rpc({ id, name, args }, { transfer });
25
+ return new Promise(resolve => fns.set(id, resolve));
26
+ };
27
+ const f = ((...args) => g()(...args));
28
+ f.transfer = g;
29
+ return f;
30
+ }
31
+ });
32
+ };
33
+ export const server = (target) => {
34
+ const fns = new Map();
35
+ const { rpc } = postMessages(target);
36
+ onMessage(target, "rpc", async ({ id, name, args }) => {
37
+ const f = fns.get(name);
38
+ if (isDefined(f))
39
+ rpc({ id, result: await promise(f(...args)) });
40
+ });
41
+ return new Proxy({}, {
42
+ get: (_, k) => (f) => (fns.set(k, f), () => fns.delete(k))
43
+ });
44
+ };
45
+ export const startClient = async (worker, f) => {
46
+ const cli = client(worker);
47
+ await cli.start();
48
+ return forEach(f(worker) ?? noop, () => cli.stop().then(() => worker.terminate()));
49
+ };
50
+ export const startServer = async (worker, f) => {
51
+ const ser = server(worker);
52
+ return ser.start(async () => { ser.stop(await promise(f(worker))); });
53
+ };
package/progress.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn } from './types.js';
1
+ import type { Fn } from './types';
2
2
  export type Progress = {
3
3
  begin: Fn<void>;
4
4
  progress: Fn<number>;
package/rect.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { Tuple } from "./types";
2
+ export type Rect = Tuple<number, 4>;
3
+ export declare const rect: (x: number, y: number, w: number, h: number) => Rect;
4
+ export declare const vertical: (ratio: number[]) => (i: number) => ([x, _, w, h]: Rect) => Rect;
5
+ export declare const horizontal: (ratio: number[]) => (i: number) => ([_, y, w, h]: Rect) => Rect;
package/rect.js ADDED
@@ -0,0 +1,16 @@
1
+ import { add } from "./number";
2
+ export const rect = (x, y, w, h) => [x, y, w, h];
3
+ export const vertical = (ratio) => {
4
+ const tot = ratio.reduce(add, 0);
5
+ const cum = [0];
6
+ ratio.forEach(x => cum.push(x / tot + cum[cum.length - 1]));
7
+ cum.pop();
8
+ return (i) => ([x, _, w, h]) => [x, cum[i] * h, w, ratio[i] / tot * h];
9
+ };
10
+ export const horizontal = (ratio) => {
11
+ const tot = ratio.reduce(add, 0);
12
+ const cum = [0];
13
+ ratio.forEach(x => cum.push(x / tot + cum[cum.length - 1]));
14
+ cum.pop();
15
+ return (i) => ([_, y, w, h]) => [cum[i] * w, y, ratio[i] / tot * w, h];
16
+ };
package/resource.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { Fn, Dispose, Resource, RecordOf } from "./types";
2
+ export declare const resource: <T>(x: T, d?: Dispose) => Resource<T>;
3
+ export declare const map: <T, S>(r: Resource<T>, g: Fn<T, S>) => Resource<S>;
4
+ type Structure<T> = T | Structure<T>[] | RecordOf<Structure<T>>;
5
+ type Resolved<T extends Structure<Resource<any>>> = {
6
+ [K in keyof T]: T[K] extends Resource<infer R> ? R : T[K] extends Structure<Resource<any>> ? Resolved<T[K]> : never;
7
+ };
8
+ export declare const combine: <R extends Structure<Resource<any>>>(rs: R) => Resource<Resolved<R>>;
9
+ export {};
package/resource.js ADDED
@@ -0,0 +1,20 @@
1
+ import { forEach, noop, comp } from "./fn";
2
+ import { push, empty } from "./array";
3
+ import { assoc } from "./object";
4
+ import { reduceRec } from './iterable';
5
+ import { isFunction } from "./checks";
6
+ export const resource = (x, d = noop) => (f) => forEach(f(x) ?? noop, d);
7
+ export const map = (r, g) => f => r(comp(g, f));
8
+ const isResource = (r) => isFunction(r);
9
+ export const combine = (rs) => f => {
10
+ const rec = (rs, f) => {
11
+ if (isResource(rs)) {
12
+ return rs(x => f(x) ?? noop);
13
+ }
14
+ else if (Array.isArray(rs)) {
15
+ return reduceRec(Iterator.from(rs), (o, r, next) => rec(r, (x) => next(push(o, x))), empty(), o => f(o) ?? noop);
16
+ }
17
+ return reduceRec(Iterator.from(Object.entries(rs)), (o, [k, r], next) => rec(r, (x) => next(assoc(o, [k, x]))), {}, r => f(r) ?? noop);
18
+ };
19
+ return rec(rs, f);
20
+ };
package/schedule.d.ts CHANGED
@@ -1,8 +1,10 @@
1
- import type { Fn, FnV, Reactive } from './types';
1
+ import type { Dispose, Fn, FnV, Reactive } from './types';
2
2
  export declare const raf: Reactive<number>;
3
3
  export declare const timeout: (ms: number) => Reactive<void>;
4
- export declare const schedule: <T>(s: Reactive<T>) => <F extends FnV>(f: F) => (...xs: Parameters<F>) => ReturnType<Fn<Parameters<F>>>;
4
+ export declare const schedule: <T>(s: Reactive<T>) => <F extends FnV>(f: F) => FnV<Parameters<F>, Dispose>;
5
+ export declare const idempotent: <T>(s: Reactive<T>, d: Fn<Dispose, Dispose>) => <F extends FnV>(f: F) => FnV<Parameters<F>, Dispose>;
5
6
  export declare const debounce: <T>(sched?: Reactive<T>) => Reactive<T>;
6
7
  export declare const reset: <T>(sched?: Reactive<T>) => Reactive<T>;
7
- export declare const repeatedly: (sched?: Reactive<any>) => (f: Fn<void>) => () => any;
8
- export declare const toggler: (f: Fn<void, Fn<void>>) => (x: boolean) => Fn<void>;
8
+ export declare const repeatedly: <T>(f: Fn<void>, sched?: Reactive<T>) => () => any;
9
+ export declare const delayAfter: <F extends FnV<any>>(delta: number, n: number, f: F) => (x: any) => any;
10
+ export declare const micro: <F extends FnV>(f: F) => (...xs: Parameters<F>) => void;
package/schedule.js CHANGED
@@ -1,17 +1,9 @@
1
- import { isUndefined, isDefined } from './checks';
2
- import { comp, lazy, noop, bind, apply, call } from './fn';
3
- export const raf = f => {
4
- const id = requestAnimationFrame(f);
5
- return () => cancelAnimationFrame(id);
6
- };
7
- export const timeout = (ms) => (f) => {
8
- const id = setTimeout(f, ms);
9
- return () => clearTimeout(id);
10
- };
11
- export const schedule = (s) => (f) => {
12
- const g = bind(lazy, f);
13
- return call(comp(apply(g), s));
14
- };
1
+ import { isUndefined, isDefined, counted } from './checks';
2
+ import { comp, lazy, bind, apply, call, where, disposing, noop } from './fn';
3
+ export const raf = f => bind(cancelAnimationFrame, requestAnimationFrame(f));
4
+ export const timeout = (ms) => f => bind(clearTimeout, setTimeout(f, ms));
5
+ export const schedule = (s) => (f) => call(comp(apply(bind((lazy), f)), s));
6
+ export const idempotent = (s, d) => (f) => disposing()((...xs) => d(s(() => f(...xs))));
15
7
  export const debounce = (sched = raf) => {
16
8
  let cancel;
17
9
  const stop = () => { if (isDefined(cancel))
@@ -23,22 +15,19 @@ export const debounce = (sched = raf) => {
23
15
  };
24
16
  };
25
17
  export const reset = (sched = raf) => {
26
- let cancel;
27
- const stop = () => { if (isDefined(cancel))
28
- cancel(), cancel = undefined; };
18
+ let cancel = noop;
19
+ const stop = () => (cancel(), cancel = noop);
29
20
  return (f) => {
30
21
  stop();
31
22
  cancel = sched(f);
32
23
  return stop;
33
24
  };
34
25
  };
35
- export const repeatedly = (sched = raf) => (f) => {
26
+ export const repeatedly = (f, sched = raf) => {
36
27
  let cancel;
37
28
  const g = () => (f(), (cancel = sched(g)));
38
29
  cancel = sched(g);
39
30
  return () => cancel();
40
31
  };
41
- export const toggler = (f) => {
42
- let cancel = noop;
43
- return (x) => (cancel(), (cancel = x ? f() : noop));
44
- };
32
+ export const delayAfter = (delta, n, f) => where(counted(n), f, schedule(reset(timeout(delta)))(f));
33
+ export const micro = (f) => (...xs) => queueMicrotask(() => f(...xs));
package/set.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const add: <T>(xs: Set<T>, x: T) => () => boolean;
package/set.js ADDED
@@ -0,0 +1 @@
1
+ export const add = (xs, x) => (xs.add(x), () => xs.delete(x));
package/signal.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import type { Fn, FnV, Reactive, RecordOf } from "./types";
1
+ import type { Fn, FnV, Fns, Reactive, RecordOf } from "./types";
2
2
  export type Signal<T> = Fn<T> & {
3
3
  on: Reactive<T>;
4
4
  };
5
- export declare const create: <A>(f: Fn<A>, on: Reactive<A>) => Signal<A>;
5
+ export declare const create: <T>(f: Fn<T>, on: Reactive<T>) => Signal<T>;
6
6
  export declare const signal: <T>(fns?: Set<Fn<T>>) => Signal<T>;
7
7
  export interface Value<T> {
8
8
  value: T;
@@ -10,10 +10,15 @@ export interface Value<T> {
10
10
  op: <F extends FnV>(f: Fn<T, F>) => F;
11
11
  ops: <F extends RecordOf<FnV>>(f: Fn<T, F>) => F;
12
12
  set: Fn<T>;
13
- update: Fn;
13
+ update(): void;
14
14
  }
15
15
  export declare const value: <T>(x: T) => Value<T>;
16
16
  export type Values<T extends RecordOf<any>> = {
17
- [key in keyof T]: Value<T[key]>;
17
+ [k in keyof T]: Value<T[k]>;
18
18
  };
19
19
  export declare const values: <T extends RecordOf<any>>(vals: T) => Values<T>;
20
+ export declare const map: <T, S>(source: Reactive<T>, f: Fn<T, S>) => Reactive<S>;
21
+ export declare const merge: <T extends RecordOf<any>>(vals: Values<T>) => Reactive<T>;
22
+ export declare const mux: <I extends RecordOf<any>, O extends RecordOf<any>>(init: Fn<Fns<O>, Fns<I>>) => Fns<I> & {
23
+ on: { [k in keyof O]: Reactive<O[k]>; };
24
+ };
package/signal.js CHANGED
@@ -1,25 +1,48 @@
1
- import { after, constantly, noop } from "./fn";
1
+ import { after, constantly, noop, bind, comp, forEach } from "./fn";
2
2
  import { mapValues } from "./object";
3
+ import { isDefined } from "./checks";
4
+ import { add } from "./set";
3
5
  export const create = (f, on) => {
4
6
  const g = f;
5
7
  g.on = on;
6
8
  return g;
7
9
  };
8
- export const signal = (fns = new Set()) => create((x) => fns.forEach(f => f(x)), (f) => (fns.add(f), () => fns.delete(f)));
10
+ export const signal = (fns = new Set()) => create((x) => fns.forEach(f => f(x)), bind(add, fns));
9
11
  ;
10
12
  export const value = (x) => {
11
13
  const sig = signal();
12
14
  const notify = after(() => sig(x));
13
15
  const op = (f) => notify(f(x));
14
- const mop = mapValues(notify);
15
16
  return {
16
17
  get value() { return x; },
17
18
  set value(y) { sig(x = y); },
18
19
  op,
19
20
  on: (f) => (f(x), sig.on(f)),
20
- ops: (f) => mop(f(x)),
21
+ ops: (f) => mapValues(f(x), notify),
21
22
  set: op(constantly((y) => x = y)),
22
23
  update: op(constantly(noop))
23
24
  };
24
25
  };
25
- export const values = (vals) => mapValues(value)(vals);
26
+ export const values = (vals) => mapValues(vals, value);
27
+ export const map = (source, f) => g => source(comp(f, g));
28
+ export const merge = (vals) => f => {
29
+ const sub = () => f(mapValues(vals, (v) => v.value));
30
+ return forEach(...Object.values(vals).map((v) => v.on(sub)));
31
+ };
32
+ export const mux = (init) => {
33
+ const sigs = {};
34
+ const ins = init(new Proxy({}, {
35
+ get: (_, k) => (x) => {
36
+ const f = sigs[k];
37
+ if (isDefined(f))
38
+ f(x);
39
+ }
40
+ }));
41
+ ins.on = new Proxy({}, {
42
+ get: (_, k) => f => {
43
+ const sig = sigs[k] ??= signal();
44
+ return sig.on(f);
45
+ }
46
+ });
47
+ return ins;
48
+ };
package/string.d.ts CHANGED
@@ -1,5 +1,7 @@
1
+ import type { Tuple } from "./types";
1
2
  export declare const basename: (path: string) => string;
2
3
  export declare const dirname: (path: string) => string;
3
- export declare const objectURL: (data: Uint8Array, type: string) => string;
4
+ export declare const splitext: (path: string) => Tuple<string, 2>;
5
+ export declare const objectURL: (data: Uint8Array<ArrayBuffer>, type: string) => string;
4
6
  export declare const decode: (label?: string, opts?: TextDecoderOptions) => (x: Uint8Array, stream?: boolean) => string;
5
7
  export declare const readLine: (delims?: Set<number>) => (data: Uint8Array, offset?: number) => string;
package/string.js CHANGED
@@ -6,6 +6,12 @@ export const dirname = (path) => {
6
6
  parts.pop();
7
7
  return parts.join("/");
8
8
  };
9
+ export const splitext = (path) => {
10
+ const idx = path.lastIndexOf(".");
11
+ if (idx == -1)
12
+ return [path, ""];
13
+ return [path.slice(0, idx), path.slice(idx + 1)];
14
+ };
9
15
  export const objectURL = (data, type) => URL.createObjectURL(new Blob([data], { type }));
10
16
  export const decode = (label = "utf-8", opts = {
11
17
  fatal: false, ignoreBOM: false
package/struct.d.ts CHANGED
@@ -1,26 +1,40 @@
1
- import type { TypedArray } from "./types";
2
- export interface View<T> {
3
- (buf?: ArrayBuffer, offset?: number, length?: number): T;
1
+ import type { TypedArray, Fn } from "./types";
2
+ import type { Constructor } from "./typedarray";
3
+ export interface Type {
4
4
  size: number;
5
5
  align: number;
6
6
  }
7
- type Inner<T> = T extends View<infer U> ? U : never;
8
- type Num<T extends TypedArray> = View<T> & {
9
- length: 1;
7
+ export type Vec<T extends TypedArray, N extends number> = Type & {
8
+ length: N;
9
+ type: Constructor<T>;
10
10
  };
11
- export declare const u8: Num<Uint8Array<ArrayBufferLike>>;
12
- export declare const u16: Num<Uint16Array<ArrayBufferLike>>;
13
- export declare const u32: Num<Uint32Array<ArrayBufferLike>>;
14
- export declare const i8: Num<Int8Array<ArrayBufferLike>>;
15
- export declare const i16: Num<Int16Array<ArrayBufferLike>>;
16
- export declare const i32: Num<Int32Array<ArrayBufferLike>>;
17
- export declare const f32: Num<Float32Array<ArrayBufferLike>>;
18
- export declare const f64: Num<Float64Array<ArrayBufferLike>>;
19
- export declare const vec: <T extends TypedArray, N extends number>(t: Num<T>, length: N, align?: number, size?: number) => View<T>;
20
- export declare const array: <T>(type: View<T>, length: number, size?: number, align?: number) => View<T[]>;
21
- export type Fields = ReadonlyArray<readonly [string, View<unknown>]>;
22
- export type Struct<T extends Fields> = {
23
- [K in T[number] as K[0]]: K[1] extends Num<any> ? number : Inner<K[1]>;
11
+ export declare const u8: <N extends number = 1>(length?: N, align?: number) => Vec<Uint8Array<ArrayBufferLike>, N>;
12
+ export declare const u16: <N extends number = 1>(length?: N, align?: number) => Vec<Uint16Array<ArrayBufferLike>, N>;
13
+ export declare const u32: <N extends number = 1>(length?: N, align?: number) => Vec<Uint32Array<ArrayBufferLike>, N>;
14
+ export declare const i8: <N extends number = 1>(length?: N, align?: number) => Vec<Int8Array<ArrayBufferLike>, N>;
15
+ export declare const i16: <N extends number = 1>(length?: N, align?: number) => Vec<Int16Array<ArrayBufferLike>, N>;
16
+ export declare const i32: <N extends number = 1>(length?: N, align?: number) => Vec<Int32Array<ArrayBufferLike>, N>;
17
+ export declare const f16: <N extends number = 1>(length?: N, align?: number) => Vec<Float16Array<ArrayBufferLike>, N>;
18
+ export declare const f32: <N extends number = 1>(length?: N, align?: number) => Vec<Float32Array<ArrayBufferLike>, N>;
19
+ export declare const f64: <N extends number = 1>(length?: N, align?: number) => Vec<Float64Array<ArrayBufferLike>, N>;
20
+ export type Array<T extends Type> = Type & {
21
+ type: T;
22
+ length: number;
23
+ offset: Fn<number, number>;
24
24
  };
25
- export declare const struct: <T extends Fields>(fs: T) => View<Struct<T>>;
25
+ export declare const isArray: (t: any) => t is Array<any>;
26
+ export declare const array: <T extends Type>(type: T, length: number, align?: number) => Array<T>;
27
+ type Field<T extends Type> = readonly [name: string, type: T, aligb?: number];
28
+ export type Struct<T extends ReadonlyArray<Field<any>>> = Type & {
29
+ fields: {
30
+ [K in T[number] as K[0]]: K[1];
31
+ };
32
+ offset(key: T[number][0]): number;
33
+ };
34
+ export declare const isStruct: (t: any) => t is Struct<any>;
35
+ export declare const struct: <T extends ReadonlyArray<Field<any>>>(fs: T, align?: number, offset?: number) => Struct<T>;
36
+ export type View<T extends Type> = T extends Vec<infer C, any> ? C : T extends Array<infer S> ? View<S>[] : T extends Struct<infer F> ? {
37
+ [K in F[number] as K[0]]: K[1] extends Vec<infer C, infer N> ? (N extends 1 ? number : C) : View<K[1]>;
38
+ } : never;
39
+ export declare const view: <T extends Vec<any, any> | Array<any> | Struct<any>>(t: T, buf?: ArrayBufferLike, offset?: number) => View<T>;
26
40
  export {};
package/struct.js CHANGED
@@ -1,6 +1,43 @@
1
1
  import { align as _align } from "./math";
2
2
  import { isTypedArray } from "./checks";
3
- import * as TA from "./typedarray";
3
+ const vec = (type) => (length = 1, align = type.BYTES_PER_ELEMENT) => ({
4
+ type, align, length, size: type.BYTES_PER_ELEMENT * length,
5
+ });
6
+ export const u8 = vec(Uint8Array);
7
+ export const u16 = vec(Uint16Array);
8
+ export const u32 = vec(Uint32Array);
9
+ export const i8 = vec(Int8Array);
10
+ export const i16 = vec(Int16Array);
11
+ export const i32 = vec(Int32Array);
12
+ export const f16 = vec(Float16Array);
13
+ export const f32 = vec(Float32Array);
14
+ export const f64 = vec(Float64Array);
15
+ export const isArray = (t) => Object.hasOwn(t, "length") && Object.hasOwn(t, "offset");
16
+ export const array = (type, length, align = type.align) => {
17
+ const step = _align(type.size, align);
18
+ return ({
19
+ type, align, length,
20
+ size: step * length,
21
+ offset: i => i * step
22
+ });
23
+ };
24
+ export const isStruct = (t) => Object.hasOwn(t, "offset") && Object.hasOwn(t, "fields");
25
+ export const struct = (fs, align = 0, offset = 0) => {
26
+ const fields = {}, offsets = {};
27
+ for (const [name, type, align_ = type.align] of fs) {
28
+ align = Math.max(align, align_);
29
+ offset = _align(offset, align_);
30
+ fields[name] = type;
31
+ offsets[name] = offset;
32
+ offset += type.size;
33
+ }
34
+ return {
35
+ size: _align(offset, align),
36
+ align,
37
+ fields: fields,
38
+ offset: k => offsets[k]
39
+ };
40
+ };
4
41
  const property = (value) => ({
5
42
  enumerable: true,
6
43
  ...(isTypedArray(value) && value.length == 1 ? {
@@ -8,50 +45,12 @@ const property = (value) => ({
8
45
  set: (x) => value[0] = x,
9
46
  } : { value })
10
47
  });
11
- const num = (t) => {
12
- const f = ((buf, offset, length = 1) => new t(buf, offset, length));
13
- f.size = t.BYTES_PER_ELEMENT;
14
- f.align = t.BYTES_PER_ELEMENT;
15
- return f;
16
- };
17
- export const u8 = num(Uint8Array);
18
- export const u16 = num(Uint16Array);
19
- export const u32 = num(Uint32Array);
20
- export const i8 = num(Int8Array);
21
- export const i16 = num(Int16Array);
22
- export const i32 = num(Int32Array);
23
- export const f32 = num(Float32Array);
24
- export const f64 = num(Float64Array);
25
- export const vec = (t, length, align = t.align, size = t.size * length) => {
26
- const f = ((buf = new ArrayBuffer(size), offset = 0) => t(buf, offset, length));
27
- f.size = _align(size, align);
28
- f.align = align;
29
- return f;
30
- };
31
- export const array = (type, length, size = type.size * length, align = type.align) => {
32
- const f = ((buf = new ArrayBuffer(size), offset = 0) => Array.from({ length }, (_, i) => type(buf, offset + i * _align(type.size, type.align))));
33
- f.size = size;
34
- f.align = align;
35
- return f;
36
- };
37
- export const struct = (fs) => {
38
- let align = 0, offset = 0;
39
- const fields = {}, offsets = {};
40
- for (const [name, type] of fs) {
41
- align = Math.max(align, type.align);
42
- offset = _align(offset, type.align);
43
- fields[name] = type;
44
- offsets[name] = offset;
45
- offset += type.size;
48
+ export const view = (t, buf = new ArrayBuffer(t.size), offset = 0) => {
49
+ if (isArray(t)) {
50
+ return Array.from({ length: t.length }, (_, i) => view(t.type, buf, offset + t.offset(i)));
46
51
  }
47
- const size = _align(offset, align);
48
- const f = ((buf = new ArrayBuffer(size), offset = 0) => {
49
- const res = {};
50
- for (const [name, type] of Object.entries(fields))
51
- Object.defineProperty(res, name, property(type(buf, offset + offsets[name])));
52
- return res;
53
- });
54
- f.size = size;
55
- f.align = align;
56
- return f;
52
+ else if (isStruct(t)) {
53
+ return Object.entries(t.fields).reduce((o, [name, type]) => Object.defineProperty(o, name, property(view(type, buf, offset + t.offset(name)))), {});
54
+ }
55
+ return new t.type(buf, offset, t.length);
57
56
  };
package/tree.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, FnV, TPred, PredV } from './types.js';
1
+ import type { Fn, FnV, TPred, PredV } from './types';
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>;
@@ -8,13 +8,4 @@ export declare const value: <S, T = S>(p?: TPred<Tree<S, T>, Node<S, T>>) => (x:
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>>>;
11
- export declare const toggleChildren: <S, T = S, C = void>(g: FnV<[Tree<S, T>, C], boolean>, update?: FnV<[C, S], C>, p?: TPred<Tree<S, T>>) => Fn<C, Fn<Node<S, T>, Node<S, T>>>;
12
- type TreeObject<T extends Node<string>[]> = {
13
- [K in T[number] as K extends string ? K : K[0]]: K extends Tree<string> ? {
14
- value: string;
15
- children: TreeObject<K[1]>;
16
- } : string;
17
- };
18
- export declare const asObject: <T extends Node<string>[]>(items: T, f?: FnV<[string, string], string>, update?: FnV<[string, string], string>, context?: string) => TreeObject<T>;
19
11
  export declare const zip: <S, T, U, V>(na: Node<S, T>[], nb: Node<U, V>[]) => Node<[S, U], [T, V]>[];
20
- export {};
package/tree.js CHANGED
@@ -1,7 +1,6 @@
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';
1
+ import { identity } from './fn';
2
+ import { EMPTY } from './constants';
3
+ import { zip as zipI } from './iterable';
5
4
  export const tree = (value, children = EMPTY.LIST) => [value, children];
6
5
  export const isTree = (x) => Array.isArray(x) && x.length == 2 && Array.isArray(x[1]);
7
6
  export const children = (p = isTree) => (x) => p(x) ? x[1] : EMPTY.LIST;
@@ -12,11 +11,6 @@ export const transform = (node, leaf, p = isTree) => {
12
11
  };
13
12
  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
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);
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
14
  export const zip = (na, nb) => {
21
15
  const res = [];
22
16
  const va = value(), vb = value();
package/treemap.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isUndefined } from "./checks.js";
1
+ import { isUndefined } from "./checks";
2
2
  export class Treemap {
3
3
  static from(iter) {
4
4
  const tree = new Treemap();
package/typedarray.d.ts CHANGED
@@ -1,6 +1,12 @@
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>;
1
+ import type { Fn, FnV, TypedArray, BufferType, Numbers } from "./types";
2
+ export interface Constructor<T extends TypedArray> {
3
+ new (length: number): T;
4
+ new (buffer: BufferType<T> | ArrayBufferLike, byteOffset?: number, length?: number): T;
5
+ readonly BYTES_PER_ELEMENT: number;
6
+ of(...items: number[]): T;
7
+ from(arrayLike: ArrayLike<number>): T;
8
+ }
9
+ export type View<T extends TypedArray> = FnV<[BufferType<T>?, number?], T>;
4
10
  export declare const view: <T extends TypedArray>(type: Constructor<T>, length: number) => View<T>;
5
11
  export declare const u8: (length: number) => View<Uint8Array<ArrayBufferLike>>;
6
12
  export declare const u16: (length: number) => View<Uint16Array<ArrayBufferLike>>;
@@ -8,6 +14,7 @@ export declare const u32: (length: number) => View<Uint32Array<ArrayBufferLike>>
8
14
  export declare const i8: (length: number) => View<Int8Array<ArrayBufferLike>>;
9
15
  export declare const i16: (length: number) => View<Int16Array<ArrayBufferLike>>;
10
16
  export declare const i32: (length: number) => View<Int32Array<ArrayBufferLike>>;
17
+ export declare const f16: (length: number) => View<Float16Array<ArrayBufferLike>>;
11
18
  export declare const f32: (length: number) => View<Float32Array<ArrayBufferLike>>;
12
19
  export declare const f64: (length: number) => View<Float64Array<ArrayBufferLike>>;
13
20
  export declare const UintArray: (n: number) => Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
@@ -18,6 +25,7 @@ export declare const concat: <T extends TypedArray>(chunks: T[]) => T;
18
25
  export declare const iter: (size: number, stride?: number) => <T extends TypedArray>(data: T) => Generator<T>;
19
26
  export declare const tile: (n: number) => (out: TypedArray, x: TypedArray) => TypedArray;
20
27
  export declare const tileEvery: (m: number, n: number) => <T extends TypedArray>(xs: T, out?: T) => T;
28
+ export declare const strided: <T extends TypedArray>(data: T, size: number, stride?: number, out?: T) => T;
21
29
  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;
30
+ export declare const unweld: <T extends TypedArray>(data: T, indices: Numbers, size: number, stride?: number, out?: T) => T;
23
31
  export declare const toFixed: (n: number, sep?: string) => (xs: TypedArray) => string;
package/typedarray.js CHANGED
@@ -8,6 +8,7 @@ export const u32 = bind((view), Uint32Array);
8
8
  export const i8 = bind((view), Int8Array);
9
9
  export const i16 = bind((view), Int16Array);
10
10
  export const i32 = bind((view), Int32Array);
11
+ export const f16 = bind((view), Float16Array);
11
12
  export const f32 = bind((view), Float32Array);
12
13
  export const f64 = bind((view), Float64Array);
13
14
  export const UintArray = (n) => n < 256 ? Uint8Array : n < 65536 ? Uint16Array : Uint32Array;
@@ -23,7 +24,6 @@ export const iter = (size, stride = size) => function* (data) {
23
24
  const C = ctor(data);
24
25
  for (let i = 0, n = data.length; i < n - size + 1; i += stride) {
25
26
  const offset = data.byteOffset + i * C.BYTES_PER_ELEMENT;
26
- // https://github.com/microsoft/TypeScript/pull/59417
27
27
  yield new C(data.buffer, offset, size);
28
28
  }
29
29
  };
@@ -33,11 +33,12 @@ export const tile = (n) => (out, x) => {
33
33
  return out;
34
34
  };
35
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 strided = (data, size, stride = size, out = new (ctor(data))(data.length / size * stride)) => (iter(size)(data).forEach((p, i) => out.set(p, i * stride)), out);
36
37
  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
+ export const unweld = (data, indices, size, stride = size, out = new (ctor(data))(indices.length * stride)) => {
38
39
  indices.forEach((i, j) => {
39
40
  for (let k = 0; k < size; k++)
40
- out[j * size + k] = data[i * size + k];
41
+ out[j * stride + k] = data[i * size + k];
41
42
  });
42
43
  return out;
43
44
  };