@terrygonguet/utils 0.4.0 → 0.5.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/dist/async.d.ts +13 -3
- package/dist/async.js +46 -18
- package/dist/random.d.ts +23 -0
- package/dist/random.js +86 -0
- package/package.json +10 -6
package/dist/async.d.ts
CHANGED
|
@@ -5,11 +5,21 @@ interface RetryOptions {
|
|
|
5
5
|
}
|
|
6
6
|
declare function retry(options: RetryOptions): <T>(provider: () => Promise<T>) => Promise<T>;
|
|
7
7
|
declare function retry<T>(provider: () => Promise<T>, options?: RetryOptions): Promise<T>;
|
|
8
|
-
interface AsyncMapOptions {
|
|
8
|
+
interface AsyncMapOptions<FailFast extends boolean = false, WithSourceIndexes extends boolean = false> {
|
|
9
9
|
concurrent?: number;
|
|
10
|
+
failFast?: FailFast;
|
|
11
|
+
withSourceIndexes?: WithSourceIndexes;
|
|
12
|
+
}
|
|
13
|
+
interface AsyncMapResultWithIndexes<U> {
|
|
14
|
+
results: [i: number, value: U][];
|
|
15
|
+
errors: [i: number, error: unknown][];
|
|
16
|
+
}
|
|
17
|
+
interface AsyncMapResult<U> {
|
|
18
|
+
results: U[];
|
|
19
|
+
errors: unknown[];
|
|
10
20
|
}
|
|
11
|
-
declare function asyncMap<T, U>(f: AsyncMapFn<T, U>, options?: AsyncMapOptions): (data: T[]) => Promise<U[]>;
|
|
12
|
-
declare function asyncMap<T, U>(data: T[], f: AsyncMapFn<T, U>, options?: AsyncMapOptions): Promise<U[]>;
|
|
13
21
|
type AsyncMapFn<T, U> = (el: T, i: number, data: T[]) => Promise<U>;
|
|
22
|
+
declare function asyncMap<T, U, FailFast extends boolean = false, WithSourceIndexes extends boolean = false>(f: AsyncMapFn<T, U>, options?: AsyncMapOptions<FailFast, WithSourceIndexes>): (data: T[]) => Promise<FailFast extends true ? WithSourceIndexes extends true ? [i: number, value: U][] : U[] : WithSourceIndexes extends true ? AsyncMapResultWithIndexes<U> : AsyncMapResult<U>>;
|
|
23
|
+
declare function asyncMap<T, U, FailFast extends boolean = false, WithSourceIndexes extends boolean = false>(data: T[], f: AsyncMapFn<T, U>, options?: AsyncMapOptions<FailFast, WithSourceIndexes>): Promise<FailFast extends true ? WithSourceIndexes extends true ? [i: number, value: U][] : U[] : WithSourceIndexes extends true ? AsyncMapResultWithIndexes<U> : AsyncMapResult<U>>;
|
|
14
24
|
|
|
15
25
|
export { asyncMap, pause, retry };
|
package/dist/async.js
CHANGED
|
@@ -40,34 +40,62 @@ function asyncMap(dataOrF, ForOptions, options) {
|
|
|
40
40
|
return asyncMap_(data, f, options);
|
|
41
41
|
} else throw new Error("Invalid arguments passed to asyncMap");
|
|
42
42
|
}
|
|
43
|
-
async function asyncMap_(data, f, {
|
|
43
|
+
async function asyncMap_(data, f, {
|
|
44
|
+
concurrent = 5,
|
|
45
|
+
failFast = false,
|
|
46
|
+
withSourceIndexes = false
|
|
47
|
+
} = {}) {
|
|
44
48
|
return new Promise((resolve, reject) => {
|
|
45
49
|
const resolved = [];
|
|
46
|
-
const
|
|
50
|
+
const errors = [];
|
|
51
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
47
52
|
let next = 0;
|
|
53
|
+
let stop = false;
|
|
48
54
|
function queue(i) {
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const el = data[i];
|
|
53
|
-
inFlight.push([
|
|
55
|
+
if (!stop && i > 0 && inFlight.size == 0) return finish();
|
|
56
|
+
if (stop || i >= data.length) return;
|
|
57
|
+
inFlight.set(
|
|
54
58
|
i,
|
|
55
|
-
f(
|
|
56
|
-
resolved.push([i, result])
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
f(data[i], i, data).then(
|
|
60
|
+
(result) => void resolved.push([i, result]),
|
|
61
|
+
(error) => {
|
|
62
|
+
if (!failFast) errors.push([i, error]);
|
|
63
|
+
else {
|
|
64
|
+
stop = true;
|
|
65
|
+
reject(error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
).finally(() => {
|
|
69
|
+
inFlight.delete(i);
|
|
59
70
|
queue(next);
|
|
60
|
-
}
|
|
61
|
-
|
|
71
|
+
})
|
|
72
|
+
);
|
|
62
73
|
next++;
|
|
63
74
|
}
|
|
64
75
|
function finish() {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
76
|
+
resolved.sort(([a], [b]) => a - b);
|
|
77
|
+
if (failFast) {
|
|
78
|
+
if (withSourceIndexes) resolve(resolved);
|
|
79
|
+
else resolve(resolved.map(([, value]) => value));
|
|
80
|
+
} else {
|
|
81
|
+
errors.sort(([a], [b]) => a - b);
|
|
82
|
+
if (withSourceIndexes)
|
|
83
|
+
resolve({
|
|
84
|
+
results: resolved,
|
|
85
|
+
errors
|
|
86
|
+
});
|
|
87
|
+
else
|
|
88
|
+
resolve({
|
|
89
|
+
results: resolved.map(([, value]) => value),
|
|
90
|
+
errors: errors.map(([, error]) => error)
|
|
91
|
+
});
|
|
92
|
+
}
|
|
70
93
|
}
|
|
94
|
+
if (data.length == 0) finish();
|
|
95
|
+
else
|
|
96
|
+
for (let i = 0; i < concurrent; i++) {
|
|
97
|
+
queue(next);
|
|
98
|
+
}
|
|
71
99
|
});
|
|
72
100
|
}
|
|
73
101
|
export {
|
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.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
@@ -24,17 +24,21 @@
|
|
|
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"
|
|
30
34
|
}
|
|
31
35
|
},
|
|
32
36
|
"devDependencies": {
|
|
33
|
-
"prettier": "^
|
|
34
|
-
"tsup": "^8.
|
|
35
|
-
"typescript": "^5.
|
|
36
|
-
"vite": "^
|
|
37
|
-
"vitest": "^
|
|
37
|
+
"prettier": "^3.5.2",
|
|
38
|
+
"tsup": "^8.3.6",
|
|
39
|
+
"typescript": "^5.7.3",
|
|
40
|
+
"vite": "^6.1.1",
|
|
41
|
+
"vitest": "^3.0.6"
|
|
38
42
|
},
|
|
39
43
|
"author": {
|
|
40
44
|
"email": "terry@gonguet.com",
|