@terrygonguet/utils 0.3.0 → 0.4.1
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/dist/index.d.ts +2 -1
- package/dist/index.js +14 -1
- package/dist/random.d.ts +23 -0
- package/dist/random.js +86 -0
- package/package.json +5 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,10 +7,11 @@ declare function noop(): void;
|
|
|
7
7
|
declare function exhaustive(_: never): never;
|
|
8
8
|
declare function hash(message: string): Promise<ArrayBuffer>;
|
|
9
9
|
declare function range(start: number, end: number, step?: number): Generator<number, void, unknown>;
|
|
10
|
+
declare function yesno(value?: string): boolean;
|
|
10
11
|
type ExecResult<T, Err = Error> = [null, T] | [Err, null];
|
|
11
12
|
declare function safe<F extends (...args: any) => any>(f: F, ...args: Parameters<F>): ReturnType<F> extends Promise<any> ? Promise<ExecResult<Awaited<ReturnType<F>>>> : ExecResult<ReturnType<F>>;
|
|
12
13
|
declare function safe<Err, F extends (...args: any) => any>(f: F, ...args: Parameters<F>): ReturnType<F> extends Promise<any> ? Promise<ExecResult<Awaited<ReturnType<F>>, Err>> : ExecResult<ReturnType<F>, Err>;
|
|
13
14
|
declare const exec: typeof safe;
|
|
14
15
|
declare const run: typeof safe;
|
|
15
16
|
|
|
16
|
-
export { type ExecResult, clamp, createNoopProxy, exec, exhaustive, hash, noop, range, run, safe };
|
|
17
|
+
export { type ExecResult, clamp, createNoopProxy, exec, exhaustive, hash, noop, range, run, safe, yesno };
|
package/dist/index.js
CHANGED
|
@@ -35,6 +35,18 @@ function* range(start, end, step = 1) {
|
|
|
35
35
|
yield i;
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
+
function yesno(value) {
|
|
39
|
+
switch (value?.toLowerCase()) {
|
|
40
|
+
case "1":
|
|
41
|
+
case "y":
|
|
42
|
+
case "yes":
|
|
43
|
+
case "true":
|
|
44
|
+
case "on":
|
|
45
|
+
return true;
|
|
46
|
+
default:
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
38
50
|
function safe(f, ...args) {
|
|
39
51
|
try {
|
|
40
52
|
const promiseOrResult = f(...args);
|
|
@@ -59,5 +71,6 @@ export {
|
|
|
59
71
|
noop,
|
|
60
72
|
range,
|
|
61
73
|
run,
|
|
62
|
-
safe
|
|
74
|
+
safe,
|
|
75
|
+
yesno
|
|
63
76
|
};
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random integer between `0` (inclusive) and `max` (exlusive)
|
|
3
|
+
*/
|
|
4
|
+
declare function randi(max: number): number;
|
|
5
|
+
/**
|
|
6
|
+
* Generates a random integer between `min` (inclusive) and `max` (exlusive)
|
|
7
|
+
*/
|
|
8
|
+
declare function randi(min: number, max: number): number;
|
|
9
|
+
/**
|
|
10
|
+
* Generates a random real between `0` (inclusive) and `max` (exlusive)
|
|
11
|
+
*/
|
|
12
|
+
declare function randf(max: number): number;
|
|
13
|
+
/**
|
|
14
|
+
* Generates a random real between `min` (inclusive) and `max` (exlusive)
|
|
15
|
+
*/
|
|
16
|
+
declare function randf(min: number, max: number): number;
|
|
17
|
+
declare const shuffle: <T>(arr: T[]) => T[];
|
|
18
|
+
declare function make_randi(prng: typeof Math.random): typeof randi;
|
|
19
|
+
declare function make_randf(prng: typeof Math.random): (minOrMax: number, max?: number) => number;
|
|
20
|
+
declare function make_shuffle(prng: typeof Math.random): <T>(arr: T[]) => T[];
|
|
21
|
+
declare function seeded(seed: string | number): typeof Math.random;
|
|
22
|
+
|
|
23
|
+
export { make_randf, make_randi, make_shuffle, randf, randi, seeded, shuffle };
|
package/dist/random.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// src/random.ts
|
|
2
|
+
var base_randi = make_randi(Math.random);
|
|
3
|
+
function randi(minOrMax, max) {
|
|
4
|
+
if (typeof max == "number") return base_randi(minOrMax, max);
|
|
5
|
+
else return base_randi(minOrMax);
|
|
6
|
+
}
|
|
7
|
+
var base_randf = make_randf(Math.random);
|
|
8
|
+
function randf(minOrMax, max) {
|
|
9
|
+
if (typeof max == "number") return base_randf(minOrMax, max);
|
|
10
|
+
else return base_randf(minOrMax);
|
|
11
|
+
}
|
|
12
|
+
var shuffle = make_shuffle(Math.random);
|
|
13
|
+
function make_randi(prng) {
|
|
14
|
+
return function randi2(minOrMax, max) {
|
|
15
|
+
if (typeof max == "number") {
|
|
16
|
+
const min = minOrMax;
|
|
17
|
+
const delta = max - min;
|
|
18
|
+
return min + Math.floor(prng() * delta);
|
|
19
|
+
} else return Math.floor(prng() * minOrMax);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function make_randf(prng) {
|
|
23
|
+
return function randf2(minOrMax, max) {
|
|
24
|
+
if (typeof max == "number") {
|
|
25
|
+
const min = minOrMax;
|
|
26
|
+
const delta = max - min;
|
|
27
|
+
return min + prng() * delta;
|
|
28
|
+
} else return prng() * minOrMax;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function make_shuffle(prng) {
|
|
32
|
+
const randi2 = make_randi(prng);
|
|
33
|
+
return function shuffle2(arr) {
|
|
34
|
+
const clone = Array.from(arr);
|
|
35
|
+
for (let i = 0; i < clone.length - 2; i++) {
|
|
36
|
+
const j = randi2(i, clone.length);
|
|
37
|
+
const temp = clone[i];
|
|
38
|
+
clone[i] = clone[j];
|
|
39
|
+
clone[j] = temp;
|
|
40
|
+
}
|
|
41
|
+
return clone;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function seeded(seed) {
|
|
45
|
+
if (typeof seed == "string") seed = cyrb128(seed);
|
|
46
|
+
else seed ^= 3735928559;
|
|
47
|
+
const prng = splitmix32(seed);
|
|
48
|
+
for (let i = 0; i < 15; i++) prng();
|
|
49
|
+
return prng;
|
|
50
|
+
}
|
|
51
|
+
function cyrb128(str) {
|
|
52
|
+
let h1 = 1779033703, h2 = 3144134277, h3 = 1013904242, h4 = 2773480762;
|
|
53
|
+
for (let i = 0, k; i < str.length; i++) {
|
|
54
|
+
k = str.charCodeAt(i);
|
|
55
|
+
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
|
|
56
|
+
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
|
|
57
|
+
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
|
|
58
|
+
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
|
|
59
|
+
}
|
|
60
|
+
h1 = Math.imul(h3 ^ h1 >>> 18, 597399067);
|
|
61
|
+
h2 = Math.imul(h4 ^ h2 >>> 22, 2869860233);
|
|
62
|
+
h3 = Math.imul(h1 ^ h3 >>> 17, 951274213);
|
|
63
|
+
h4 = Math.imul(h2 ^ h4 >>> 19, 2716044179);
|
|
64
|
+
h1 ^= h2 ^ h3 ^ h4;
|
|
65
|
+
return h1 >>> 0;
|
|
66
|
+
}
|
|
67
|
+
function splitmix32(a) {
|
|
68
|
+
return function() {
|
|
69
|
+
a |= 0;
|
|
70
|
+
a = a + 2654435769 | 0;
|
|
71
|
+
let t = a ^ a >>> 16;
|
|
72
|
+
t = Math.imul(t, 569420461);
|
|
73
|
+
t = t ^ t >>> 15;
|
|
74
|
+
t = Math.imul(t, 1935289751);
|
|
75
|
+
return ((t = t ^ t >>> 15) >>> 0) / 4294967296;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export {
|
|
79
|
+
make_randf,
|
|
80
|
+
make_randi,
|
|
81
|
+
make_shuffle,
|
|
82
|
+
randf,
|
|
83
|
+
randi,
|
|
84
|
+
seeded,
|
|
85
|
+
shuffle
|
|
86
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrygonguet/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
"types": "./dist/json.d.ts",
|
|
25
25
|
"import": "./dist/json.js"
|
|
26
26
|
},
|
|
27
|
+
"./random": {
|
|
28
|
+
"types": "./dist/random.d.ts",
|
|
29
|
+
"import": "./dist/random.js"
|
|
30
|
+
},
|
|
27
31
|
"./tableau": {
|
|
28
32
|
"types": "./dist/tableau.d.ts",
|
|
29
33
|
"import": "./dist/tableau.js"
|