@latticexyz/utils 2.0.12-account-kit-10002da0 → 2.0.12-account-kit-9160f5e1a
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/package.json +4 -2
- package/CHANGELOG.md +0 -622
- package/src/CoordMap.spec.ts +0 -57
- package/src/CoordMap.ts +0 -106
- package/src/VoxelCoordMap.ts +0 -76
- package/src/area.ts +0 -15
- package/src/arrays.ts +0 -19
- package/src/bytes.spec.ts +0 -77
- package/src/bytes.ts +0 -85
- package/src/console.ts +0 -62
- package/src/cubic.spec.ts +0 -14
- package/src/cubic.ts +0 -109
- package/src/deferred.ts +0 -14
- package/src/distance.ts +0 -11
- package/src/enums.ts +0 -13
- package/src/eth.ts +0 -42
- package/src/guards.ts +0 -10
- package/src/index.ts +0 -38
- package/src/iterable.spec.ts +0 -56
- package/src/iterable.ts +0 -59
- package/src/math.ts +0 -11
- package/src/mobx.ts +0 -26
- package/src/objects.ts +0 -16
- package/src/pack.spec.ts +0 -37
- package/src/pack.ts +0 -61
- package/src/promise.ts +0 -46
- package/src/proxy.spec.ts +0 -186
- package/src/proxy.ts +0 -101
- package/src/random.ts +0 -17
- package/src/rx.spec.ts +0 -45
- package/src/rx.ts +0 -124
- package/src/sleep.ts +0 -3
- package/src/types.ts +0 -48
- package/src/uuid.ts +0 -70
- package/src/v2/arrayToHex.ts +0 -3
- package/src/v2/bytesToString.ts +0 -1
- package/src/v2/hexToArray.ts +0 -12
- package/src/v2/index.ts +0 -5
- package/src/v2/isHex.ts +0 -6
- package/src/v2/stringToBytes.ts +0 -9
- package/src/worker.ts +0 -16
package/src/CoordMap.spec.ts
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
import { coordToKey, keyToCoord } from "./CoordMap";
|
2
|
-
|
3
|
-
describe("CoordMap", () => {
|
4
|
-
describe("coordToKey", () => {
|
5
|
-
it("should be the inverse of keyToCoord", () => {
|
6
|
-
const MAX = 2 ** 15 - 1;
|
7
|
-
const coords = [
|
8
|
-
{ x: 1, y: 2 },
|
9
|
-
{ x: -1, y: 2 },
|
10
|
-
{ x: 1, y: -2 },
|
11
|
-
{ x: 0, y: 0 },
|
12
|
-
{ x: -1, y: -2 },
|
13
|
-
{ x: MAX, y: MAX },
|
14
|
-
{ x: -1 * MAX, y: MAX },
|
15
|
-
{ x: MAX, y: -1 * MAX },
|
16
|
-
{ x: -1 * MAX, y: -1 * MAX },
|
17
|
-
];
|
18
|
-
|
19
|
-
for (const coord of coords) {
|
20
|
-
const key = coordToKey(coord);
|
21
|
-
expect(keyToCoord(key)).toEqual(coord);
|
22
|
-
}
|
23
|
-
});
|
24
|
-
});
|
25
|
-
|
26
|
-
describe("keyToCoord", () => {
|
27
|
-
it("should be the inverse of coordToKey", () => {
|
28
|
-
const keys = [
|
29
|
-
"11111111111111110000000000000000",
|
30
|
-
"11011101110111011000101001001000",
|
31
|
-
"01101101101110110001010101111001",
|
32
|
-
"11010101111011010010010010001001",
|
33
|
-
"00111111111111110111000001001100",
|
34
|
-
"01101110001111010110010010001001",
|
35
|
-
"10101110011011010010010001010101",
|
36
|
-
"01011011101011010101100100101000",
|
37
|
-
"11011110111101111000010000100011",
|
38
|
-
"10110111011011010101101010001001",
|
39
|
-
"00110111011101110001000100010000",
|
40
|
-
"10110101101101010001001010001001",
|
41
|
-
"01100011010101111010010100110010",
|
42
|
-
"11111111111111111111111111111111",
|
43
|
-
"00000000000000000000000000000000",
|
44
|
-
];
|
45
|
-
|
46
|
-
for (const key of keys) {
|
47
|
-
const numKey = parseInt(key, 2) >> 0;
|
48
|
-
const coord = keyToCoord(numKey);
|
49
|
-
expect(coordToKey(coord)).toEqual(numKey);
|
50
|
-
}
|
51
|
-
});
|
52
|
-
|
53
|
-
it("should work with {x: 0, y: 0}", () => {
|
54
|
-
expect(keyToCoord(0)).toEqual({ x: 0, y: 0 });
|
55
|
-
});
|
56
|
-
});
|
57
|
-
});
|
package/src/CoordMap.ts
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
import { Coord } from "./types";
|
2
|
-
import { transformIterator } from "./iterable";
|
3
|
-
|
4
|
-
const LOWER_HALF_MASK = 2 ** 16 - 1;
|
5
|
-
const MAX_SUPPORTED = 2 ** 15 - 1;
|
6
|
-
|
7
|
-
export function subtract(from: CoordMap<boolean>, subtract: CoordMap<boolean>): CoordMap<boolean> {
|
8
|
-
const result = new CoordMap<boolean>();
|
9
|
-
|
10
|
-
for (const coord of from.coords()) {
|
11
|
-
if (subtract.get(coord)) continue;
|
12
|
-
result.set(coord, true);
|
13
|
-
}
|
14
|
-
|
15
|
-
return result;
|
16
|
-
}
|
17
|
-
|
18
|
-
export function coordToKey(coord: Coord) {
|
19
|
-
const key = (coord.x << 16) | (coord.y & LOWER_HALF_MASK);
|
20
|
-
return key;
|
21
|
-
|
22
|
-
// Old version using strings:
|
23
|
-
// return `${coord.x}/${coord.y}`;
|
24
|
-
}
|
25
|
-
|
26
|
-
export function keyToCoord(key: number): Coord {
|
27
|
-
const x = key >> 16;
|
28
|
-
const y = (key << 16) >> 16;
|
29
|
-
return { x, y };
|
30
|
-
|
31
|
-
// Old version using strings:
|
32
|
-
// const fragments = key.split("/");
|
33
|
-
// return { x: Number(fragments[0]), y: Number(fragments[1]) };
|
34
|
-
}
|
35
|
-
|
36
|
-
export class CoordMap<T> {
|
37
|
-
map: Map<number, T>;
|
38
|
-
defaultValue?: T;
|
39
|
-
|
40
|
-
constructor(props?: { defaultValue?: T }) {
|
41
|
-
this.map = new Map<number, T>();
|
42
|
-
this.defaultValue = props?.defaultValue;
|
43
|
-
}
|
44
|
-
|
45
|
-
static from<T>(coordMapLike: { map: Map<number, T>; defaultValue?: T }): CoordMap<T> {
|
46
|
-
const coordMap = new CoordMap<T>();
|
47
|
-
coordMap.map = coordMapLike.map;
|
48
|
-
coordMap.defaultValue = coordMapLike.defaultValue;
|
49
|
-
return coordMap;
|
50
|
-
}
|
51
|
-
|
52
|
-
set(coord: Coord, value: T) {
|
53
|
-
if (
|
54
|
-
coord.x > MAX_SUPPORTED ||
|
55
|
-
coord.x < -1 * MAX_SUPPORTED ||
|
56
|
-
coord.y > MAX_SUPPORTED ||
|
57
|
-
coord.y < -1 * MAX_SUPPORTED
|
58
|
-
) {
|
59
|
-
throw new Error(`CoordMap only supports coords up to ${MAX_SUPPORTED}`);
|
60
|
-
}
|
61
|
-
return this.map.set(coordToKey(coord), value);
|
62
|
-
}
|
63
|
-
|
64
|
-
get(coord: Coord) {
|
65
|
-
return this.map.get(coordToKey(coord)) ?? this.defaultValue;
|
66
|
-
}
|
67
|
-
|
68
|
-
keys() {
|
69
|
-
return this.map.keys();
|
70
|
-
}
|
71
|
-
|
72
|
-
coords(): IterableIterator<Coord> {
|
73
|
-
return transformIterator(this.map.keys(), (key) => keyToCoord(key));
|
74
|
-
}
|
75
|
-
|
76
|
-
entries() {
|
77
|
-
return this.map.entries();
|
78
|
-
}
|
79
|
-
|
80
|
-
toArray(): [Coord, T][] {
|
81
|
-
const entries = Array.from(this.map.entries());
|
82
|
-
return entries.map(([key, value]) => [keyToCoord(key), value]);
|
83
|
-
}
|
84
|
-
|
85
|
-
values() {
|
86
|
-
return this.map.values();
|
87
|
-
}
|
88
|
-
|
89
|
-
delete(coord: Coord) {
|
90
|
-
return this.map.delete(coordToKey(coord));
|
91
|
-
}
|
92
|
-
|
93
|
-
has(coord: Coord): boolean {
|
94
|
-
return this.map.has(coordToKey(coord));
|
95
|
-
}
|
96
|
-
|
97
|
-
clear() {
|
98
|
-
for (const key of this.map.keys()) {
|
99
|
-
this.map.delete(key);
|
100
|
-
}
|
101
|
-
}
|
102
|
-
|
103
|
-
get size(): number {
|
104
|
-
return this.map.size;
|
105
|
-
}
|
106
|
-
}
|
package/src/VoxelCoordMap.ts
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
import { VoxelCoord } from "./types";
|
2
|
-
import { transformIterator } from "./iterable";
|
3
|
-
|
4
|
-
function coordToKey(coord: VoxelCoord) {
|
5
|
-
// TODO: find a more memory efficient way to store these keys
|
6
|
-
return `${coord.x}/${coord.y}/${coord.z}`;
|
7
|
-
}
|
8
|
-
|
9
|
-
function keyToCoord(key: string): VoxelCoord {
|
10
|
-
const fragments = key.split("/");
|
11
|
-
return { x: Number(fragments[0]), y: Number(fragments[1]), z: Number(fragments[2]) };
|
12
|
-
}
|
13
|
-
|
14
|
-
export class VoxelCoordMap<T> {
|
15
|
-
map: Map<string, T>;
|
16
|
-
defaultValue?: T;
|
17
|
-
|
18
|
-
constructor(props?: { defaultValue?: T }) {
|
19
|
-
this.map = new Map<string, T>();
|
20
|
-
this.defaultValue = props?.defaultValue;
|
21
|
-
}
|
22
|
-
|
23
|
-
static from<T>(coordMapLike: { map: Map<string, T>; defaultValue?: T }): VoxelCoordMap<T> {
|
24
|
-
const coordMap = new VoxelCoordMap<T>();
|
25
|
-
coordMap.map = coordMapLike.map;
|
26
|
-
coordMap.defaultValue = coordMapLike.defaultValue;
|
27
|
-
return coordMap;
|
28
|
-
}
|
29
|
-
|
30
|
-
set(coord: VoxelCoord, value: T) {
|
31
|
-
return this.map.set(coordToKey(coord), value);
|
32
|
-
}
|
33
|
-
|
34
|
-
get(coord: VoxelCoord) {
|
35
|
-
return this.map.get(coordToKey(coord)) ?? this.defaultValue;
|
36
|
-
}
|
37
|
-
|
38
|
-
keys() {
|
39
|
-
return this.map.keys();
|
40
|
-
}
|
41
|
-
|
42
|
-
coords(): IterableIterator<VoxelCoord> {
|
43
|
-
return transformIterator(this.map.keys(), (key) => keyToCoord(key));
|
44
|
-
}
|
45
|
-
|
46
|
-
entries() {
|
47
|
-
return this.map.entries();
|
48
|
-
}
|
49
|
-
|
50
|
-
toArray(): [VoxelCoord, T][] {
|
51
|
-
const entries = Array.from(this.map.entries());
|
52
|
-
return entries.map(([key, value]) => [keyToCoord(key), value]);
|
53
|
-
}
|
54
|
-
|
55
|
-
values() {
|
56
|
-
return this.map.values();
|
57
|
-
}
|
58
|
-
|
59
|
-
delete(coord: VoxelCoord) {
|
60
|
-
return this.map.delete(coordToKey(coord));
|
61
|
-
}
|
62
|
-
|
63
|
-
has(coord: VoxelCoord): boolean {
|
64
|
-
return this.map.has(coordToKey(coord));
|
65
|
-
}
|
66
|
-
|
67
|
-
clear() {
|
68
|
-
for (const key of this.map.keys()) {
|
69
|
-
this.map.delete(key);
|
70
|
-
}
|
71
|
-
}
|
72
|
-
|
73
|
-
get size(): number {
|
74
|
-
return this.map.size;
|
75
|
-
}
|
76
|
-
}
|
package/src/area.ts
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
import { Area, Coord } from "./types";
|
2
|
-
|
3
|
-
export function areaContains(area: Area, coord: Coord) {
|
4
|
-
return coord.x >= area.x && coord.y >= area.y && coord.x < area.x + area.width && coord.y < area.y + area.height;
|
5
|
-
}
|
6
|
-
|
7
|
-
export function coordsOf(area: Area) {
|
8
|
-
const coords: Coord[] = [];
|
9
|
-
for (let dx = 0; dx < area.width; dx++) {
|
10
|
-
for (let dy = 0; dy < area.height; dy++) {
|
11
|
-
coords.push({ x: area.x + dx, y: area.y + dy });
|
12
|
-
}
|
13
|
-
}
|
14
|
-
return coords;
|
15
|
-
}
|
package/src/arrays.ts
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* TypeScript type guard to assert the type of a non-empty array
|
3
|
-
* @param array Any array to check for non-emptiness
|
4
|
-
* @returns True if the empty is non-empty, else false. TypeScript accepts the array as non-empty after the assertion.
|
5
|
-
*/
|
6
|
-
export function isNotEmpty<T>(array: T[]): array is [T, ...T[]] {
|
7
|
-
if (array.length === 0) return false;
|
8
|
-
return true;
|
9
|
-
}
|
10
|
-
|
11
|
-
/**
|
12
|
-
* Filters undefined values from an array and lets TypeScript know the resulting array
|
13
|
-
* does not have undefined values
|
14
|
-
* @param array Array potentially including undefined values
|
15
|
-
* @returns Array without undefined values
|
16
|
-
*/
|
17
|
-
export function filterNullishValues<T>(array: (T | undefined)[]): T[] {
|
18
|
-
return array.filter((value) => value != null) as T[];
|
19
|
-
}
|
package/src/bytes.spec.ts
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
formatHex,
|
3
|
-
hexStringToUint8Array,
|
4
|
-
Uint8ArrayToHexString,
|
5
|
-
concatUint8Arrays,
|
6
|
-
splitUint8Arrays,
|
7
|
-
Int32ArrayToUint8Array,
|
8
|
-
Uint8ArrayToInt32Array,
|
9
|
-
ethAddressToUint8Array,
|
10
|
-
} from "./bytes";
|
11
|
-
import { random } from "./random";
|
12
|
-
|
13
|
-
const NUM_TESTS = 1000;
|
14
|
-
|
15
|
-
describe("bytes", () => {
|
16
|
-
describe("Uint8ArrayToHexString", () => {
|
17
|
-
it("should be the inverse of hexStringToUint8Array", () => {
|
18
|
-
for (let i = 0; i < NUM_TESTS; i++) {
|
19
|
-
const hex = formatHex(random(Number.MAX_SAFE_INTEGER).toString(16));
|
20
|
-
expect(Uint8ArrayToHexString(hexStringToUint8Array(hex))).toBe(hex);
|
21
|
-
}
|
22
|
-
});
|
23
|
-
});
|
24
|
-
|
25
|
-
describe("hexStringToUint8Array", () => {
|
26
|
-
it("should be the inverse of Uint8ArrayToHexString", () => {
|
27
|
-
for (let i = 0; i < NUM_TESTS; i++) {
|
28
|
-
const uint8arr = hexStringToUint8Array(formatHex(random(Number.MAX_SAFE_INTEGER).toString(16)));
|
29
|
-
expect(hexStringToUint8Array(Uint8ArrayToHexString(uint8arr))).toEqual(uint8arr);
|
30
|
-
}
|
31
|
-
});
|
32
|
-
});
|
33
|
-
|
34
|
-
describe("concatUint8Arrays", () => {
|
35
|
-
it("should concat Uint8Arrays", () => {
|
36
|
-
const arr1 = [123, 456, -134];
|
37
|
-
const arr2 = [763, 99887, 0, 0];
|
38
|
-
const result = [...arr1, ...arr2];
|
39
|
-
expect(concatUint8Arrays(Uint8Array.from(arr1), Uint8Array.from(arr2))).toEqual(Uint8Array.from(result));
|
40
|
-
});
|
41
|
-
});
|
42
|
-
|
43
|
-
describe("splitUint8Arrays", () => {
|
44
|
-
it("should split Uint8Arrays", () => {
|
45
|
-
const arr1 = [123, 456, -134];
|
46
|
-
const arr2 = [763, 99887, 0, 0];
|
47
|
-
const result = [...arr1, ...arr2];
|
48
|
-
expect(splitUint8Arrays(Uint8Array.from(result), [3, 4])).toEqual([Uint8Array.from(arr1), Uint8Array.from(arr2)]);
|
49
|
-
expect(splitUint8Arrays(Uint8Array.from(result), [4, 3])).toEqual([
|
50
|
-
Uint8Array.from([123, 456, -134, 763]),
|
51
|
-
Uint8Array.from([99887, 0, 0]),
|
52
|
-
]);
|
53
|
-
});
|
54
|
-
});
|
55
|
-
|
56
|
-
describe("Uint8ArrayToInt32Array", () => {
|
57
|
-
it("should be the inverse of Int32ArrayToUint8Array", () => {
|
58
|
-
const input = [123, -456, 0, 2 ** 31 - 1, -1 * 2 ** 31];
|
59
|
-
const bytes = Int32ArrayToUint8Array(input);
|
60
|
-
expect(Uint8ArrayToInt32Array(bytes)).toEqual(input);
|
61
|
-
});
|
62
|
-
});
|
63
|
-
|
64
|
-
describe("Int32ArrayToUint8Array", () => {
|
65
|
-
it("should be the inverse of Uint8ArrayToInt32Array", () => {
|
66
|
-
const input = [123, -456, 0, 2 ** 31 - 1, -1 * 2 ** 31];
|
67
|
-
const bytes = Int32ArrayToUint8Array(input);
|
68
|
-
expect(Int32ArrayToUint8Array(Uint8ArrayToInt32Array(bytes))).toEqual(bytes);
|
69
|
-
});
|
70
|
-
});
|
71
|
-
|
72
|
-
describe("ethAddressToUint8Array", () => {
|
73
|
-
it("should produce a 160bit Uint8Array", () => {
|
74
|
-
expect(ethAddressToUint8Array("0x00").length).toBe(20);
|
75
|
-
});
|
76
|
-
});
|
77
|
-
});
|
package/src/bytes.ts
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
import { toEthAddress } from "./eth";
|
2
|
-
import { hexToArray } from "./v2";
|
3
|
-
|
4
|
-
export function formatHex(hex: string): string {
|
5
|
-
if (hex.substring(0, 2) == "0x") hex = hex.substring(2);
|
6
|
-
const prefix = hex.length % 2 !== 0 ? "0x0" : "0x";
|
7
|
-
return prefix + hex;
|
8
|
-
}
|
9
|
-
|
10
|
-
export function hexStringToUint8Array(hexString: string): Uint8Array {
|
11
|
-
return hexToArray(hexString);
|
12
|
-
}
|
13
|
-
|
14
|
-
export function Uint8ArrayToHexString(data: Uint8Array): string {
|
15
|
-
if (data.length === 0) return "0x00";
|
16
|
-
return formatHex(data.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), ""));
|
17
|
-
}
|
18
|
-
|
19
|
-
export function concatUint8Arrays(...arrays: Uint8Array[]): Uint8Array {
|
20
|
-
return Uint8Array.from(
|
21
|
-
arrays.reduce<number[]>((acc, curr) => {
|
22
|
-
return [...acc, ...curr];
|
23
|
-
}, []),
|
24
|
-
);
|
25
|
-
}
|
26
|
-
|
27
|
-
export function splitUint8Arrays(data: Uint8Array, byteLengths: number[]): Uint8Array[] {
|
28
|
-
const arrays: Uint8Array[] = [];
|
29
|
-
let i = 0;
|
30
|
-
for (const length of byteLengths) {
|
31
|
-
const array = new Uint8Array(length);
|
32
|
-
arrays.push(array);
|
33
|
-
for (let j = 0; j < length; j++) {
|
34
|
-
array[j] = data[i];
|
35
|
-
i++;
|
36
|
-
}
|
37
|
-
}
|
38
|
-
return arrays;
|
39
|
-
}
|
40
|
-
|
41
|
-
export function Int32ArrayToUint8Array(input: number[]): Uint8Array {
|
42
|
-
const buffer = new ArrayBuffer(input.length * 4);
|
43
|
-
const int32arr = new Int32Array(buffer);
|
44
|
-
for (let i = 0; i < input.length; i++) {
|
45
|
-
int32arr[i] = input[i];
|
46
|
-
}
|
47
|
-
return new Uint8Array(buffer);
|
48
|
-
}
|
49
|
-
|
50
|
-
export function Uint8ArrayToInt32Array(input: Uint8Array): number[] {
|
51
|
-
return [...new Int32Array(input.buffer)];
|
52
|
-
}
|
53
|
-
|
54
|
-
export function ethAddressToUint8Array(address: string): Uint8Array {
|
55
|
-
return hexStringToUint8Array(toEthAddress(address));
|
56
|
-
}
|
57
|
-
|
58
|
-
// https://stackoverflow.com/a/55330424
|
59
|
-
export function createToInt(size: number) {
|
60
|
-
if (size < 2) {
|
61
|
-
throw new Error("Minimum size is 2");
|
62
|
-
} else if (size > 64) {
|
63
|
-
throw new Error("Maximum size is 64");
|
64
|
-
}
|
65
|
-
|
66
|
-
// Determine value range
|
67
|
-
const maxValue = 2 ** (size - 1) - 1;
|
68
|
-
const minValue = -maxValue - 1;
|
69
|
-
|
70
|
-
return (value: number) => {
|
71
|
-
value = value << 0;
|
72
|
-
if (value > maxValue || value < minValue) {
|
73
|
-
console.log("value", value, maxValue, minValue, value > maxValue, value < minValue);
|
74
|
-
throw new Error(`Int${size} overflow`);
|
75
|
-
}
|
76
|
-
|
77
|
-
if (value < 0) {
|
78
|
-
return 2 ** size + value;
|
79
|
-
} else {
|
80
|
-
return value;
|
81
|
-
}
|
82
|
-
};
|
83
|
-
}
|
84
|
-
|
85
|
-
export const toInt32 = createToInt(32);
|
package/src/console.ts
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
const TOPICS_KEY = "mud-logger-topics";
|
2
|
-
|
3
|
-
export function enableLogger() {
|
4
|
-
const windowConsole = window.console;
|
5
|
-
let filtersActive = Boolean(localStorage.getItem(TOPICS_KEY));
|
6
|
-
|
7
|
-
const topicsString = localStorage.getItem(TOPICS_KEY);
|
8
|
-
let topics: string[] = topicsString ? JSON.parse(topicsString) : [];
|
9
|
-
|
10
|
-
function log(...logs: string[]) {
|
11
|
-
if (filtersActive) return;
|
12
|
-
windowConsole.log(...logs);
|
13
|
-
}
|
14
|
-
|
15
|
-
function logWithTopic(topic: string, ...logs: string[]) {
|
16
|
-
if (!filtersActive || topics.includes(topic)) {
|
17
|
-
windowConsole.log(`--- BETTER CONSOLE / TOPIC ${topic} ---`);
|
18
|
-
windowConsole.log(...logs);
|
19
|
-
}
|
20
|
-
}
|
21
|
-
|
22
|
-
function enableFilters() {
|
23
|
-
localStorage.setItem(TOPICS_KEY, JSON.stringify([]));
|
24
|
-
filtersActive = true;
|
25
|
-
}
|
26
|
-
|
27
|
-
function disableFilters() {
|
28
|
-
localStorage.removeItem(TOPICS_KEY);
|
29
|
-
filtersActive = false;
|
30
|
-
}
|
31
|
-
|
32
|
-
function addTopic(topic: string) {
|
33
|
-
topics.push(topic);
|
34
|
-
localStorage.setItem(TOPICS_KEY, JSON.stringify(topics));
|
35
|
-
}
|
36
|
-
|
37
|
-
function removeTopic(topic: string) {
|
38
|
-
topics = topics.filter((t) => t !== topic);
|
39
|
-
localStorage.setItem(TOPICS_KEY, JSON.stringify(topics));
|
40
|
-
}
|
41
|
-
|
42
|
-
function resetTopics() {
|
43
|
-
topics = [];
|
44
|
-
localStorage.setItem(TOPICS_KEY, JSON.stringify(topics));
|
45
|
-
}
|
46
|
-
|
47
|
-
const logger = {
|
48
|
-
...windowConsole,
|
49
|
-
log,
|
50
|
-
logWithTopic,
|
51
|
-
enableFilters,
|
52
|
-
disableFilters,
|
53
|
-
addTopic,
|
54
|
-
removeTopic,
|
55
|
-
resetTopics,
|
56
|
-
};
|
57
|
-
|
58
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
59
|
-
(window as any).logger = logger;
|
60
|
-
window.console = logger;
|
61
|
-
return logger;
|
62
|
-
}
|
package/src/cubic.spec.ts
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
import { cubicNoiseSample2, cubicNoiseConfig } from "./cubic";
|
2
|
-
|
3
|
-
describe("Cubic", () => {
|
4
|
-
it("computes noise", () => {
|
5
|
-
const coords: [number, number][] = [];
|
6
|
-
for (let i = 0; i < 100; i++) {
|
7
|
-
coords.push([Math.floor((Math.random() - 0.5) * 1000), Math.floor((Math.random() - 0.5) * 1000)]);
|
8
|
-
}
|
9
|
-
|
10
|
-
const noises = coords.map((c) => cubicNoiseSample2(cubicNoiseConfig(1000, 64, 16, 256, 256), c[0], c[1]));
|
11
|
-
console.log("coords", JSON.stringify(coords));
|
12
|
-
console.log("noises", JSON.stringify(noises));
|
13
|
-
});
|
14
|
-
});
|
package/src/cubic.ts
DELETED
@@ -1,109 +0,0 @@
|
|
1
|
-
const RND_A = 134775813;
|
2
|
-
const RND_B = 1103515245;
|
3
|
-
const ACCURACY = 1000;
|
4
|
-
|
5
|
-
export function randomize(seed: number, x: number, y: number) {
|
6
|
-
return (((((x ^ y) * RND_A) ^ (seed + x)) * (((RND_B * x) << 16) ^ (RND_B * y - RND_A))) >>> 0) / 4294967295;
|
7
|
-
}
|
8
|
-
|
9
|
-
export function tile(coordinate: number, period: number) {
|
10
|
-
if (coordinate < 0) while (coordinate < 0) coordinate += period;
|
11
|
-
return coordinate % period;
|
12
|
-
}
|
13
|
-
|
14
|
-
export function interpolate(a: number, b: number, c: number, d: number, x: number, s: number, scale: number) {
|
15
|
-
const p = d - c - (a - b);
|
16
|
-
return (b * Math.pow(s, 3) + x * (c * Math.pow(s, 2) + a * s * (-s + x) + x * (-(b + p) * s + p * x))) * scale;
|
17
|
-
|
18
|
-
// return (x) * ((x ) * ((x ) * p + (a - b - p)) + (c - a)) + b;
|
19
|
-
}
|
20
|
-
|
21
|
-
/**
|
22
|
-
* Config a cubic noise.
|
23
|
-
* @param {Number} seed A seed in the range [0, 1].
|
24
|
-
* @param {Number} [periodX] The number of units after which the x coordinate repeats.
|
25
|
-
* @param {Number} [periodY] The number of units after which the y coordinate repeats.
|
26
|
-
* @returns {Object} A configuration object used by noise functions.
|
27
|
-
*/
|
28
|
-
export function cubicNoiseConfig(
|
29
|
-
seed: number,
|
30
|
-
octave: number,
|
31
|
-
scale: number,
|
32
|
-
periodX = Number.MAX_SAFE_INTEGER,
|
33
|
-
periodY = Number.MAX_SAFE_INTEGER,
|
34
|
-
) {
|
35
|
-
return {
|
36
|
-
seed: Math.floor(seed * Number.MAX_SAFE_INTEGER),
|
37
|
-
periodX: periodX,
|
38
|
-
periodY: periodY,
|
39
|
-
octave,
|
40
|
-
scale,
|
41
|
-
};
|
42
|
-
}
|
43
|
-
|
44
|
-
/**
|
45
|
-
* Sample 1D cubic noise.
|
46
|
-
* @param {Object} config A valid noise configuration.
|
47
|
-
* @param {Number} x The X position to sample at.
|
48
|
-
* @returns {Number} A noise value in the range [0, 1].
|
49
|
-
*/
|
50
|
-
export function cubicNoiseSample1(config: ReturnType<typeof cubicNoiseConfig>, x: number) {
|
51
|
-
const xi = Math.floor(x);
|
52
|
-
const lerp = x - xi;
|
53
|
-
|
54
|
-
return (
|
55
|
-
interpolate(
|
56
|
-
randomize(config.seed, tile(xi - 1, config.periodX), 0),
|
57
|
-
randomize(config.seed, tile(xi, config.periodX), 0),
|
58
|
-
randomize(config.seed, tile(xi + 1, config.periodX), 0),
|
59
|
-
randomize(config.seed, tile(xi + 2, config.periodX), 0),
|
60
|
-
lerp,
|
61
|
-
1,
|
62
|
-
1,
|
63
|
-
) *
|
64
|
-
0.666666 +
|
65
|
-
0.166666
|
66
|
-
);
|
67
|
-
}
|
68
|
-
|
69
|
-
/**
|
70
|
-
* Sample 2D cubic noise.
|
71
|
-
* @param {Object} config A valid noise configuration.
|
72
|
-
* @param {Number} x The X position to sample at.
|
73
|
-
* @param {Number} y The Y position to sample at.
|
74
|
-
* @returns {Number} A noise value in the range [0, 1].
|
75
|
-
*/
|
76
|
-
export function cubicNoiseSample2(
|
77
|
-
{ octave, periodX, periodY, seed, scale }: ReturnType<typeof cubicNoiseConfig>,
|
78
|
-
x: number,
|
79
|
-
y: number,
|
80
|
-
) {
|
81
|
-
const xi = Math.floor(x / octave);
|
82
|
-
const lerpX = Math.floor((x * ACCURACY) / octave) - xi * ACCURACY;
|
83
|
-
const yi = Math.floor(y / octave);
|
84
|
-
const lerpY = Math.floor((y * ACCURACY) / octave) - yi * ACCURACY;
|
85
|
-
const x0 = tile(xi - 1, periodX);
|
86
|
-
const x1 = tile(xi, periodX);
|
87
|
-
const x2 = tile(xi + 1, periodX);
|
88
|
-
const x3 = tile(xi + 2, periodX);
|
89
|
-
|
90
|
-
const xSamples = new Array(4);
|
91
|
-
|
92
|
-
for (let i = 0; i < 4; ++i) {
|
93
|
-
const y = tile(yi - 1 + i, periodY);
|
94
|
-
|
95
|
-
xSamples[i] = interpolate(
|
96
|
-
randomize(seed, x0, y),
|
97
|
-
randomize(seed, x1, y),
|
98
|
-
randomize(seed, x2, y),
|
99
|
-
randomize(seed, x3, y),
|
100
|
-
lerpX,
|
101
|
-
ACCURACY,
|
102
|
-
1,
|
103
|
-
);
|
104
|
-
}
|
105
|
-
|
106
|
-
return Math.floor(
|
107
|
-
interpolate(xSamples[0], xSamples[1], xSamples[2], xSamples[3], lerpY, ACCURACY, scale) / Math.pow(ACCURACY, 6),
|
108
|
-
);
|
109
|
-
}
|
package/src/deferred.ts
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* A convenient way to create a promise with resolve and reject functions.
|
3
|
-
* @returns Tuple with resolve function, reject function and promise.
|
4
|
-
*/
|
5
|
-
export function deferred<T>(): [(t: T) => void, (t: Error) => void, Promise<T>] {
|
6
|
-
let resolve: ((t: T) => void) | null = null;
|
7
|
-
let reject: ((t: Error) => void) | null = null;
|
8
|
-
const promise = new Promise<T>((r, rj) => {
|
9
|
-
resolve = (t: T) => r(t);
|
10
|
-
reject = (e: Error) => rj(e);
|
11
|
-
});
|
12
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
13
|
-
return [resolve as any, reject as any, promise];
|
14
|
-
}
|
package/src/distance.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Compute the Euclidean distance between two points
|
3
|
-
* https://en.wikipedia.org/wiki/Euclidean_distance
|
4
|
-
* @param a
|
5
|
-
* @param b
|
6
|
-
* @returns Euclidian distance between a and b
|
7
|
-
*/
|
8
|
-
export function euclidean(a: number[], b: number[]): number {
|
9
|
-
if (a.length !== b.length) throw new Error("points must have same dimension");
|
10
|
-
return Math.sqrt(a.reduce((acc, _, i) => acc + Math.pow(a[i] - b[i], 2), 0));
|
11
|
-
}
|
package/src/enums.ts
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @param enm Numeric enum
|
3
|
-
* @returns Number array containing the enum values
|
4
|
-
*/
|
5
|
-
export function numValues(enm: object): number[] {
|
6
|
-
const nums: number[] = [];
|
7
|
-
for (const val of Object.values(enm)) {
|
8
|
-
if (!isNaN(Number(val))) {
|
9
|
-
nums.push(Number(val));
|
10
|
-
}
|
11
|
-
}
|
12
|
-
return nums;
|
13
|
-
}
|