@latticexyz/utils 0.10.0 → 0.12.0
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/CHANGELOG.md +14 -0
- package/dist/CoordMap.d.ts +26 -0
- package/dist/area.d.ts +3 -0
- package/{src/arrays.ts → dist/arrays.d.ts} +2 -8
- package/dist/console.d.ts +52 -0
- package/dist/cubic.d.ts +32 -0
- package/dist/deferred.d.ts +5 -0
- package/{src/distance.ts → dist/distance.d.ts} +1 -3
- package/dist/enums.d.ts +5 -0
- package/dist/eth.d.ts +22 -0
- package/dist/guards.d.ts +3 -0
- package/dist/hash.d.ts +6 -0
- package/{src/index.ts → dist/index.d.ts} +1 -13
- package/dist/index.js +802 -0
- package/dist/index.js.map +1 -0
- package/dist/iterable.d.ts +10 -0
- package/{src/math.ts → dist/math.d.ts} +1 -4
- package/dist/mobx.d.ts +6 -0
- package/dist/objects.d.ts +11 -0
- package/dist/pack.d.ts +16 -0
- package/dist/promise.d.ts +4 -0
- package/dist/proxy.d.ts +8 -0
- package/{src/random.ts → dist/random.d.ts} +2 -7
- package/dist/rx.d.ts +30 -0
- package/dist/sleep.d.ts +1 -0
- package/dist/types.d.ts +40 -0
- package/dist/uuid.d.ts +19 -0
- package/dist/worker.d.ts +6 -0
- package/package.json +9 -5
- package/jest.config.js +0 -5
- package/rollup.config.js +0 -16
- package/src/CoordMap.spec.ts +0 -57
- package/src/CoordMap.ts +0 -106
- package/src/area.ts +0 -15
- 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/enums.ts +0 -13
- package/src/eth.ts +0 -42
- package/src/guards.ts +0 -10
- package/src/hash.ts +0 -11
- package/src/iterable.spec.ts +0 -56
- package/src/iterable.ts +0 -59
- 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 -45
- package/src/proxy.spec.ts +0 -186
- package/src/proxy.ts +0 -101
- 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/worker.ts +0 -16
- package/tsconfig.json +0 -102
- package/typedoc.json +0 -9
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/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
|
-
}
|
package/src/eth.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pads start of a hex string with 0 to create a bit string of the given length
|
|
3
|
-
* @param input Hex string
|
|
4
|
-
* @param bits Number of bits in the output hex string
|
|
5
|
-
* @returns Hex string of specified length
|
|
6
|
-
*/
|
|
7
|
-
export function padToBitLength(input: string, bits: number) {
|
|
8
|
-
// Cut off 0x prefix
|
|
9
|
-
if (input.substring(0, 2) == "0x") input = input.substring(2);
|
|
10
|
-
// Pad start with 0 to get desired bit length
|
|
11
|
-
const length = bits / 4;
|
|
12
|
-
input = input.padStart(length, "0");
|
|
13
|
-
input = input.substring(input.length - length);
|
|
14
|
-
// Prefix with 0x
|
|
15
|
-
return `0x${input}`;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Pads start of a hex string with 0 to create a 160 bit hex string
|
|
20
|
-
* which can be used as an Ethereum address
|
|
21
|
-
* @param input Hex string
|
|
22
|
-
* @returns 160 bit hex string
|
|
23
|
-
*/
|
|
24
|
-
export function toEthAddress(input: string) {
|
|
25
|
-
return padToBitLength(input, 160);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Pads start of a hex string with 0 to create a 256bit hex string
|
|
30
|
-
* which can be used as an Ethereum address
|
|
31
|
-
* @param input Hex string
|
|
32
|
-
* @returns 256 bit hex string
|
|
33
|
-
*/
|
|
34
|
-
export function to256BitString(input: string) {
|
|
35
|
-
return padToBitLength(input, 256);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function extractEncodedArguments(input: string) {
|
|
39
|
-
// Cutting off the first 4 bytes, which represent the function selector
|
|
40
|
-
if (input[0] !== "0" && input[1] !== "x") throw new Error("Invalid hex string");
|
|
41
|
-
return "0x" + input.substring(10);
|
|
42
|
-
}
|
package/src/guards.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { Func } from "./types";
|
|
3
|
-
|
|
4
|
-
export function isObject(c: unknown): c is Record<string, any> {
|
|
5
|
-
return typeof c === "object" && !Array.isArray(c) && c !== null;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function isFunction(c: unknown): c is Func<any, any> {
|
|
9
|
-
return c instanceof Function;
|
|
10
|
-
}
|
package/src/hash.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { BigNumber } from "ethers";
|
|
2
|
-
import { keccak256 as keccak256Bytes, toUtf8Bytes } from "ethers/lib/utils";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Compute keccak256 hash from given string and remove padding from the resulting hex string
|
|
6
|
-
* @param data String to be hashed
|
|
7
|
-
* @returns Hash of the given string as hex string without padding
|
|
8
|
-
*/
|
|
9
|
-
export function keccak256(data: string) {
|
|
10
|
-
return BigNumber.from(keccak256Bytes(toUtf8Bytes(data))).toHexString();
|
|
11
|
-
}
|
package/src/iterable.spec.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { arrayToIterator, mergeIterators } from "./iterable";
|
|
2
|
-
|
|
3
|
-
describe("arrayToIterator", () => {
|
|
4
|
-
it("should return an iterable iterator with the same content as the array", () => {
|
|
5
|
-
const array = ["a", "b", "c", 1, 2, 3];
|
|
6
|
-
const iterator = arrayToIterator(array);
|
|
7
|
-
expect([...iterator]).toEqual(array);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it("should not return a next value if the array is empty", () => {
|
|
11
|
-
const array: string[] = [];
|
|
12
|
-
const iterator = arrayToIterator(array);
|
|
13
|
-
expect([...iterator]).toEqual(array);
|
|
14
|
-
|
|
15
|
-
const mock = jest.fn();
|
|
16
|
-
for (const item of iterator) {
|
|
17
|
-
mock(item);
|
|
18
|
-
}
|
|
19
|
-
expect(mock).not.toHaveBeenCalled();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("should not be possible to iterate over an iterator multiple times", () => {
|
|
23
|
-
const array = ["a", "b", "c", 1, 2, 3];
|
|
24
|
-
const iterator = arrayToIterator(array);
|
|
25
|
-
expect([...iterator]).toEqual(array);
|
|
26
|
-
expect([...iterator]).toEqual([]);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe("mergeIterators", () => {
|
|
31
|
-
it("should return a merged iterator", () => {
|
|
32
|
-
const a = arrayToIterator(["a", "b", "c"]);
|
|
33
|
-
const b = arrayToIterator([1, 2, 3]);
|
|
34
|
-
expect([...mergeIterators(a, b)]).toEqual([
|
|
35
|
-
["a", 1],
|
|
36
|
-
["b", 2],
|
|
37
|
-
["c", 3],
|
|
38
|
-
]);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("should work with iterators of unequal length", () => {
|
|
42
|
-
const a = arrayToIterator(["a"]);
|
|
43
|
-
const b = arrayToIterator([1, 2, 3]);
|
|
44
|
-
expect([...mergeIterators(a, b)]).toEqual([
|
|
45
|
-
["a", 1],
|
|
46
|
-
[null, 2],
|
|
47
|
-
[null, 3],
|
|
48
|
-
]);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("should return an empty iterator if both inputs are empty", () => {
|
|
52
|
-
const a = arrayToIterator([]);
|
|
53
|
-
const b = arrayToIterator([]);
|
|
54
|
-
expect([...mergeIterators(a, b)]).toEqual([]);
|
|
55
|
-
});
|
|
56
|
-
});
|
package/src/iterable.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
export function makeIterable<T>(iterator: Iterator<T>): IterableIterator<T> {
|
|
2
|
-
const iterable: IterableIterator<T> = {
|
|
3
|
-
...iterator,
|
|
4
|
-
[Symbol.iterator]() {
|
|
5
|
-
return this;
|
|
6
|
-
},
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
return iterable;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function concatIterators<T>(first: Iterator<T>, second?: Iterator<T>): IterableIterator<T> {
|
|
13
|
-
if (!second) return makeIterable(first);
|
|
14
|
-
return makeIterable({
|
|
15
|
-
next() {
|
|
16
|
-
const next = first.next();
|
|
17
|
-
if (!next.done) return next;
|
|
18
|
-
return second.next();
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function mergeIterators<A, B>(iteratorA: Iterator<A>, iteratorB: Iterator<B>): IterableIterator<[A, B]> {
|
|
24
|
-
const iterator: Iterator<[A, B]> = {
|
|
25
|
-
next() {
|
|
26
|
-
const nextA = iteratorA.next();
|
|
27
|
-
const nextB = iteratorB.next();
|
|
28
|
-
if (nextA.done && nextB.done) return { done: true, value: null };
|
|
29
|
-
return { value: [nextA.value, nextB.value] };
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
return makeIterable(iterator);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function transformIterator<A, B>(iterator: Iterator<A>, transform: (value: A) => B): IterableIterator<B> {
|
|
36
|
-
return makeIterable({
|
|
37
|
-
next() {
|
|
38
|
-
const { done, value } = iterator.next();
|
|
39
|
-
return { done, value: done ? value : transform(value) };
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Turns an array into an iterator. NOTE: an iterator can only be iterated once.
|
|
46
|
-
* @param array Array to be turned into an iterator
|
|
47
|
-
* @returns Iterator to iterate through the array
|
|
48
|
-
*/
|
|
49
|
-
export function arrayToIterator<T>(array: T[]): IterableIterator<T> {
|
|
50
|
-
let i = 0;
|
|
51
|
-
const iterator: Iterator<T> = {
|
|
52
|
-
next() {
|
|
53
|
-
const done = i >= array.length;
|
|
54
|
-
if (done) return { done, value: null };
|
|
55
|
-
return { value: array[i++] };
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
return makeIterable(iterator);
|
|
59
|
-
}
|
package/src/mobx.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { IComputedValue, IObservableValue, reaction } from "mobx";
|
|
2
|
-
import { deferred } from "./deferred";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @param comp Computed/Observable value that is either defined or undefined
|
|
6
|
-
* @returns promise that resolves with the first truthy computed value
|
|
7
|
-
*/
|
|
8
|
-
export async function awaitValue<T>(comp: IComputedValue<T | undefined> | IObservableValue<T | undefined>): Promise<T> {
|
|
9
|
-
const [resolve, , promise] = deferred<T>();
|
|
10
|
-
|
|
11
|
-
const dispose = reaction(
|
|
12
|
-
() => comp.get(),
|
|
13
|
-
(value) => {
|
|
14
|
-
if (value) {
|
|
15
|
-
resolve(value);
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
{ fireImmediately: true }
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
const value = await promise;
|
|
22
|
-
// Dispose the reaction once the promise is resolved
|
|
23
|
-
dispose();
|
|
24
|
-
|
|
25
|
-
return value;
|
|
26
|
-
}
|
package/src/objects.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utility function to map a source object to an object with the same keys but mapped values
|
|
3
|
-
* @param source Source object to be mapped
|
|
4
|
-
* @param valueMap Mapping values of the source object to values of the target object
|
|
5
|
-
* @returns An object with the same keys as the source object but mapped values
|
|
6
|
-
*/
|
|
7
|
-
export function mapObject<S extends { [key: string]: unknown }, T extends { [key in keyof S]: unknown }>(
|
|
8
|
-
source: S,
|
|
9
|
-
valueMap: (value: S[keyof S], key: keyof S) => T[keyof S]
|
|
10
|
-
): T {
|
|
11
|
-
const target: Partial<{ [key in keyof typeof source]: T[keyof S] }> = {};
|
|
12
|
-
for (const key in source) {
|
|
13
|
-
target[key] = valueMap(source[key], key);
|
|
14
|
-
}
|
|
15
|
-
return target as T;
|
|
16
|
-
}
|
package/src/pack.spec.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { pack, unpack } from "./pack";
|
|
2
|
-
|
|
3
|
-
describe("pack", () => {
|
|
4
|
-
it("should pack multiple numbers into one 32 bit integer", () => {
|
|
5
|
-
const packed = pack([1, 2], [8, 24]);
|
|
6
|
-
expect(packed).toBe(parseInt("1000000000000000000000010", 2));
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it("should be the inverse of unpack", () => {
|
|
10
|
-
const bits = [8, 24];
|
|
11
|
-
const numbers = [16777218, 2072396467, -1];
|
|
12
|
-
|
|
13
|
-
for (const nums of numbers) {
|
|
14
|
-
expect(pack(unpack(nums, bits), bits)).toEqual(nums);
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
describe("unpack", () => {
|
|
20
|
-
it("should unpack a packed 32 bit integer into multiple numbers", () => {
|
|
21
|
-
const unpacked = unpack(parseInt("1000000000000000000000010", 2), [8, 24]);
|
|
22
|
-
expect(unpacked).toEqual([1, 2]);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("should be the inverse of pack", () => {
|
|
26
|
-
const bits = [8, 24];
|
|
27
|
-
const numbers = [
|
|
28
|
-
[1, 2],
|
|
29
|
-
[123, 8798899],
|
|
30
|
-
[2 ** 8 - 1, 2 ** 24 - 1],
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
for (const nums of numbers) {
|
|
34
|
-
expect(unpack(pack(nums, bits), bits)).toEqual(nums);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
});
|
package/src/pack.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
function rightMask(input: number, keep: number): number {
|
|
2
|
-
return input & (2 ** keep - 1);
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Packs two unsigned integers in one 32 bit unsigned integer
|
|
7
|
-
* @param numbers Unsigned integers to be packed in 32 bit integer
|
|
8
|
-
* @param bitsPerNumber Bits for each number
|
|
9
|
-
* @returns Packed 32 bit unsigned integer
|
|
10
|
-
*/
|
|
11
|
-
export function pack(numbers: number[], bitsPerNumber: number[]): number {
|
|
12
|
-
// Total number of bits must be 32
|
|
13
|
-
if (bitsPerNumber.reduce((acc, curr) => acc + curr, 0) > 32) {
|
|
14
|
-
throw new Error("JS pretends integers are 32 bit when bitshifts are involved");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Array lengths must match
|
|
18
|
-
if (numbers.length !== bitsPerNumber.length) throw new Error("Arrays' lengths must match");
|
|
19
|
-
|
|
20
|
-
// Numbers must fit in number of bits and must be unsigned
|
|
21
|
-
for (let i = 0; i < numbers.length; i++) {
|
|
22
|
-
if (numbers[i] < 0) {
|
|
23
|
-
throw new Error("Underflow: can only pack unsigned integer");
|
|
24
|
-
}
|
|
25
|
-
if (numbers[i] > 2 ** bitsPerNumber[i] - 1) {
|
|
26
|
-
const error = `Overflow: ${numbers[i]} does not fit in ${bitsPerNumber[i]} bits`;
|
|
27
|
-
throw new Error(error);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Pack number
|
|
32
|
-
let packed = 0;
|
|
33
|
-
for (let i = 0; i < numbers.length; i++) {
|
|
34
|
-
packed = (packed << bitsPerNumber[i]) | numbers[i];
|
|
35
|
-
}
|
|
36
|
-
return packed;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Unpacks a packed 32 bit unsigned integer into the original unsigned integers
|
|
41
|
-
* @param packed Packed 32 bit unsigned integer
|
|
42
|
-
* @param bitsPerNumber Bits for each unsigned integer
|
|
43
|
-
* @returns Array of unpacked unsignd integers
|
|
44
|
-
*/
|
|
45
|
-
export function unpack(packed: number, bitsPerNumber: number[]): number[] {
|
|
46
|
-
const numbers: number[] = [];
|
|
47
|
-
let shiftedPacked = packed;
|
|
48
|
-
for (let i = bitsPerNumber.length - 1; i >= 0; i--) {
|
|
49
|
-
numbers.unshift(rightMask(shiftedPacked, bitsPerNumber[i]));
|
|
50
|
-
shiftedPacked = shiftedPacked >>> bitsPerNumber[i];
|
|
51
|
-
}
|
|
52
|
-
return numbers;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function packTuple(numbers: [number, number]): number {
|
|
56
|
-
return pack(numbers, [8, 24]);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function unpackTuple(packed: number): [number, number] {
|
|
60
|
-
return unpack(packed, [8, 24]) as [number, number];
|
|
61
|
-
}
|
package/src/promise.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { deferred } from "./deferred";
|
|
3
|
-
import { sleep } from "./sleep";
|
|
4
|
-
|
|
5
|
-
export const range = function* (total = 0, step = 1, from = 0) {
|
|
6
|
-
// eslint-disable-next-line no-empty
|
|
7
|
-
for (let i = 0; i < total; yield from + i++ * step) {}
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export async function rejectAfter<T>(ms: number, msg: string): Promise<T> {
|
|
11
|
-
await sleep(ms);
|
|
12
|
-
throw new Error(msg);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const timeoutAfter = async <T>(promise: Promise<T>, ms: number, timeoutMsg: string) => {
|
|
16
|
-
return Promise.race([promise, rejectAfter<T>(ms, timeoutMsg)]);
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export const callWithRetry = <T>(
|
|
20
|
-
fn: (...args: any[]) => Promise<T>,
|
|
21
|
-
args: any[] = [],
|
|
22
|
-
maxRetries = 10,
|
|
23
|
-
retryInterval = 1000
|
|
24
|
-
): Promise<T> => {
|
|
25
|
-
const [resolve, reject, promise] = deferred<T>();
|
|
26
|
-
const process = async () => {
|
|
27
|
-
let res: T;
|
|
28
|
-
for (let i = 0; i < maxRetries; i++) {
|
|
29
|
-
try {
|
|
30
|
-
res = await fn(...args);
|
|
31
|
-
resolve(res);
|
|
32
|
-
break;
|
|
33
|
-
} catch (e) {
|
|
34
|
-
if (i < maxRetries - 1) {
|
|
35
|
-
console.log("going to sleep", i);
|
|
36
|
-
await sleep(Math.min(retryInterval * 2 ** i + Math.random() * 100, 15000));
|
|
37
|
-
} else {
|
|
38
|
-
reject(e as unknown as Error);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
process();
|
|
44
|
-
return promise;
|
|
45
|
-
};
|