@tktb-tess/util-fns 0.8.4 → 0.8.6

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 CHANGED
@@ -1,4 +1,4 @@
1
- const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
1
+ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
2
2
  if (typeof n != typeof t) return !1;
3
3
  const e = Object.prototype.toString.call(n), r = Object.prototype.toString.call(t);
4
4
  if (e !== r) return !1;
@@ -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 o = 0; o < n.length; o++)
14
- if (!d(n[o], t[o])) return !1;
13
+ for (let s = 0; s < n.length; s++)
14
+ if (!g(n[s], t[s])) return !1;
15
15
  return !0;
16
16
  }
17
17
  if (n instanceof Set && t instanceof Set) {
18
- const o = [...n.values()], s = [...t.values()];
19
- return !!d(o, s);
18
+ const s = [...n.values()], o = [...t.values()];
19
+ return !!g(s, o);
20
20
  }
21
21
  if (n instanceof Map && t instanceof Map) {
22
- const o = [...n.keys()], s = [...t.keys()];
23
- if (!d(o, s)) return !1;
22
+ const s = [...n.keys()], o = [...t.keys()];
23
+ if (!g(s, o)) return !1;
24
24
  const i = [...n.values()], c = [...t.values()];
25
- return !!d(i, c);
25
+ return !!g(i, c);
26
26
  }
27
27
  if (e === "[object Object]") {
28
- const o = n, s = t, i = Object.keys(o), c = Object.keys(s);
28
+ const s = n, o = t, i = Object.keys(s), c = Object.keys(o);
29
29
  for (const a of i) {
30
- const f = c.find((g) => g === a);
30
+ const f = c.find((d) => d === a);
31
31
  if (f === void 0) return !1;
32
- const [h, u] = [o[a], s[f]];
33
- if (!d(h, u)) return !1;
32
+ const [h, u] = [s[a], o[f]];
33
+ if (!g(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, o) => {
44
- n = r, t = o;
43
+ promise: new Promise((r, s) => {
44
+ n = r, t = s;
45
45
  }),
46
46
  resolve: n,
47
47
  reject: t
@@ -50,17 +50,17 @@ 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 = "", 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;
53
+ let e = [], r = "", s = !1;
54
+ for (let o = 0; o < n.length; o++) {
55
+ const i = n[o];
56
+ i === '"' && (o === 0 || n[o - 1] !== "\\") ? s = !s : i === "," && !s ? (e.push(r.trim()), r = "") : i === `
57
+ ` && !s ? (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) => {
61
61
  const e = T.encode(n), r = await crypto.subtle.digest(t, e);
62
62
  return new Uint8Array(r);
63
- }, Z = () => globalThis.process && typeof process.version < "u" && typeof process.versions.node < "u", X = (n) => encodeURIComponent(n).replace(
63
+ }, Z = () => !!globalThis.process && typeof process.version < "u" && typeof process.versions.node < "u", X = (n) => encodeURIComponent(n).replace(
64
64
  /[!'()*]/g,
65
65
  (e) => `%${e.charCodeAt(0).toString(16).toUpperCase()}`
66
66
  ), Y = (n) => {
@@ -77,23 +77,23 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
77
77
  t < 0n && (t *= -1n);
78
78
  const e = n % t;
79
79
  return e < 0n ? e + t : e;
80
- }, B = (n, t = !1) => {
80
+ }, E = (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 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);
84
+ let s = Array.from(r, (o) => o.toString(2).padStart(8, "0")).join("").slice(0, n);
85
+ return t && (s = s.replace(/^\d/, "1")), BigInt("0b" + s);
86
86
  }, j = (n, t) => {
87
87
  if (n >= t) throw Error("rangeError");
88
- const e = t - n, r = e.toString(2).length, o = (() => {
88
+ const e = t - n, r = e.toString(2).length, s = (() => {
89
89
  for (let i = 0; i < 1e5; i++) {
90
- const c = B(r);
90
+ const c = E(r);
91
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 + o;
96
+ return n + s;
97
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");
@@ -104,19 +104,19 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
104
104
  for (; t > 0n; )
105
105
  t & 1n && (r = r * n % e), n = n * n % e, t >>= 1n;
106
106
  return r;
107
- }, L = (n, t) => {
107
+ }, _ = (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, o] = [1n, 0n, n], [s, i, c] = [0n, -1n, t];
113
+ let [e, r, s] = [1n, 0n, n], [o, i, c] = [0n, -1n, t];
114
114
  for (; ; ) {
115
- const a = o / c, f = o - a * c;
115
+ const a = s / c, f = s - a * c;
116
116
  if (f === 0n) break;
117
- [e, s] = [s, e - a * s], [r, i] = [i, r - a * i], [o, c] = [c, f];
117
+ [e, o] = [o, e - a * o], [r, i] = [i, r - a * i], [s, c] = [c, f];
118
118
  }
119
- return c < 0n && (s *= -1n, i *= -1n, c *= -1n), { x: s, y: i, gcd: c };
119
+ return c < 0n && (o *= -1n, i *= -1n, c *= -1n), { x: o, 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;
@@ -126,13 +126,13 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
126
126
  c *= a;
127
127
  return c;
128
128
  }
129
- const o = n + r | 1n, s = w(n, o), i = w(o, t);
130
- return s * i;
131
- }, _ = (n) => {
129
+ const s = n + r | 1n, o = w(n, s), i = w(s, t);
130
+ return o * i;
131
+ }, v = (n) => {
132
132
  let t = 3n, e = 1n, r = 1n;
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;
133
+ const s = BigInt(n.toString(2).length) - 1n;
134
+ for (let o = s - 1n; o > -1n; --o) {
135
+ const i = (n >> o) + 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 _(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) => {
143
+ return v(n) << t;
144
+ }, rt = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), C = (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
- }, C = (n) => {
160
+ }, F = (n) => {
161
161
  if (n < 0n) return !1;
162
162
  if (n === 0n) return !0;
163
163
  let t = 1n, e = n;
@@ -166,16 +166,16 @@ 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
- }, F = (n) => {
169
+ }, L = (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, o] = [t, e];
175
+ const [r, s] = [t, e];
176
176
  let i = p(2n, r, n);
177
177
  if (i === 1n) return !0;
178
- for (let c = 0n; c < o; c++) {
178
+ for (let c = 0n; c < s; c++) {
179
179
  if (i === n - 1n) return !0;
180
180
  i = i * i % n;
181
181
  }
@@ -183,32 +183,32 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
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 && C(n))
186
+ if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && F(n))
187
187
  return [0n, 0n];
188
188
  e = b(t, n);
189
189
  }
190
190
  return [t, e];
191
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;
192
+ let s = 1n, o = e;
193
193
  const i = n.toString(2).slice(1);
194
194
  for (const c of i)
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];
195
+ [s, o] = [m(s * o, t), y(o * o + r * s * s, t)], c === "1" && ([s, o] = [y(e * s + o, t), y(r * s + e * o, t)]);
196
+ return [s, o];
197
197
  }, O = (n, t, e, r) => {
198
198
  if (n % 2n !== 1n) throw Error("`n` must be odd");
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);
199
+ let s = n + 1n, o = 0n;
200
+ for (; s % 2n === 0n; )
201
+ s >>= 1n, o += 1n;
202
+ const [i, c] = V(s, n, e, t);
203
203
  let a = c;
204
204
  if (i === 0n) return !0;
205
- r = p(r, o, n);
206
- for (let f = 0n; f < s; f++) {
205
+ r = p(r, s, n);
206
+ for (let f = 0n; f < o; f++) {
207
207
  if (a === 0n) return !0;
208
208
  a = m(a * a - 2n * r, n), r = p(r, 2n, n);
209
209
  }
210
210
  return !1;
211
- }, E = (n) => {
211
+ }, x = (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 s of t)
243
- if (n % s === 0n)
244
- return n === s;
245
- if (!F(n))
242
+ for (const o of t)
243
+ if (n % o === 0n)
244
+ return n === o;
245
+ if (!L(n))
246
246
  return !1;
247
247
  const [e, r] = P(n);
248
248
  if (r === 0n) return !1;
249
- const o = (1n - e) / 4n;
250
- return O(n, e, 1n, o);
249
+ const s = (1n - e) / 4n;
250
+ return O(n, e, 1n, s);
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 o = j(n, t);
256
- if (E(o)) return o;
255
+ const s = j(n, t);
256
+ if (x(s)) return s;
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 o = B(n, t);
264
- if (E(o)) return o;
263
+ const s = E(n, t);
264
+ if (x(s)) return s;
265
265
  }
266
266
  throw Error("noPrimesFound");
267
267
  };
@@ -290,11 +290,11 @@ 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 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];
293
+ const s = BigInt(Math.floor(t)), o = t - Number(s);
294
+ if (o === 0)
295
+ return new l(r ? -s : s, 1n);
296
+ t = 1 / o;
297
+ let [i, c] = [1n, s], [a, f] = [0n, 1n];
298
298
  for (; `${f}`.length < e + 1; ) {
299
299
  const h = BigInt(Math.floor(t)), u = t - Number(h);
300
300
  if ([i, c] = [c, h * c + i], [a, f] = [f, h * f + a], u === 0)
@@ -307,7 +307,7 @@ class l {
307
307
  * reduction
308
308
  */
309
309
  #e() {
310
- const { gcd: t } = L(this.#t, this.#n);
310
+ const { gcd: t } = _(this.#t, this.#n);
311
311
  t !== 0n && (this.#t /= t, this.#n /= t);
312
312
  }
313
313
  /**
@@ -409,10 +409,10 @@ 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 o = r.at(1), s = r.at(2);
413
- if (!o || !s)
412
+ const s = r.at(1), o = r.at(2);
413
+ if (!s || !o)
414
414
  throw Error("cannot parse");
415
- return new l(BigInt(o), BigInt(s));
415
+ return new l(BigInt(s), BigInt(o));
416
416
  }
417
417
  }
418
418
  const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
@@ -428,25 +428,25 @@ const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
428
428
  case "hex": {
429
429
  n.length & 1 && (n = "0" + n);
430
430
  const r = n.matchAll(/.{2}/g);
431
- return Uint8Array.from(r, (o) => Number.parseInt(o[0], 16));
431
+ return Uint8Array.from(r, (s) => Number.parseInt(s[0], 16));
432
432
  }
433
433
  case "oct": {
434
434
  const e = n.length & 7;
435
435
  e && (n = "0".repeat(8 - e) + n);
436
- const r = n.matchAll(/.{8}/g), s = Array.from(r, (i) => {
436
+ const r = n.matchAll(/.{8}/g), o = Array.from(r, (i) => {
437
437
  const c = i[0];
438
438
  return Number.parseInt(c, 8).toString(16).padStart(6, "0");
439
439
  }).join("").matchAll(/.{2}/g);
440
440
  return Uint8Array.from(
441
- s,
441
+ o,
442
442
  (i) => Number.parseInt(i[0], 16)
443
443
  );
444
444
  }
445
445
  case "bin": {
446
446
  const e = n.length & 7;
447
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);
448
+ const r = n.matchAll(/.{8}/g), s = Array.from(r, (o) => Number.parseInt(o[0], 2));
449
+ return Uint8Array.from(s);
450
450
  }
451
451
  default:
452
452
  throw Error(`Invalid encoding: ${t}`, { cause: t });
@@ -460,7 +460,7 @@ const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
460
460
  return btoa(e);
461
461
  }
462
462
  case "base64url": {
463
- const e = Array.from(n, (o) => String.fromCharCode(o)).join("");
463
+ const e = Array.from(n, (s) => String.fromCharCode(s)).join("");
464
464
  return btoa(e).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
465
465
  }
466
466
  case "hex":
@@ -472,9 +472,9 @@ const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
472
472
  ).join("");
473
473
  const r = e.length % 6;
474
474
  r && (e = "0".repeat(6 - r) + e);
475
- const o = e.matchAll(/.{6}/g);
475
+ const s = e.matchAll(/.{6}/g);
476
476
  return Array.from(
477
- o,
477
+ s,
478
478
  (i) => Number.parseInt(i[0], 16).toString(8).padStart(8, "0")
479
479
  ).join("").replace(/^0+/, "");
480
480
  }
@@ -483,32 +483,12 @@ const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
483
483
  default:
484
484
  throw Error(`Invalid encoding: ${t}`, { cause: t });
485
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;
506
486
  };
507
- class N {
487
+ class A {
508
488
  #t;
509
489
  #n;
510
490
  static name = "Queue";
511
- [Symbol.toStringTag] = N.name;
491
+ [Symbol.toStringTag] = A.name;
512
492
  /**
513
493
  * Queue
514
494
  * @param data
@@ -546,22 +526,22 @@ class N {
546
526
  return this.toArray().toString();
547
527
  }
548
528
  }
549
- class A extends Error {
529
+ class B extends Error {
550
530
  name;
551
531
  static name = "NamedError";
552
- [Symbol.toStringTag] = A.name;
532
+ [Symbol.toStringTag] = B.name;
553
533
  constructor(t, e, r) {
554
534
  super(e, { cause: r }), this.name = t;
555
535
  }
556
536
  toJSON() {
557
537
  const t = (() => {
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;
538
+ const o = this.cause;
539
+ return typeof o == "string" || typeof o == "number" || typeof o == "boolean" ? o : typeof o == "bigint" ? o.toString() : typeof o == "object" && o !== null ? o instanceof Set || o instanceof Map ? Object.fromEntries(o) : o : void 0;
540
+ })(), { name: e, message: r, stack: s } = this;
561
541
  return {
562
542
  name: e,
563
543
  message: r,
564
- stack: o,
544
+ stack: s,
565
545
  cause: t
566
546
  };
567
547
  }
@@ -579,42 +559,41 @@ const I = (n) => {
579
559
  ++t, n >>= 1n;
580
560
  return BigInt.asUintN(64, t);
581
561
  };
582
- class x {
562
+ class U {
583
563
  #t;
584
- #n;
585
564
  static name = "FloatRand";
586
- [Symbol.toStringTag] = x.name;
587
- constructor(t, e) {
588
- this.#t = t, this.#n = e;
565
+ [Symbol.toStringTag] = U.name;
566
+ constructor(t) {
567
+ this.#t = t;
589
568
  }
590
- #e() {
591
- const r = this.#t.getU32Rand() | 0, o = r & 255;
592
- let s = 126;
593
- if (o === 0) {
594
- s -= 8;
569
+ #n() {
570
+ const r = this.#t.getU32Rand() >>> 0, s = r & 255;
571
+ let o = 126;
572
+ if (s === 0) {
573
+ o -= 8;
595
574
  const h = 1e5;
596
575
  let u = 0;
597
576
  t: for (; ; ) {
598
577
  if (u > h)
599
578
  throw Error("loop exceeded limit");
600
- const g = this.#t.getU32Rand() | 0;
601
- if (g === 0) {
602
- if (s -= 32, s < 0) {
603
- s = 0;
579
+ const d = this.#t.getU32Rand() >>> 0;
580
+ if (d === 0) {
581
+ if (o -= 32, o < 0) {
582
+ o = 0;
604
583
  break t;
605
584
  }
606
585
  } else {
607
- s -= I(g);
586
+ o -= I(d);
608
587
  break t;
609
588
  }
610
589
  ++u;
611
590
  }
612
591
  } else
613
- s -= I(o);
592
+ o -= I(s);
614
593
  const i = r >>> 8 & 8388607;
615
- i === 0 && r >>> 31 && ++s;
594
+ i === 0 && r >>> 31 && ++o;
616
595
  const { buffer: c, byteOffset: a, length: f } = Uint32Array.from([
617
- s << 23 | i
596
+ o << 23 | i
618
597
  ]);
619
598
  return new Float32Array(c, a, f)[0];
620
599
  }
@@ -624,46 +603,46 @@ class x {
624
603
  */
625
604
  getF32Rand() {
626
605
  for (let e = 0; e < 1e5; ++e) {
627
- const r = this.#e();
606
+ const r = this.#n();
628
607
  if (r < 1)
629
608
  return r;
630
609
  }
631
610
  throw Error("exceeded loop limit");
632
611
  }
633
- #r() {
612
+ #e() {
634
613
  const t = 0n, e = 1023n, r = (() => {
635
- const h = BigInt(this.#t.getU32Rand() | 0), u = BigInt(this.#n.getU32Rand() | 0);
614
+ const h = BigInt(this.#t.getU32Rand() >>> 0), u = BigInt(this.#t.getU32Rand() >>> 0);
636
615
  return h << 32n | u;
637
- })(), o = r & 0x7ffn;
638
- let s = e - 1n;
639
- if (o === 0n) {
640
- s -= 11n;
616
+ })(), s = r & 0x7ffn;
617
+ let o = e - 1n;
618
+ if (s === 0n) {
619
+ o -= 11n;
641
620
  const h = 1e5;
642
621
  let u = 0;
643
622
  t: for (; ; ) {
644
623
  if (u > h)
645
624
  throw Error("loop exceeded limit");
646
- const g = (() => {
647
- const M = BigInt(this.#t.getU32Rand() | 0), R = BigInt(this.#n.getU32Rand() | 0);
648
- return M << 32n | R;
625
+ const d = (() => {
626
+ const R = BigInt(this.#t.getU32Rand() >>> 0), M = BigInt(this.#t.getU32Rand() >>> 0);
627
+ return R << 32n | M;
649
628
  })();
650
- if (g === 0n) {
651
- if (s -= 64n, s < t) {
652
- s = t;
629
+ if (d === 0n) {
630
+ if (o -= 64n, o < t) {
631
+ o = t;
653
632
  break t;
654
633
  }
655
634
  } else {
656
- s -= S(g);
635
+ o -= S(d);
657
636
  break t;
658
637
  }
659
638
  ++u;
660
639
  }
661
640
  } else
662
- s -= S(o);
641
+ o -= S(s);
663
642
  const i = r >> 11n & 0xfffffffffffffn;
664
- i === 0n && r >> 63n && ++s;
643
+ i === 0n && r >> 63n && ++o;
665
644
  const { buffer: c, byteOffset: a, length: f } = BigUint64Array.from([
666
- s << 52n | i
645
+ o << 52n | i
667
646
  ]);
668
647
  return new Float64Array(c, a, f)[0];
669
648
  }
@@ -673,7 +652,7 @@ class x {
673
652
  */
674
653
  getF64Rand() {
675
654
  for (let e = 0; e < 1e5; ++e) {
676
- const r = this.#r();
655
+ const r = this.#e();
677
656
  if (r < 1)
678
657
  return r;
679
658
  }
@@ -681,13 +660,13 @@ class x {
681
660
  }
682
661
  }
683
662
  const D = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], q = 0x5851f42d4c957f2dn;
684
- class U {
663
+ class N {
685
664
  /**
686
665
  * length = 2, `[state, increment]`
687
666
  */
688
667
  #t;
689
668
  static name = "PCGMinimal";
690
- [Symbol.toStringTag] = U.name;
669
+ [Symbol.toStringTag] = N.name;
691
670
  /**
692
671
  * returns seed for constructing `PCGMinimal` instance
693
672
  * @returns random seed `BigUint64Array` with length 2
@@ -719,7 +698,7 @@ class U {
719
698
  /** 32bit 乱数を返す (内部状態は変わらない) */
720
699
  get #e() {
721
700
  const t = this.#t[0], e = Number(t >> 59n), r = Number(BigInt.asUintN(32, (t ^ t >> 18n) >> 27n));
722
- return v(r, e);
701
+ return C(r, e);
723
702
  }
724
703
  /**
725
704
  *
@@ -735,8 +714,8 @@ class U {
735
714
  if (t > 4294967296) throw Error("`bound` exceeded limit (2^32)");
736
715
  const r = 4294967296 % t;
737
716
  for (; ; ) {
738
- const o = this.getU32Rand();
739
- if (o >= r) return o % t;
717
+ const s = this.getU32Rand();
718
+ if (s >= r) return s % t;
740
719
  }
741
720
  }
742
721
  /**
@@ -753,37 +732,35 @@ class U {
753
732
  }
754
733
  }
755
734
  export {
756
- x as FloatRand,
757
- A as NamedError,
758
- U as PCGMinimal,
759
- N as Queue,
735
+ U as FloatRand,
736
+ B as NamedError,
737
+ N as PCGMinimal,
738
+ A as Queue,
760
739
  l as Rational,
761
- E as bailliePSW,
740
+ x as bailliePSW,
762
741
  Q as compress,
763
- ft as decodeLEB128,
764
742
  Y as decodeRFC3986URIComponent,
765
743
  tt as decompress,
766
- at as encodeLEB128,
767
744
  X as encodeRFC3986URIComponent,
768
- L as exEuclidean,
745
+ _ as exEuclidean,
769
746
  et as factorial,
770
747
  it as fromString,
771
748
  H as getHash,
772
- B as getRandBIByBitLength,
749
+ E as getRandBIByBitLength,
773
750
  j as getRandBIByRange,
774
751
  ot as getRandPrimeByBitLength,
775
752
  st as getRandPrimeByRange,
776
753
  nt as getRndInt,
777
- d as isDeepStrictEqual,
754
+ g as isDeepStrictEqual,
778
755
  Z as isNode,
779
- C as isSquare,
756
+ F as isSquare,
780
757
  b as jacobiSymbol,
781
758
  W as lazify,
782
759
  p as modPow,
783
760
  G as parseCSV,
784
761
  z as promiseWithResolvers,
785
762
  m as residue,
786
- v as rot32,
763
+ C as rot32,
787
764
  rt as rot32BI,
788
765
  K as sameValueZero,
789
766
  J as sleep,
package/dist/main.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from './u8arr-ext';
6
6
  export * from './queue';
7
7
  export * from './named-error';
8
8
  export * from './random';
9
+ export * from './pcg-minimal';
@@ -0,0 +1,47 @@
1
+ import { RandomGenerator } from './random';
2
+ /**
3
+ * PCG-XSH-RR (Permuted congruential generator)
4
+ */
5
+ export declare class PCGMinimal implements RandomGenerator {
6
+ #private;
7
+ static readonly name = "PCGMinimal";
8
+ readonly [Symbol.toStringTag] = "PCGMinimal";
9
+ /**
10
+ * returns seed for constructing `PCGMinimal` instance
11
+ * @returns random seed `BigUint64Array` with length 2
12
+ * @example
13
+ * const rng = new PCGMinimal(PCGMinimal.getSeed())
14
+ */
15
+ static getSeed(): BigUint64Array<ArrayBuffer>;
16
+ /**
17
+ * @param seeds
18
+ * `BigUintArray` with length 2. \
19
+ * if it is not given, initialized by default value
20
+ * @example
21
+ * // the following example is always initialized by the same seeds.
22
+ * // it's not recommended
23
+ * const weakRng = new PCGMinimal();
24
+ *
25
+ * // you should construct with random seeds generated by `PCGMinimal.getSeed()`.
26
+ * const betterRng = new PCGMinimal(PCGMinimal.getSeed());
27
+ */
28
+ constructor(seeds?: BigUint64Array<ArrayBuffer>);
29
+ /**
30
+ *
31
+ * @returns a random 32-bit unsigned integer
32
+ */
33
+ getU32Rand(): number;
34
+ /**
35
+ * @returns a random 32-bit unsigned integer less than `bound`
36
+ */
37
+ getBoundedU32Rand(bound: number): number;
38
+ /**
39
+ *
40
+ * @param step the number of needed random integers
41
+ * @param bound upper limit
42
+ * @returns
43
+ * the iterator that generates random 32-bit unsigned integers `step` times \
44
+ * if `bound` is given, random integers are less than `bound`
45
+ */
46
+ genU32Rands(step: number, bound?: number): Generator<number, void, unknown>;
47
+ }
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, rng2: RandomGenerator);
10
+ constructor(rng: 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
@@ -19,49 +19,3 @@ export declare class FloatRand {
19
19
  */
20
20
  getF64Rand(): number;
21
21
  }
22
- /**
23
- * PCG-XSH-RR (Permuted congruential generator)
24
- */
25
- export declare class PCGMinimal implements RandomGenerator {
26
- #private;
27
- static readonly name = "PCGMinimal";
28
- readonly [Symbol.toStringTag] = "PCGMinimal";
29
- /**
30
- * returns seed for constructing `PCGMinimal` instance
31
- * @returns random seed `BigUint64Array` with length 2
32
- * @example
33
- * const rng = new PCGMinimal(PCGMinimal.getSeed())
34
- */
35
- static getSeed(): BigUint64Array<ArrayBuffer>;
36
- /**
37
- * @param seeds
38
- * `BigUintArray` with length 2. \
39
- * if it is not given, initialized by default value
40
- * @example
41
- * // the following example is always initialized by the same seeds.
42
- * // it's not recommended
43
- * const weakRng = new PCGMinimal();
44
- *
45
- * // you should construct with random seeds generated by `PCGMinimal.getSeed()`.
46
- * const betterRng = new PCGMinimal(PCGMinimal.getSeed());
47
- */
48
- constructor(seeds?: BigUint64Array<ArrayBuffer>);
49
- /**
50
- *
51
- * @returns a random 32-bit unsigned integer
52
- */
53
- getU32Rand(): number;
54
- /**
55
- * @returns a random 32-bit unsigned integer less than `bound`
56
- */
57
- getBoundedU32Rand(bound: number): number;
58
- /**
59
- *
60
- * @param step the number of needed random integers
61
- * @param bound upper limit
62
- * @returns
63
- * the iterator that generates random 32-bit unsigned integers `step` times \
64
- * if `bound` is given, random integers are less than `bound`
65
- */
66
- genU32Rands(step: number, bound?: number): Generator<number, void, unknown>;
67
- }
@@ -1,10 +1,3 @@
1
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tktb-tess/util-fns",
3
- "version": "0.8.4",
3
+ "version": "0.8.6",
4
4
  "description": "Utility functions for personal use",
5
5
  "files": [
6
6
  "dist"