@tktb-tess/util-fns 0.8.3 → 0.8.4
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/bundle.js +223 -168
- package/dist/random.d.ts +1 -1
- package/dist/u8arr-ext.d.ts +8 -1
- package/package.json +1 -1
package/dist/bundle.js
CHANGED
|
@@ -10,27 +10,27 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
|
|
|
10
10
|
return !1;
|
|
11
11
|
if (Array.isArray(n) && Array.isArray(t)) {
|
|
12
12
|
if (n.length !== t.length) return !1;
|
|
13
|
-
for (let
|
|
14
|
-
if (!d(n[
|
|
13
|
+
for (let o = 0; o < n.length; o++)
|
|
14
|
+
if (!d(n[o], t[o])) return !1;
|
|
15
15
|
return !0;
|
|
16
16
|
}
|
|
17
17
|
if (n instanceof Set && t instanceof Set) {
|
|
18
|
-
const
|
|
19
|
-
return !!d(
|
|
18
|
+
const o = [...n.values()], s = [...t.values()];
|
|
19
|
+
return !!d(o, s);
|
|
20
20
|
}
|
|
21
21
|
if (n instanceof Map && t instanceof Map) {
|
|
22
|
-
const
|
|
23
|
-
if (!d(
|
|
22
|
+
const o = [...n.keys()], s = [...t.keys()];
|
|
23
|
+
if (!d(o, s)) return !1;
|
|
24
24
|
const i = [...n.values()], c = [...t.values()];
|
|
25
25
|
return !!d(i, c);
|
|
26
26
|
}
|
|
27
27
|
if (e === "[object Object]") {
|
|
28
|
-
const
|
|
29
|
-
for (const
|
|
30
|
-
const
|
|
31
|
-
if (
|
|
32
|
-
const [h,
|
|
33
|
-
if (!d(h,
|
|
28
|
+
const o = n, s = t, i = Object.keys(o), c = Object.keys(s);
|
|
29
|
+
for (const a of i) {
|
|
30
|
+
const f = c.find((g) => g === a);
|
|
31
|
+
if (f === void 0) return !1;
|
|
32
|
+
const [h, u] = [o[a], s[f]];
|
|
33
|
+
if (!d(h, u)) return !1;
|
|
34
34
|
}
|
|
35
35
|
return !0;
|
|
36
36
|
}
|
|
@@ -40,8 +40,8 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
|
|
|
40
40
|
}, z = () => {
|
|
41
41
|
let n, t;
|
|
42
42
|
return {
|
|
43
|
-
promise: new Promise((r,
|
|
44
|
-
n = r, t =
|
|
43
|
+
promise: new Promise((r, o) => {
|
|
44
|
+
n = r, t = o;
|
|
45
45
|
}),
|
|
46
46
|
resolve: n,
|
|
47
47
|
reject: t
|
|
@@ -50,11 +50,11 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
|
|
|
50
50
|
setTimeout(() => t(), n);
|
|
51
51
|
}), W = (n) => (...t) => () => n(...t), G = (n) => {
|
|
52
52
|
const t = [];
|
|
53
|
-
let e = [], r = "",
|
|
54
|
-
for (let
|
|
55
|
-
const i = n[
|
|
56
|
-
i === '"' && (
|
|
57
|
-
` && !
|
|
53
|
+
let e = [], r = "", o = !1;
|
|
54
|
+
for (let s = 0; s < n.length; s++) {
|
|
55
|
+
const i = n[s];
|
|
56
|
+
i === '"' && (s === 0 || n[s - 1] !== "\\") ? o = !o : i === "," && !o ? (e.push(r.trim()), r = "") : i === `
|
|
57
|
+
` && !o ? (e.push(r.trim()), t.push(e), e = [], r = "") : r += i;
|
|
58
58
|
}
|
|
59
59
|
return e.push(r.trim()), t.push(e), t;
|
|
60
60
|
}, H = async (n, t) => {
|
|
@@ -73,66 +73,66 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
|
|
|
73
73
|
}, tt = async (n, t) => {
|
|
74
74
|
const r = new Blob([n]).stream().pipeThrough(new DecompressionStream(t));
|
|
75
75
|
return new Response(r).bytes();
|
|
76
|
-
}, nt = (n, t) => Math.floor(Math.random() * (t - n) + n),
|
|
76
|
+
}, nt = (n, t) => Math.floor(Math.random() * (t - n) + n), m = (n, t) => {
|
|
77
77
|
t < 0n && (t *= -1n);
|
|
78
78
|
const e = n % t;
|
|
79
79
|
return e < 0n ? e + t : e;
|
|
80
|
-
},
|
|
80
|
+
}, B = (n, t = !1) => {
|
|
81
81
|
if (!Number.isFinite(n)) throw Error("`length` is not a valid number");
|
|
82
82
|
if (n <= 0) throw Error("`length` must be positive");
|
|
83
83
|
const e = Math.ceil(n / 8), r = crypto.getRandomValues(new Uint8Array(e));
|
|
84
|
-
let
|
|
85
|
-
return t && (
|
|
84
|
+
let o = Array.from(r, (s) => s.toString(2).padStart(8, "0")).join("").slice(0, n);
|
|
85
|
+
return t && (o = o.replace(/^\d/, "1")), BigInt("0b" + o);
|
|
86
86
|
}, j = (n, t) => {
|
|
87
87
|
if (n >= t) throw Error("rangeError");
|
|
88
|
-
const e = t - n, r = e.toString(2).length,
|
|
88
|
+
const e = t - n, r = e.toString(2).length, o = (() => {
|
|
89
89
|
for (let i = 0; i < 1e5; i++) {
|
|
90
|
-
const c =
|
|
91
|
-
if (c >=
|
|
90
|
+
const c = B(r);
|
|
91
|
+
if (c >= p(2n, BigInt(r), e))
|
|
92
92
|
return c % e;
|
|
93
93
|
}
|
|
94
94
|
throw Error("Failed to generate a random bigint");
|
|
95
95
|
})();
|
|
96
|
-
return n +
|
|
97
|
-
},
|
|
96
|
+
return n + o;
|
|
97
|
+
}, p = (n, t, e) => {
|
|
98
98
|
if (e < 1n) throw Error("`mod` must be positive");
|
|
99
99
|
if (t < 0n) throw Error("`power` must not be negative");
|
|
100
|
-
if (n =
|
|
100
|
+
if (n = m(n, e), e === 1n) return 0n;
|
|
101
101
|
if (n % e === 1n || n % e === 0n) return n;
|
|
102
102
|
if (n === e - 1n) return t & 1n ? e - 1n : 1n;
|
|
103
103
|
let r = 1n;
|
|
104
104
|
for (; t > 0n; )
|
|
105
105
|
t & 1n && (r = r * n % e), n = n * n % e, t >>= 1n;
|
|
106
106
|
return r;
|
|
107
|
-
},
|
|
107
|
+
}, L = (n, t) => {
|
|
108
108
|
if (n === 0n && t === 0n) return { x: 0n, y: 0n, gcd: 0n };
|
|
109
109
|
if (n === 0n)
|
|
110
110
|
return t > 0n ? { x: 0n, y: -1n, gcd: t } : { x: 0n, y: 1n, gcd: -t };
|
|
111
111
|
if (t === 0n)
|
|
112
112
|
return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
|
|
113
|
-
let [e, r,
|
|
113
|
+
let [e, r, o] = [1n, 0n, n], [s, i, c] = [0n, -1n, t];
|
|
114
114
|
for (; ; ) {
|
|
115
|
-
const
|
|
116
|
-
if (
|
|
117
|
-
[e,
|
|
115
|
+
const a = o / c, f = o - a * c;
|
|
116
|
+
if (f === 0n) break;
|
|
117
|
+
[e, s] = [s, e - a * s], [r, i] = [i, r - a * i], [o, c] = [c, f];
|
|
118
118
|
}
|
|
119
|
-
return c < 0n && (
|
|
119
|
+
return c < 0n && (s *= -1n, i *= -1n, c *= -1n), { x: s, y: i, gcd: c };
|
|
120
120
|
}, w = (n, t) => {
|
|
121
121
|
if (n >= t) return 1n;
|
|
122
122
|
const e = BigInt((t - 2n).toString(2).length), r = (t - n) / 2n;
|
|
123
123
|
if (e * r < 63n) {
|
|
124
124
|
let c = n;
|
|
125
|
-
for (let
|
|
126
|
-
c *=
|
|
125
|
+
for (let a = n + 2n; a < t; a += 2n)
|
|
126
|
+
c *= a;
|
|
127
127
|
return c;
|
|
128
128
|
}
|
|
129
|
-
const
|
|
130
|
-
return
|
|
131
|
-
},
|
|
129
|
+
const o = n + r | 1n, s = w(n, o), i = w(o, t);
|
|
130
|
+
return s * i;
|
|
131
|
+
}, _ = (n) => {
|
|
132
132
|
let t = 3n, e = 1n, r = 1n;
|
|
133
|
-
const
|
|
134
|
-
for (let
|
|
135
|
-
const i = (n >>
|
|
133
|
+
const o = BigInt(n.toString(2).length) - 1n;
|
|
134
|
+
for (let s = o - 1n; s > -1n; --s) {
|
|
135
|
+
const i = (n >> s) + 1n | 1n;
|
|
136
136
|
r *= w(t, i), t = i, e *= r;
|
|
137
137
|
}
|
|
138
138
|
return e;
|
|
@@ -140,8 +140,8 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
|
|
|
140
140
|
if (n < 0n) throw Error("'n' must be non-negative");
|
|
141
141
|
if (n === 0n) return 1n;
|
|
142
142
|
const t = n - BigInt(n.toString(2).match(/1/g)?.length ?? 0);
|
|
143
|
-
return
|
|
144
|
-
}, rt = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)),
|
|
143
|
+
return _(n) << t;
|
|
144
|
+
}, rt = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), v = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, b = (n, t) => {
|
|
145
145
|
if (t < 1n || t % 2n === 0n)
|
|
146
146
|
throw Error("`n` is invalid");
|
|
147
147
|
for (; n < 0n; )
|
|
@@ -157,7 +157,7 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
|
|
|
157
157
|
[n, t] = [t, n], n % 4n === 3n && t % 4n === 3n && (e *= -1n), n %= t;
|
|
158
158
|
}
|
|
159
159
|
return t === 1n ? e : 0n;
|
|
160
|
-
},
|
|
160
|
+
}, C = (n) => {
|
|
161
161
|
if (n < 0n) return !1;
|
|
162
162
|
if (n === 0n) return !0;
|
|
163
163
|
let t = 1n, e = n;
|
|
@@ -166,49 +166,49 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
|
|
|
166
166
|
r ** 2n < n ? t = r : e = r;
|
|
167
167
|
}
|
|
168
168
|
return n === t ** 2n || n === (t + 1n) ** 2n;
|
|
169
|
-
},
|
|
169
|
+
}, F = (n) => {
|
|
170
170
|
if (n <= 1n) return !1;
|
|
171
171
|
if (n % 2n === 0n) return n === 2n;
|
|
172
172
|
let t = n - 1n, e = 0n;
|
|
173
173
|
for (; t % 2n === 0n; )
|
|
174
174
|
t >>= 1n, e += 1n;
|
|
175
|
-
const [r,
|
|
176
|
-
let i =
|
|
175
|
+
const [r, o] = [t, e];
|
|
176
|
+
let i = p(2n, r, n);
|
|
177
177
|
if (i === 1n) return !0;
|
|
178
|
-
for (let c = 0n; c <
|
|
178
|
+
for (let c = 0n; c < o; c++) {
|
|
179
179
|
if (i === n - 1n) return !0;
|
|
180
180
|
i = i * i % n;
|
|
181
181
|
}
|
|
182
182
|
return !1;
|
|
183
|
-
},
|
|
183
|
+
}, P = (n) => {
|
|
184
184
|
let t = 5n, e = b(t, n);
|
|
185
185
|
for (; e > 0n; ) {
|
|
186
|
-
if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n &&
|
|
186
|
+
if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && C(n))
|
|
187
187
|
return [0n, 0n];
|
|
188
188
|
e = b(t, n);
|
|
189
189
|
}
|
|
190
190
|
return [t, e];
|
|
191
|
-
}, y = (n, t) => (n & 1n) === 1n ?
|
|
192
|
-
let
|
|
191
|
+
}, y = (n, t) => (n & 1n) === 1n ? m(n + t >> 1n, t) : m(n >> 1n, t), V = (n, t, e, r) => {
|
|
192
|
+
let o = 1n, s = e;
|
|
193
193
|
const i = n.toString(2).slice(1);
|
|
194
194
|
for (const c of i)
|
|
195
|
-
[
|
|
196
|
-
return [
|
|
195
|
+
[o, s] = [m(o * s, t), y(s * s + r * o * o, t)], c === "1" && ([o, s] = [y(e * o + s, t), y(r * o + e * s, t)]);
|
|
196
|
+
return [o, s];
|
|
197
197
|
}, O = (n, t, e, r) => {
|
|
198
198
|
if (n % 2n !== 1n) throw Error("`n` must be odd");
|
|
199
|
-
let
|
|
200
|
-
for (;
|
|
201
|
-
|
|
202
|
-
const [i, c] = V(
|
|
203
|
-
let
|
|
199
|
+
let o = n + 1n, s = 0n;
|
|
200
|
+
for (; o % 2n === 0n; )
|
|
201
|
+
o >>= 1n, s += 1n;
|
|
202
|
+
const [i, c] = V(o, n, e, t);
|
|
203
|
+
let a = c;
|
|
204
204
|
if (i === 0n) return !0;
|
|
205
|
-
r =
|
|
206
|
-
for (let
|
|
207
|
-
if (
|
|
208
|
-
|
|
205
|
+
r = p(r, o, n);
|
|
206
|
+
for (let f = 0n; f < s; f++) {
|
|
207
|
+
if (a === 0n) return !0;
|
|
208
|
+
a = m(a * a - 2n * r, n), r = p(r, 2n, n);
|
|
209
209
|
}
|
|
210
210
|
return !1;
|
|
211
|
-
},
|
|
211
|
+
}, E = (n) => {
|
|
212
212
|
if (n <= 1n) return !1;
|
|
213
213
|
if (n % 2n === 0n) return n === 2n;
|
|
214
214
|
const t = [
|
|
@@ -239,29 +239,29 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
|
|
|
239
239
|
97n,
|
|
240
240
|
101n
|
|
241
241
|
];
|
|
242
|
-
for (const
|
|
243
|
-
if (n %
|
|
244
|
-
return n ===
|
|
245
|
-
if (!
|
|
242
|
+
for (const s of t)
|
|
243
|
+
if (n % s === 0n)
|
|
244
|
+
return n === s;
|
|
245
|
+
if (!F(n))
|
|
246
246
|
return !1;
|
|
247
|
-
const [e, r] =
|
|
247
|
+
const [e, r] = P(n);
|
|
248
248
|
if (r === 0n) return !1;
|
|
249
|
-
const
|
|
250
|
-
return O(n, e, 1n,
|
|
249
|
+
const o = (1n - e) / 4n;
|
|
250
|
+
return O(n, e, 1n, o);
|
|
251
251
|
}, st = (n, t) => {
|
|
252
252
|
if (t < 2n)
|
|
253
253
|
throw Error("noPrimesFound");
|
|
254
254
|
for (let r = 0; r < 1e5; r++) {
|
|
255
|
-
const
|
|
256
|
-
if (
|
|
255
|
+
const o = j(n, t);
|
|
256
|
+
if (E(o)) return o;
|
|
257
257
|
}
|
|
258
258
|
throw Error("noPrimesFound");
|
|
259
259
|
}, ot = (n, t = !1) => {
|
|
260
260
|
if (n < 2)
|
|
261
261
|
throw Error("noPrimesFound");
|
|
262
262
|
for (let r = 0; r < 1e5; r++) {
|
|
263
|
-
const
|
|
264
|
-
if (
|
|
263
|
+
const o = B(n, t);
|
|
264
|
+
if (E(o)) return o;
|
|
265
265
|
}
|
|
266
266
|
throw Error("noPrimesFound");
|
|
267
267
|
};
|
|
@@ -290,24 +290,24 @@ class l {
|
|
|
290
290
|
return new l(t > 0 ? 1n : -1n, 0n);
|
|
291
291
|
const r = t < 0;
|
|
292
292
|
r && (t *= -1);
|
|
293
|
-
const
|
|
294
|
-
if (
|
|
295
|
-
return new l(r ? -
|
|
296
|
-
t = 1 /
|
|
297
|
-
let [i, c] = [1n,
|
|
298
|
-
for (; `${
|
|
299
|
-
const h = BigInt(Math.floor(t)),
|
|
300
|
-
if ([i, c] = [c, h * c + i], [
|
|
301
|
-
return new l(r ? -c : c,
|
|
302
|
-
t = 1 /
|
|
293
|
+
const o = BigInt(Math.floor(t)), s = t - Number(o);
|
|
294
|
+
if (s === 0)
|
|
295
|
+
return new l(r ? -o : o, 1n);
|
|
296
|
+
t = 1 / s;
|
|
297
|
+
let [i, c] = [1n, o], [a, f] = [0n, 1n];
|
|
298
|
+
for (; `${f}`.length < e + 1; ) {
|
|
299
|
+
const h = BigInt(Math.floor(t)), u = t - Number(h);
|
|
300
|
+
if ([i, c] = [c, h * c + i], [a, f] = [f, h * f + a], u === 0)
|
|
301
|
+
return new l(r ? -c : c, f);
|
|
302
|
+
t = 1 / u;
|
|
303
303
|
}
|
|
304
|
-
return new l(r ? -i : i,
|
|
304
|
+
return new l(r ? -i : i, a);
|
|
305
305
|
}
|
|
306
306
|
/**
|
|
307
307
|
* reduction
|
|
308
308
|
*/
|
|
309
309
|
#e() {
|
|
310
|
-
const { gcd: t } =
|
|
310
|
+
const { gcd: t } = L(this.#t, this.#n);
|
|
311
311
|
t !== 0n && (this.#t /= t, this.#n /= t);
|
|
312
312
|
}
|
|
313
313
|
/**
|
|
@@ -409,16 +409,16 @@ class l {
|
|
|
409
409
|
const r = /^\s*(\d+)\s*\/\s*(\d+)\s*$/.exec(t);
|
|
410
410
|
if (!r)
|
|
411
411
|
throw Error("cannot parse");
|
|
412
|
-
const
|
|
413
|
-
if (!
|
|
412
|
+
const o = r.at(1), s = r.at(2);
|
|
413
|
+
if (!o || !s)
|
|
414
414
|
throw Error("cannot parse");
|
|
415
|
-
return new l(BigInt(
|
|
415
|
+
return new l(BigInt(o), BigInt(s));
|
|
416
416
|
}
|
|
417
417
|
}
|
|
418
|
-
const
|
|
418
|
+
const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
|
|
419
419
|
switch (t) {
|
|
420
420
|
case "utf-8":
|
|
421
|
-
return
|
|
421
|
+
return $.encode(n);
|
|
422
422
|
case "base64":
|
|
423
423
|
return Uint8Array.from(atob(n), (e) => e.charCodeAt(0));
|
|
424
424
|
case "base64url": {
|
|
@@ -426,37 +426,89 @@ const k = new TextEncoder(), $ = new TextDecoder(), it = (n, t) => {
|
|
|
426
426
|
return e > 0 && (n = n + "=".repeat(4 - e)), n = n.replaceAll("-", "+").replaceAll("_", "/"), Uint8Array.from(atob(n), (r) => r.charCodeAt(0));
|
|
427
427
|
}
|
|
428
428
|
case "hex": {
|
|
429
|
-
|
|
430
|
-
|
|
429
|
+
n.length & 1 && (n = "0" + n);
|
|
430
|
+
const r = n.matchAll(/.{2}/g);
|
|
431
|
+
return Uint8Array.from(r, (o) => Number.parseInt(o[0], 16));
|
|
432
|
+
}
|
|
433
|
+
case "oct": {
|
|
434
|
+
const e = n.length & 7;
|
|
435
|
+
e && (n = "0".repeat(8 - e) + n);
|
|
436
|
+
const r = n.matchAll(/.{8}/g), s = Array.from(r, (i) => {
|
|
437
|
+
const c = i[0];
|
|
438
|
+
return Number.parseInt(c, 8).toString(16).padStart(6, "0");
|
|
439
|
+
}).join("").matchAll(/.{2}/g);
|
|
440
|
+
return Uint8Array.from(
|
|
441
|
+
s,
|
|
442
|
+
(i) => Number.parseInt(i[0], 16)
|
|
443
|
+
);
|
|
431
444
|
}
|
|
432
|
-
case "
|
|
433
|
-
const e = n.
|
|
434
|
-
|
|
445
|
+
case "bin": {
|
|
446
|
+
const e = n.length & 7;
|
|
447
|
+
e && (n = "0".repeat(8 - e) + n);
|
|
448
|
+
const r = n.matchAll(/.{8}/g), o = Array.from(r, (s) => Number.parseInt(s[0], 2));
|
|
449
|
+
return Uint8Array.from(o);
|
|
435
450
|
}
|
|
451
|
+
default:
|
|
452
|
+
throw Error(`Invalid encoding: ${t}`, { cause: t });
|
|
436
453
|
}
|
|
437
454
|
}, ct = (n, t) => {
|
|
438
455
|
switch (t) {
|
|
439
456
|
case "utf-8":
|
|
440
|
-
return
|
|
457
|
+
return k.decode(n);
|
|
441
458
|
case "base64": {
|
|
442
459
|
const e = Array.from(n, (r) => String.fromCharCode(r)).join("");
|
|
443
460
|
return btoa(e);
|
|
444
461
|
}
|
|
445
462
|
case "base64url": {
|
|
446
|
-
const e = Array.from(n, (
|
|
463
|
+
const e = Array.from(n, (o) => String.fromCharCode(o)).join("");
|
|
447
464
|
return btoa(e).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
448
465
|
}
|
|
449
466
|
case "hex":
|
|
450
|
-
return Array.from(n, (
|
|
451
|
-
case "
|
|
452
|
-
|
|
453
|
-
|
|
467
|
+
return Array.from(n, (r) => r.toString(16).padStart(2, "0")).join("").replace(/^0+/, "");
|
|
468
|
+
case "oct": {
|
|
469
|
+
let e = Array.from(
|
|
470
|
+
n,
|
|
471
|
+
(i) => i.toString(16).padStart(2, "0")
|
|
472
|
+
).join("");
|
|
473
|
+
const r = e.length % 6;
|
|
474
|
+
r && (e = "0".repeat(6 - r) + e);
|
|
475
|
+
const o = e.matchAll(/.{6}/g);
|
|
476
|
+
return Array.from(
|
|
477
|
+
o,
|
|
478
|
+
(i) => Number.parseInt(i[0], 16).toString(8).padStart(8, "0")
|
|
479
|
+
).join("").replace(/^0+/, "");
|
|
480
|
+
}
|
|
481
|
+
case "bin":
|
|
482
|
+
return Array.from(n, (r) => r.toString(2).padStart(8, "0")).join("").replace(/^0+/, "");
|
|
483
|
+
default:
|
|
484
|
+
throw Error(`Invalid encoding: ${t}`, { cause: t });
|
|
485
|
+
}
|
|
486
|
+
}, at = (n) => {
|
|
487
|
+
const t = n < 0n;
|
|
488
|
+
t && (n *= -1n);
|
|
489
|
+
const e = n.toString(2).length, r = Math.ceil(e / 7);
|
|
490
|
+
t && (n = BigInt.asUintN(7 * r, ~n + 1n));
|
|
491
|
+
const o = new Uint8Array(r);
|
|
492
|
+
for (let s = 0; s < r - 1; ++s) {
|
|
493
|
+
const i = Number(n & 127n) | 128;
|
|
494
|
+
o[s] = i, n >>= 7n;
|
|
495
|
+
}
|
|
496
|
+
return o[r - 1] = Number(n), o;
|
|
497
|
+
}, ft = (n) => {
|
|
498
|
+
const t = n.length;
|
|
499
|
+
let e = 0n;
|
|
500
|
+
for (let s = 0; s < t; ++s) {
|
|
501
|
+
const i = n[s] & 127;
|
|
502
|
+
e += BigInt(i) << BigInt(7 * s);
|
|
503
|
+
}
|
|
504
|
+
const r = 1n << BigInt(7 * t - 1);
|
|
505
|
+
return (e & r) === 1n && (e = BigInt.asIntN(7 * t, e)), e;
|
|
454
506
|
};
|
|
455
|
-
class
|
|
507
|
+
class N {
|
|
456
508
|
#t;
|
|
457
509
|
#n;
|
|
458
510
|
static name = "Queue";
|
|
459
|
-
[Symbol.toStringTag] =
|
|
511
|
+
[Symbol.toStringTag] = N.name;
|
|
460
512
|
/**
|
|
461
513
|
* Queue
|
|
462
514
|
* @param data
|
|
@@ -503,13 +555,13 @@ class A extends Error {
|
|
|
503
555
|
}
|
|
504
556
|
toJSON() {
|
|
505
557
|
const t = (() => {
|
|
506
|
-
const
|
|
507
|
-
return typeof
|
|
508
|
-
})(), { name: e, message: r, stack:
|
|
558
|
+
const s = this.cause;
|
|
559
|
+
return typeof s == "string" || typeof s == "number" || typeof s == "boolean" ? s : typeof s == "bigint" ? s.toString() : typeof s == "object" && s !== null ? s instanceof Set || s instanceof Map ? Object.fromEntries(s) : s : void 0;
|
|
560
|
+
})(), { name: e, message: r, stack: o } = this;
|
|
509
561
|
return {
|
|
510
562
|
name: e,
|
|
511
563
|
message: r,
|
|
512
|
-
stack:
|
|
564
|
+
stack: o,
|
|
513
565
|
cause: t
|
|
514
566
|
};
|
|
515
567
|
}
|
|
@@ -527,43 +579,44 @@ const I = (n) => {
|
|
|
527
579
|
++t, n >>= 1n;
|
|
528
580
|
return BigInt.asUintN(64, t);
|
|
529
581
|
};
|
|
530
|
-
class
|
|
582
|
+
class x {
|
|
531
583
|
#t;
|
|
584
|
+
#n;
|
|
532
585
|
static name = "FloatRand";
|
|
533
|
-
[Symbol.toStringTag] =
|
|
534
|
-
constructor(t) {
|
|
535
|
-
this.#t = t;
|
|
586
|
+
[Symbol.toStringTag] = x.name;
|
|
587
|
+
constructor(t, e) {
|
|
588
|
+
this.#t = t, this.#n = e;
|
|
536
589
|
}
|
|
537
|
-
#
|
|
538
|
-
const r = this.#t.getU32Rand() | 0,
|
|
539
|
-
let
|
|
540
|
-
if (
|
|
541
|
-
|
|
590
|
+
#e() {
|
|
591
|
+
const r = this.#t.getU32Rand() | 0, o = r & 255;
|
|
592
|
+
let s = 126;
|
|
593
|
+
if (o === 0) {
|
|
594
|
+
s -= 8;
|
|
542
595
|
const h = 1e5;
|
|
543
|
-
let
|
|
596
|
+
let u = 0;
|
|
544
597
|
t: for (; ; ) {
|
|
545
|
-
if (
|
|
598
|
+
if (u > h)
|
|
546
599
|
throw Error("loop exceeded limit");
|
|
547
600
|
const g = this.#t.getU32Rand() | 0;
|
|
548
601
|
if (g === 0) {
|
|
549
|
-
if (
|
|
550
|
-
|
|
602
|
+
if (s -= 32, s < 0) {
|
|
603
|
+
s = 0;
|
|
551
604
|
break t;
|
|
552
605
|
}
|
|
553
606
|
} else {
|
|
554
|
-
|
|
607
|
+
s -= I(g);
|
|
555
608
|
break t;
|
|
556
609
|
}
|
|
557
|
-
++
|
|
610
|
+
++u;
|
|
558
611
|
}
|
|
559
612
|
} else
|
|
560
|
-
|
|
613
|
+
s -= I(o);
|
|
561
614
|
const i = r >>> 8 & 8388607;
|
|
562
|
-
i === 0 && r >>> 31 && ++
|
|
563
|
-
const { buffer: c, byteOffset:
|
|
564
|
-
|
|
615
|
+
i === 0 && r >>> 31 && ++s;
|
|
616
|
+
const { buffer: c, byteOffset: a, length: f } = Uint32Array.from([
|
|
617
|
+
s << 23 | i
|
|
565
618
|
]);
|
|
566
|
-
return new Float32Array(c,
|
|
619
|
+
return new Float32Array(c, a, f)[0];
|
|
567
620
|
}
|
|
568
621
|
/**
|
|
569
622
|
* returns a random single-precision floating-point number (float32) in the range of [0.0, 1.0)
|
|
@@ -571,48 +624,48 @@ class U {
|
|
|
571
624
|
*/
|
|
572
625
|
getF32Rand() {
|
|
573
626
|
for (let e = 0; e < 1e5; ++e) {
|
|
574
|
-
const r = this.#
|
|
627
|
+
const r = this.#e();
|
|
575
628
|
if (r < 1)
|
|
576
629
|
return r;
|
|
577
630
|
}
|
|
578
631
|
throw Error("exceeded loop limit");
|
|
579
632
|
}
|
|
580
|
-
#
|
|
633
|
+
#r() {
|
|
581
634
|
const t = 0n, e = 1023n, r = (() => {
|
|
582
|
-
const h = BigInt(this.#t.getU32Rand() | 0),
|
|
583
|
-
return h << 32n |
|
|
584
|
-
})(),
|
|
585
|
-
let
|
|
586
|
-
if (
|
|
587
|
-
|
|
635
|
+
const h = BigInt(this.#t.getU32Rand() | 0), u = BigInt(this.#n.getU32Rand() | 0);
|
|
636
|
+
return h << 32n | u;
|
|
637
|
+
})(), o = r & 0x7ffn;
|
|
638
|
+
let s = e - 1n;
|
|
639
|
+
if (o === 0n) {
|
|
640
|
+
s -= 11n;
|
|
588
641
|
const h = 1e5;
|
|
589
|
-
let
|
|
642
|
+
let u = 0;
|
|
590
643
|
t: for (; ; ) {
|
|
591
|
-
if (
|
|
644
|
+
if (u > h)
|
|
592
645
|
throw Error("loop exceeded limit");
|
|
593
646
|
const g = (() => {
|
|
594
|
-
const
|
|
595
|
-
return
|
|
647
|
+
const M = BigInt(this.#t.getU32Rand() | 0), R = BigInt(this.#n.getU32Rand() | 0);
|
|
648
|
+
return M << 32n | R;
|
|
596
649
|
})();
|
|
597
650
|
if (g === 0n) {
|
|
598
|
-
if (
|
|
599
|
-
|
|
651
|
+
if (s -= 64n, s < t) {
|
|
652
|
+
s = t;
|
|
600
653
|
break t;
|
|
601
654
|
}
|
|
602
655
|
} else {
|
|
603
|
-
|
|
656
|
+
s -= S(g);
|
|
604
657
|
break t;
|
|
605
658
|
}
|
|
606
|
-
++
|
|
659
|
+
++u;
|
|
607
660
|
}
|
|
608
661
|
} else
|
|
609
|
-
|
|
662
|
+
s -= S(o);
|
|
610
663
|
const i = r >> 11n & 0xfffffffffffffn;
|
|
611
|
-
i === 0n && r >> 63n && ++
|
|
612
|
-
const { buffer: c, byteOffset:
|
|
613
|
-
|
|
664
|
+
i === 0n && r >> 63n && ++s;
|
|
665
|
+
const { buffer: c, byteOffset: a, length: f } = BigUint64Array.from([
|
|
666
|
+
s << 52n | i
|
|
614
667
|
]);
|
|
615
|
-
return new Float64Array(c,
|
|
668
|
+
return new Float64Array(c, a, f)[0];
|
|
616
669
|
}
|
|
617
670
|
/**
|
|
618
671
|
* returns a random double-precision floating-point number (float64) in the range of [0.0, 1.0)
|
|
@@ -620,7 +673,7 @@ class U {
|
|
|
620
673
|
*/
|
|
621
674
|
getF64Rand() {
|
|
622
675
|
for (let e = 0; e < 1e5; ++e) {
|
|
623
|
-
const r = this.#
|
|
676
|
+
const r = this.#r();
|
|
624
677
|
if (r < 1)
|
|
625
678
|
return r;
|
|
626
679
|
}
|
|
@@ -628,13 +681,13 @@ class U {
|
|
|
628
681
|
}
|
|
629
682
|
}
|
|
630
683
|
const D = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], q = 0x5851f42d4c957f2dn;
|
|
631
|
-
class
|
|
684
|
+
class U {
|
|
632
685
|
/**
|
|
633
686
|
* length = 2, `[state, increment]`
|
|
634
687
|
*/
|
|
635
688
|
#t;
|
|
636
689
|
static name = "PCGMinimal";
|
|
637
|
-
[Symbol.toStringTag] =
|
|
690
|
+
[Symbol.toStringTag] = U.name;
|
|
638
691
|
/**
|
|
639
692
|
* returns seed for constructing `PCGMinimal` instance
|
|
640
693
|
* @returns random seed `BigUint64Array` with length 2
|
|
@@ -666,7 +719,7 @@ class N {
|
|
|
666
719
|
/** 32bit 乱数を返す (内部状態は変わらない) */
|
|
667
720
|
get #e() {
|
|
668
721
|
const t = this.#t[0], e = Number(t >> 59n), r = Number(BigInt.asUintN(32, (t ^ t >> 18n) >> 27n));
|
|
669
|
-
return
|
|
722
|
+
return v(r, e);
|
|
670
723
|
}
|
|
671
724
|
/**
|
|
672
725
|
*
|
|
@@ -682,8 +735,8 @@ class N {
|
|
|
682
735
|
if (t > 4294967296) throw Error("`bound` exceeded limit (2^32)");
|
|
683
736
|
const r = 4294967296 % t;
|
|
684
737
|
for (; ; ) {
|
|
685
|
-
const
|
|
686
|
-
if (
|
|
738
|
+
const o = this.getU32Rand();
|
|
739
|
+
if (o >= r) return o % t;
|
|
687
740
|
}
|
|
688
741
|
}
|
|
689
742
|
/**
|
|
@@ -700,35 +753,37 @@ class N {
|
|
|
700
753
|
}
|
|
701
754
|
}
|
|
702
755
|
export {
|
|
703
|
-
|
|
756
|
+
x as FloatRand,
|
|
704
757
|
A as NamedError,
|
|
705
|
-
|
|
706
|
-
|
|
758
|
+
U as PCGMinimal,
|
|
759
|
+
N as Queue,
|
|
707
760
|
l as Rational,
|
|
708
|
-
|
|
761
|
+
E as bailliePSW,
|
|
709
762
|
Q as compress,
|
|
763
|
+
ft as decodeLEB128,
|
|
710
764
|
Y as decodeRFC3986URIComponent,
|
|
711
765
|
tt as decompress,
|
|
766
|
+
at as encodeLEB128,
|
|
712
767
|
X as encodeRFC3986URIComponent,
|
|
713
|
-
|
|
768
|
+
L as exEuclidean,
|
|
714
769
|
et as factorial,
|
|
715
770
|
it as fromString,
|
|
716
771
|
H as getHash,
|
|
717
|
-
|
|
772
|
+
B as getRandBIByBitLength,
|
|
718
773
|
j as getRandBIByRange,
|
|
719
774
|
ot as getRandPrimeByBitLength,
|
|
720
775
|
st as getRandPrimeByRange,
|
|
721
776
|
nt as getRndInt,
|
|
722
777
|
d as isDeepStrictEqual,
|
|
723
778
|
Z as isNode,
|
|
724
|
-
|
|
779
|
+
C as isSquare,
|
|
725
780
|
b as jacobiSymbol,
|
|
726
781
|
W as lazify,
|
|
727
|
-
|
|
782
|
+
p as modPow,
|
|
728
783
|
G as parseCSV,
|
|
729
784
|
z as promiseWithResolvers,
|
|
730
|
-
|
|
731
|
-
|
|
785
|
+
m as residue,
|
|
786
|
+
v as rot32,
|
|
732
787
|
rt as rot32BI,
|
|
733
788
|
K as sameValueZero,
|
|
734
789
|
J as sleep,
|
package/dist/random.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare class FloatRand {
|
|
|
7
7
|
#private;
|
|
8
8
|
static readonly name = "FloatRand";
|
|
9
9
|
readonly [Symbol.toStringTag] = "FloatRand";
|
|
10
|
-
constructor(rng: RandomGenerator);
|
|
10
|
+
constructor(rng: RandomGenerator, rng2: RandomGenerator);
|
|
11
11
|
/**
|
|
12
12
|
* returns a random single-precision floating-point number (float32) in the range of [0.0, 1.0)
|
|
13
13
|
* @returns
|
package/dist/u8arr-ext.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
-
export type Encoding = 'utf-8' | 'base64' | 'base64url' | 'hex' | '
|
|
1
|
+
export type Encoding = 'utf-8' | 'base64' | 'base64url' | 'hex' | 'oct' | 'bin';
|
|
2
2
|
export declare const fromString: (str: string, encoding: Encoding) => Uint8Array<ArrayBuffer>;
|
|
3
3
|
export declare const toString: (u8Arr: Uint8Array, encoding: Encoding) => string;
|
|
4
|
+
/**
|
|
5
|
+
* encode a bigint as a signed LEB128 format
|
|
6
|
+
* @param n
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare const encodeLEB128: (n: bigint) => Uint8Array<ArrayBuffer>;
|
|
10
|
+
export declare const decodeLEB128: (leb128: Uint8Array<ArrayBuffer>) => bigint;
|