@tktb-tess/util-fns 0.8.3 → 0.8.5

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
@@ -26,11 +26,11 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
26
26
  }
27
27
  if (e === "[object Object]") {
28
28
  const s = n, o = t, i = Object.keys(s), c = Object.keys(o);
29
- for (const f of i) {
30
- const u = c.find((g) => g === f);
31
- if (u === void 0) return !1;
32
- const [h, a] = [s[f], o[u]];
33
- if (!d(h, a)) return !1;
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] = [s[a], o[f]];
33
+ if (!d(h, u)) return !1;
34
34
  }
35
35
  return !0;
36
36
  }
@@ -73,7 +73,7 @@ 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), p = (n, t) => {
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;
@@ -88,23 +88,23 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
88
88
  const e = t - n, r = e.toString(2).length, s = (() => {
89
89
  for (let i = 0; i < 1e5; i++) {
90
90
  const c = E(r);
91
- if (c >= m(2n, BigInt(r), e))
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
96
  return n + s;
97
- }, m = (n, t, e) => {
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 = p(n, e), e === 1n) return 0n;
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
- }, C = (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 };
@@ -112,9 +112,9 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
112
112
  return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
113
113
  let [e, r, s] = [1n, 0n, n], [o, i, c] = [0n, -1n, t];
114
114
  for (; ; ) {
115
- const f = s / c, u = s - f * c;
116
- if (u === 0n) break;
117
- [e, o] = [o, e - f * o], [r, i] = [i, r - f * i], [s, c] = [c, u];
115
+ const a = s / c, f = s - a * c;
116
+ if (f === 0n) break;
117
+ [e, o] = [o, e - a * o], [r, i] = [i, r - a * i], [s, c] = [c, f];
118
118
  }
119
119
  return c < 0n && (o *= -1n, i *= -1n, c *= -1n), { x: o, y: i, gcd: c };
120
120
  }, w = (n, t) => {
@@ -122,13 +122,13 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
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 f = n + 2n; f < t; f += 2n)
126
- c *= f;
125
+ for (let a = n + 2n; a < t; a += 2n)
126
+ c *= a;
127
127
  return c;
128
128
  }
129
129
  const s = n + r | 1n, o = w(n, s), i = w(s, t);
130
130
  return o * i;
131
- }, F = (n) => {
131
+ }, v = (n) => {
132
132
  let t = 3n, e = 1n, r = 1n;
133
133
  const s = BigInt(n.toString(2).length) - 1n;
134
134
  for (let o = s - 1n; o > -1n; --o) {
@@ -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 F(n) << t;
144
- }, rt = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), L = (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
- }, P = (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,33 +166,33 @@ 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
- }, v = (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
175
  const [r, s] = [t, e];
176
- let i = m(2n, r, n);
176
+ let i = p(2n, r, n);
177
177
  if (i === 1n) return !0;
178
178
  for (let c = 0n; c < s; c++) {
179
179
  if (i === n - 1n) return !0;
180
180
  i = i * i % n;
181
181
  }
182
182
  return !1;
183
- }, _ = (n) => {
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 && P(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
- }, y = (n, t) => (n & 1n) === 1n ? p(n + t >> 1n, t) : p(n >> 1n, t), V = (n, t, e, r) => {
191
+ }, y = (n, t) => (n & 1n) === 1n ? m(n + t >> 1n, t) : m(n >> 1n, t), V = (n, t, e, r) => {
192
192
  let s = 1n, o = e;
193
193
  const i = n.toString(2).slice(1);
194
194
  for (const c of i)
195
- [s, o] = [p(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)]);
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
196
  return [s, o];
197
197
  }, O = (n, t, e, r) => {
198
198
  if (n % 2n !== 1n) throw Error("`n` must be odd");
@@ -200,12 +200,12 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
200
200
  for (; s % 2n === 0n; )
201
201
  s >>= 1n, o += 1n;
202
202
  const [i, c] = V(s, n, e, t);
203
- let f = c;
203
+ let a = c;
204
204
  if (i === 0n) return !0;
205
- r = m(r, s, n);
206
- for (let u = 0n; u < o; u++) {
207
- if (f === 0n) return !0;
208
- f = p(f * f - 2n * r, n), r = m(r, 2n, n);
205
+ r = p(r, s, n);
206
+ for (let f = 0n; f < o; 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
  }, x = (n) => {
@@ -242,9 +242,9 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), d = (n, t) => {
242
242
  for (const o of t)
243
243
  if (n % o === 0n)
244
244
  return n === o;
245
- if (!v(n))
245
+ if (!L(n))
246
246
  return !1;
247
- const [e, r] = _(n);
247
+ const [e, r] = P(n);
248
248
  if (r === 0n) return !1;
249
249
  const s = (1n - e) / 4n;
250
250
  return O(n, e, 1n, s);
@@ -294,20 +294,20 @@ class l {
294
294
  if (o === 0)
295
295
  return new l(r ? -s : s, 1n);
296
296
  t = 1 / o;
297
- let [i, c] = [1n, s], [f, u] = [0n, 1n];
298
- for (; `${u}`.length < e + 1; ) {
299
- const h = BigInt(Math.floor(t)), a = t - Number(h);
300
- if ([i, c] = [c, h * c + i], [f, u] = [u, h * u + f], a === 0)
301
- return new l(r ? -c : c, u);
302
- t = 1 / a;
297
+ let [i, c] = [1n, s], [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, f);
304
+ return new l(r ? -i : i, a);
305
305
  }
306
306
  /**
307
307
  * reduction
308
308
  */
309
309
  #e() {
310
- const { gcd: t } = C(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
  /**
@@ -415,10 +415,10 @@ class l {
415
415
  return new l(BigInt(s), BigInt(o));
416
416
  }
417
417
  }
418
- const k = new TextEncoder(), $ = new TextDecoder(), it = (n, t) => {
418
+ const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
419
419
  switch (t) {
420
420
  case "utf-8":
421
- return k.encode(n);
421
+ return $.encode(n);
422
422
  case "base64":
423
423
  return Uint8Array.from(atob(n), (e) => e.charCodeAt(0));
424
424
  case "base64url": {
@@ -426,18 +426,35 @@ 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
- const e = n.matchAll(/.{2}/g), r = Array.from(e, (s) => Number.parseInt(s[0], 16));
430
- return Uint8Array.from(r);
429
+ n.length & 1 && (n = "0" + n);
430
+ const r = n.matchAll(/.{2}/g);
431
+ return Uint8Array.from(r, (s) => Number.parseInt(s[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), o = 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
+ o,
442
+ (i) => Number.parseInt(i[0], 16)
443
+ );
431
444
  }
432
- case "binary": {
433
- const e = n.matchAll(/.{8}/g), r = Array.from(e, (s) => Number.parseInt(s[0], 2));
434
- return Uint8Array.from(r);
445
+ case "bin": {
446
+ const e = n.length & 7;
447
+ e && (n = "0".repeat(8 - e) + n);
448
+ const r = n.matchAll(/.{8}/g), s = Array.from(r, (o) => Number.parseInt(o[0], 2));
449
+ return Uint8Array.from(s);
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 $.decode(n);
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);
@@ -447,16 +464,31 @@ const k = new TextEncoder(), $ = new TextDecoder(), it = (n, t) => {
447
464
  return btoa(e).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
448
465
  }
449
466
  case "hex":
450
- return Array.from(n, (e) => e.toString(16).padStart(2, "0")).join("");
451
- case "binary":
452
- return Array.from(n, (e) => e.toString(2).padStart(8, "0")).join("");
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 s = e.matchAll(/.{6}/g);
476
+ return Array.from(
477
+ s,
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 });
453
485
  }
454
486
  };
455
- class B {
487
+ class A {
456
488
  #t;
457
489
  #n;
458
490
  static name = "Queue";
459
- [Symbol.toStringTag] = B.name;
491
+ [Symbol.toStringTag] = A.name;
460
492
  /**
461
493
  * Queue
462
494
  * @param data
@@ -494,10 +526,10 @@ class B {
494
526
  return this.toArray().toString();
495
527
  }
496
528
  }
497
- class A extends Error {
529
+ class B extends Error {
498
530
  name;
499
531
  static name = "NamedError";
500
- [Symbol.toStringTag] = A.name;
532
+ [Symbol.toStringTag] = B.name;
501
533
  constructor(t, e, r) {
502
534
  super(e, { cause: r }), this.name = t;
503
535
  }
@@ -529,20 +561,21 @@ const I = (n) => {
529
561
  };
530
562
  class U {
531
563
  #t;
564
+ #n;
532
565
  static name = "FloatRand";
533
566
  [Symbol.toStringTag] = U.name;
534
- constructor(t) {
535
- this.#t = t;
567
+ constructor(t, e) {
568
+ this.#t = t, this.#n = e;
536
569
  }
537
- #n() {
570
+ #e() {
538
571
  const r = this.#t.getU32Rand() | 0, s = r & 255;
539
572
  let o = 126;
540
573
  if (s === 0) {
541
574
  o -= 8;
542
575
  const h = 1e5;
543
- let a = 0;
576
+ let u = 0;
544
577
  t: for (; ; ) {
545
- if (a > h)
578
+ if (u > h)
546
579
  throw Error("loop exceeded limit");
547
580
  const g = this.#t.getU32Rand() | 0;
548
581
  if (g === 0) {
@@ -554,16 +587,16 @@ class U {
554
587
  o -= I(g);
555
588
  break t;
556
589
  }
557
- ++a;
590
+ ++u;
558
591
  }
559
592
  } else
560
593
  o -= I(s);
561
594
  const i = r >>> 8 & 8388607;
562
595
  i === 0 && r >>> 31 && ++o;
563
- const { buffer: c, byteOffset: f, length: u } = Uint32Array.from([
596
+ const { buffer: c, byteOffset: a, length: f } = Uint32Array.from([
564
597
  o << 23 | i
565
598
  ]);
566
- return new Float32Array(c, f, u)[0];
599
+ return new Float32Array(c, a, f)[0];
567
600
  }
568
601
  /**
569
602
  * returns a random single-precision floating-point number (float32) in the range of [0.0, 1.0)
@@ -571,27 +604,27 @@ class U {
571
604
  */
572
605
  getF32Rand() {
573
606
  for (let e = 0; e < 1e5; ++e) {
574
- const r = this.#n();
607
+ const r = this.#e();
575
608
  if (r < 1)
576
609
  return r;
577
610
  }
578
611
  throw Error("exceeded loop limit");
579
612
  }
580
- #e() {
613
+ #r() {
581
614
  const t = 0n, e = 1023n, r = (() => {
582
- const h = BigInt(this.#t.getU32Rand() | 0), a = BigInt(this.#t.getU32Rand() | 0);
583
- return h << 32n | a;
615
+ const h = BigInt(this.#t.getU32Rand() | 0), u = BigInt(this.#n.getU32Rand() | 0);
616
+ return h << 32n | u;
584
617
  })(), s = r & 0x7ffn;
585
618
  let o = e - 1n;
586
619
  if (s === 0n) {
587
620
  o -= 11n;
588
621
  const h = 1e5;
589
- let a = 0;
622
+ let u = 0;
590
623
  t: for (; ; ) {
591
- if (a > h)
624
+ if (u > h)
592
625
  throw Error("loop exceeded limit");
593
626
  const g = (() => {
594
- const R = BigInt(this.#t.getU32Rand() | 0), M = BigInt(this.#t.getU32Rand() | 0);
627
+ const R = BigInt(this.#t.getU32Rand() | 0), M = BigInt(this.#n.getU32Rand() | 0);
595
628
  return R << 32n | M;
596
629
  })();
597
630
  if (g === 0n) {
@@ -603,16 +636,16 @@ class U {
603
636
  o -= S(g);
604
637
  break t;
605
638
  }
606
- ++a;
639
+ ++u;
607
640
  }
608
641
  } else
609
642
  o -= S(s);
610
643
  const i = r >> 11n & 0xfffffffffffffn;
611
644
  i === 0n && r >> 63n && ++o;
612
- const { buffer: c, byteOffset: f, length: u } = BigUint64Array.from([
645
+ const { buffer: c, byteOffset: a, length: f } = BigUint64Array.from([
613
646
  o << 52n | i
614
647
  ]);
615
- return new Float64Array(c, f, u)[0];
648
+ return new Float64Array(c, a, f)[0];
616
649
  }
617
650
  /**
618
651
  * returns a random double-precision floating-point number (float64) in the range of [0.0, 1.0)
@@ -620,7 +653,7 @@ class U {
620
653
  */
621
654
  getF64Rand() {
622
655
  for (let e = 0; e < 1e5; ++e) {
623
- const r = this.#e();
656
+ const r = this.#r();
624
657
  if (r < 1)
625
658
  return r;
626
659
  }
@@ -666,7 +699,7 @@ class N {
666
699
  /** 32bit 乱数を返す (内部状態は変わらない) */
667
700
  get #e() {
668
701
  const t = this.#t[0], e = Number(t >> 59n), r = Number(BigInt.asUintN(32, (t ^ t >> 18n) >> 27n));
669
- return L(r, e);
702
+ return C(r, e);
670
703
  }
671
704
  /**
672
705
  *
@@ -701,16 +734,16 @@ class N {
701
734
  }
702
735
  export {
703
736
  U as FloatRand,
704
- A as NamedError,
737
+ B as NamedError,
705
738
  N as PCGMinimal,
706
- B as Queue,
739
+ A as Queue,
707
740
  l as Rational,
708
741
  x as bailliePSW,
709
742
  Q as compress,
710
743
  Y as decodeRFC3986URIComponent,
711
744
  tt as decompress,
712
745
  X as encodeRFC3986URIComponent,
713
- C as exEuclidean,
746
+ _ as exEuclidean,
714
747
  et as factorial,
715
748
  it as fromString,
716
749
  H as getHash,
@@ -721,14 +754,14 @@ export {
721
754
  nt as getRndInt,
722
755
  d as isDeepStrictEqual,
723
756
  Z as isNode,
724
- P as isSquare,
757
+ F as isSquare,
725
758
  b as jacobiSymbol,
726
759
  W as lazify,
727
- m as modPow,
760
+ p as modPow,
728
761
  G as parseCSV,
729
762
  z as promiseWithResolvers,
730
- p as residue,
731
- L as rot32,
763
+ m as residue,
764
+ C as rot32,
732
765
  rt as rot32BI,
733
766
  K as sameValueZero,
734
767
  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
@@ -1,3 +1,8 @@
1
- export type Encoding = 'utf-8' | 'base64' | 'base64url' | 'hex' | 'binary';
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
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tktb-tess/util-fns",
3
- "version": "0.8.3",
3
+ "version": "0.8.5",
4
4
  "description": "Utility functions for personal use",
5
5
  "files": [
6
6
  "dist"