@cgtk/std 0.0.189 → 0.0.191

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/array.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn, Tuple, Numbers } from "./types";
1
+ import type { Fn, Tuple } from "./types";
2
2
  export declare const empty: <T>() => T[];
3
3
  export declare const push: <T>(xs: T[], x: T) => T[];
4
4
  export declare const unshift: <T>(xs: T[], x: T) => T[];
@@ -11,4 +11,4 @@ export declare const retain: <T>(n: number, xs?: T[]) => (x: T) => T[];
11
11
  export declare const tuple: <T, N extends number>(...xs: T[] & {
12
12
  length: N;
13
13
  }) => Tuple<T, N>;
14
- export declare const seek: <T>(arr: T[] | Numbers, vals: Set<T | number>, offset?: number) => number;
14
+ export declare const seek: <T>(arr: ArrayLike<T>, vals: Set<T | number>, offset?: number) => number;
package/checks.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import type { TypedArray, Pred, TPred, Optional, Numbers } from "./types";
1
+ import type { TypedArray, NumberArray, BigIntArray, Pred, TPred, Optional } from "./types";
2
2
  export declare const isTypedArray: (x: any) => x is TypedArray;
3
+ export declare const isNumberArray: (x: any) => x is NumberArray;
4
+ export declare const isBigIntArray: (x: any) => x is BigIntArray;
3
5
  export declare const isUndefined: (x: any) => x is undefined;
4
6
  export declare const isNull: (x: any) => x is null;
5
7
  export declare const isDefined: <T>(x: Optional<T>) => x is T;
@@ -8,10 +10,11 @@ export declare function assertType<T>(x: any, pred: TPred<T>, msg: string): asse
8
10
  export declare const isObject: TPred<Object>;
9
11
  export declare const isString: TPred<string>;
10
12
  export declare const isNumber: TPred<number>;
13
+ export declare const isBigInt: TPred<bigint>;
11
14
  export declare const isPositive: Pred<number>;
12
15
  export declare const isTs: <T>(p: TPred<T>) => TPred<T[]>;
13
16
  export declare const isclose: (a: number, b: number, eps?: number) => boolean;
14
- export declare const allclose: (a: Numbers, b: Numbers, eps?: number) => boolean;
17
+ export declare const allclose: (a: Array<number> | NumberArray, b: Array<number> | NumberArray, eps?: number) => boolean;
15
18
  export declare const equal: <T>(a: T, b?: T) => boolean;
16
19
  export declare const strictEqual: <T>(a: T, b?: T) => boolean;
17
20
  export declare const isDiffPrev: <T>(eq?: (a: T, b?: T | undefined) => boolean) => Pred<T>;
package/checks.js CHANGED
@@ -3,7 +3,13 @@ import { retain } from "./array";
3
3
  export const isTypedArray = (x) => x && (x instanceof Uint8Array || x instanceof Uint8ClampedArray ||
4
4
  x instanceof Uint16Array || x instanceof Uint32Array ||
5
5
  x instanceof Int8Array || x instanceof Int16Array || x instanceof Int32Array ||
6
- x instanceof Float32Array || x instanceof Float64Array);
6
+ x instanceof Float16Array || x instanceof Float32Array || x instanceof Float64Array ||
7
+ x instanceof BigUint64Array || x instanceof BigInt64Array);
8
+ export const isNumberArray = (x) => x && (x instanceof Uint8Array || x instanceof Uint8ClampedArray ||
9
+ x instanceof Uint16Array || x instanceof Uint32Array ||
10
+ x instanceof Int8Array || x instanceof Int16Array || x instanceof Int32Array ||
11
+ x instanceof Float16Array || x instanceof Float32Array || x instanceof Float64Array);
12
+ export const isBigIntArray = (x) => x && (x instanceof BigUint64Array || x instanceof BigInt64Array);
7
13
  export const isUndefined = (x) => x === undefined;
8
14
  export const isNull = (x) => x === null;
9
15
  export const isDefined = (x) => !isUndefined(x);
@@ -15,6 +21,7 @@ export function assertType(x, pred, msg) { assert(pred(x), msg); }
15
21
  export const isObject = (x) => x !== null && typeof x === "object";
16
22
  export const isString = (x) => typeof x === "string";
17
23
  export const isNumber = (x) => typeof x === 'number';
24
+ export const isBigInt = (x) => typeof x === "bigint";
18
25
  export const isPositive = (x) => x > 0;
19
26
  export const isTs = (p) => and(Array.isArray, (xs) => xs.every(p));
20
27
  export const isclose = (a, b, eps = Number.EPSILON) => Math.abs(a - b) < eps;
package/number.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare const add: (a: number, x: number) => number;
2
+ export declare const toFixed: (x: number | bigint, n?: number) => string | bigint;
2
3
  export declare const toFixedMax: (x: number, n?: number) => string;
3
4
  export declare const roundTo: (x: number, n: number) => number;
package/number.js CHANGED
@@ -1,4 +1,6 @@
1
+ import { isBigInt } from "./checks";
1
2
  export const add = (a, x) => a + x;
3
+ export const toFixed = (x, n = 0) => isBigInt(x) ? x : x.toFixed(n);
2
4
  export const toFixedMax = (x, n = 0) => {
3
5
  const y = Number(x.toFixed(n));
4
6
  return `${Math.sign(y) == -1 ? '-' : ''}${Math.abs(y)}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cgtk/std",
3
- "version": "0.0.189",
3
+ "version": "0.0.191",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "exports": {
package/struct.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import type { TypedArray, BigIntArray, Fn } from "./types";
1
+ import type { TypedArray, Fn } from "./types";
2
2
  import type { Constructor } from "./typedarray";
3
3
  export interface Type {
4
4
  size: number;
5
5
  align: number;
6
6
  }
7
- export type Vec<T extends TypedArray | BigIntArray, N extends number> = Type & {
7
+ export type Vec<T extends TypedArray, N extends number> = Type & {
8
8
  length: N;
9
9
  type: Constructor<T>;
10
10
  };
@@ -36,7 +36,7 @@ export type Struct<T extends ReadonlyArray<Field<any>>> = Type & {
36
36
  export declare const isStruct: (t: any) => t is Struct<any>;
37
37
  export declare const struct: <T extends ReadonlyArray<Field<any>>>(fs: T, align?: number, offset?: number) => Struct<T>;
38
38
  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> ? {
39
- [K in F[number] as K[0]]: K[1] extends Vec<infer C, infer N> ? (N extends 1 ? number : C) : View<K[1]>;
39
+ [K in F[number] as K[0]]: K[1] extends Vec<infer C, infer N> ? (N extends 1 ? (C extends TypedArray ? number : bigint) : C) : View<K[1]>;
40
40
  } : never;
41
41
  export declare const view: <T extends Vec<any, any> | Array<any> | Struct<any>>(t: T, buf?: ArrayBufferLike, offset?: number) => View<T>;
42
42
  export {};
package/struct.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { align as _align } from "./math";
2
- import { isTypedArray } from "./checks";
2
+ import { isTypedArray, isBigIntArray } from "./checks";
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
  });
@@ -42,7 +42,7 @@ export const struct = (fs, align = 0, offset = 0) => {
42
42
  };
43
43
  const property = (value) => ({
44
44
  enumerable: true,
45
- ...(isTypedArray(value) && value.length == 1 ? {
45
+ ...((isTypedArray(value) || isBigIntArray(value)) && value.length == 1 ? {
46
46
  get: () => value[0],
47
47
  set: (x) => value[0] = x,
48
48
  } : { value })
package/typedarray.d.ts CHANGED
@@ -1,31 +1,31 @@
1
- import type { Fn, FnV, TypedArray, BigIntArray, BufferType, Numbers } from "./types";
2
- export interface Constructor<T extends TypedArray | BigIntArray> {
1
+ import type { FnV, TypedArray, NumberArray, BufferType, Number } from "./types";
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;
5
5
  readonly BYTES_PER_ELEMENT: number;
6
- of(...items: number[] | bigint[]): T;
7
- from(arrayLike: ArrayLike<number | bigint>): T;
6
+ of(...items: Number<T>[]): T;
7
+ from(arrayLike: ArrayLike<Number<T>>): T;
8
8
  }
9
9
  export type View<T extends TypedArray> = FnV<[BufferType<T>?, number?], T>;
10
10
  export declare const view: <T extends TypedArray>(type: Constructor<T>, length: number) => View<T>;
11
11
  export declare const u8: (length: number) => View<Uint8Array<ArrayBufferLike>>;
12
12
  export declare const u16: (length: number) => View<Uint16Array<ArrayBufferLike>>;
13
13
  export declare const u32: (length: number) => View<Uint32Array<ArrayBufferLike>>;
14
+ export declare const u64: (length: number) => View<BigUint64Array<ArrayBufferLike>>;
14
15
  export declare const i8: (length: number) => View<Int8Array<ArrayBufferLike>>;
15
16
  export declare const i16: (length: number) => View<Int16Array<ArrayBufferLike>>;
16
17
  export declare const i32: (length: number) => View<Int32Array<ArrayBufferLike>>;
18
+ export declare const i64: (length: number) => View<BigInt64Array<ArrayBufferLike>>;
17
19
  export declare const f16: (length: number) => View<Float16Array<ArrayBufferLike>>;
18
20
  export declare const f32: (length: number) => View<Float32Array<ArrayBufferLike>>;
19
21
  export declare const f64: (length: number) => View<Float64Array<ArrayBufferLike>>;
20
22
  export declare const UintArray: (n: number) => Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
21
23
  export declare const ctor: <T extends TypedArray>(x: T) => Constructor<T>;
22
24
  export declare const emptyLike: <T extends TypedArray>(x: T) => T;
23
- export declare const set: <T extends TypedArray>(xs: T, x: Numbers, i: number) => T;
25
+ export declare const set: <T extends TypedArray>(xs: T, x: ArrayLike<Number<T>> | T, i: number) => T;
24
26
  export declare const concat: <T extends TypedArray>(chunks: T[]) => T;
25
27
  export declare const iter: (size: number, stride?: number) => <T extends TypedArray>(data: T) => Generator<T>;
26
28
  export declare const tile: (n: number) => (out: TypedArray, x: TypedArray) => TypedArray;
27
29
  export declare const tileEvery: (m: number, n: number) => <T extends TypedArray>(xs: T, out?: T) => T;
28
30
  export declare const strided: <T extends TypedArray>(data: T, size: number, stride?: number, out?: T) => T;
29
- export declare const map: <T extends TypedArray>(it: Fn<T, IteratorObject<T>>, f: FnV<[T, T]>) => (data: T, out?: T) => T;
30
- export declare const unweld: <T extends TypedArray>(data: T, indices: Numbers, size: number, stride?: number, out?: T) => T;
31
- export declare const toFixed: (n: number, sep?: string) => (xs: TypedArray) => string;
31
+ export declare const toFixed: (n: number, sep?: string) => (xs: NumberArray) => string;
package/typedarray.js CHANGED
@@ -5,9 +5,11 @@ export const view = (type, length) => (buf = new ArrayBuffer(type.BYTES_PER_ELEM
5
5
  export const u8 = bind((view), Uint8Array);
6
6
  export const u16 = bind((view), Uint16Array);
7
7
  export const u32 = bind((view), Uint32Array);
8
+ export const u64 = bind((view), BigUint64Array);
8
9
  export const i8 = bind((view), Int8Array);
9
10
  export const i16 = bind((view), Int16Array);
10
11
  export const i32 = bind((view), Int32Array);
12
+ export const i64 = bind((view), BigInt64Array);
11
13
  export const f16 = bind((view), Float16Array);
12
14
  export const f32 = bind((view), Float32Array);
13
15
  export const f64 = bind((view), Float64Array);
@@ -29,17 +31,9 @@ export const iter = (size, stride = size) => function* (data) {
29
31
  };
30
32
  export const tile = (n) => (out, x) => {
31
33
  for (let i = 0; i < n; i++)
32
- out.set(x, i * x.length);
34
+ set(out, x, i * x.length);
33
35
  return out;
34
36
  };
35
37
  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);
37
- export const map = (it, f) => (data, out = emptyLike(data)) => (zmap(f)(it(out), it(data)), out);
38
- export const unweld = (data, indices, size, stride = size, out = new (ctor(data))(indices.length * stride)) => {
39
- indices.forEach((i, j) => {
40
- for (let k = 0; k < size; k++)
41
- out[j * stride + k] = data[i * size + k];
42
- });
43
- return out;
44
- };
38
+ export const strided = (data, size, stride = size, out = new (ctor(data))(data.length / size * stride)) => (iter(size)(data).forEach((p, i) => set(out, p, i * stride)), out);
45
39
  export const toFixed = (n, sep = ',') => (xs) => reduce((push), [])(mapI(x => x.toFixed(n))(Iterator.from(xs))).join(sep);
package/types.d.ts CHANGED
@@ -38,10 +38,14 @@ export type Entries<T> = {
38
38
  export type EntriesTuple<T, K extends UnionToTuple<keyof T> = UnionToTuple<keyof T>> = {
39
39
  [I in keyof K]: K[I] extends keyof T ? [K[I], T[K[I]]] : never;
40
40
  };
41
- export type TypedArray<T extends ArrayBufferLike = ArrayBufferLike> = Uint8Array<T> | Uint16Array<T> | Uint32Array<T> | Int8Array<T> | Int16Array<T> | Int32Array<T> | Float16Array<T> | Float32Array<T> | Float64Array<T>;
41
+ export type UintArray<T extends ArrayBufferLike = ArrayBufferLike> = Uint8Array<T> | Uint16Array<T> | Uint32Array<T>;
42
+ export type IntArray<T extends ArrayBufferLike = ArrayBufferLike> = Int8Array<T> | Int16Array<T> | Int32Array<T>;
43
+ export type FloatArray<T extends ArrayBufferLike = ArrayBufferLike> = Float16Array<T> | Float32Array<T> | Float64Array<T>;
42
44
  export type BigIntArray<T extends ArrayBufferLike = ArrayBufferLike> = BigUint64Array<T> | BigInt64Array<T>;
43
- export type BufferType<T extends TypedArray | BigIntArray> = T extends TypedArray<infer S> ? S : T extends BigIntArray<infer S> ? S : never;
44
- export type Numbers = Array<number> | TypedArray;
45
+ export type NumberArray<T extends ArrayBufferLike = ArrayBufferLike> = UintArray<T> | IntArray<T> | FloatArray<T>;
46
+ export type TypedArray<T extends ArrayBufferLike = ArrayBufferLike> = NumberArray | BigIntArray<T>;
47
+ export type Number<T extends TypedArray> = T extends NumberArray ? number : bigint;
48
+ export type BufferType<T extends TypedArray> = T extends TypedArray<infer S> ? S : never;
45
49
  export type Eq<A, B = A> = FnV<[A, B], boolean>;
46
50
  export type Pred<T> = Fn<T, boolean>;
47
51
  export type PredV<T extends any[]> = FnV<T, boolean>;