@tktb-tess/util-fns 0.3.1 → 0.3.2
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/brand.d.ts +6 -3
- package/dist/bundle.d.ts +8 -9
- package/dist/bundle.js +124 -100
- package/dist/bundle.umd.cjs +2 -2
- package/dist/fraction.d.ts +2 -1
- package/dist/math.d.ts +0 -6
- package/dist/util.d.ts +18 -10
- package/package.json +2 -1
package/dist/brand.d.ts
CHANGED
package/dist/bundle.d.ts
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getRndInt, residue,
|
|
1
|
+
import { isEqual, sleep, lazify, parseCSV, getHash, toBigInt, isNode } from './util';
|
|
2
|
+
import { getRndInt, residue, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare } from './math';
|
|
3
3
|
import { default as PCGMinimal } from './pcg-minimal';
|
|
4
4
|
import { bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange } from './baillie-psw';
|
|
5
5
|
import { default as Fraction } from './fraction';
|
|
6
6
|
import { base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI } from './u8arr-ext';
|
|
7
|
-
import {
|
|
7
|
+
import { Brand } from './brand';
|
|
8
8
|
declare const Util: {
|
|
9
|
-
|
|
9
|
+
isEqual: (a: unknown, b: unknown) => boolean;
|
|
10
10
|
sleep: (delay: number) => Promise<void>;
|
|
11
11
|
lazify: <ArgT extends unknown[], RetT>(func: (...args: ArgT) => RetT) => (...args: ArgT) => () => RetT;
|
|
12
12
|
parseCSV: (csv: string) => string[][];
|
|
13
13
|
getHash: (str: string, algorithm: AlgorithmIdentifier) => Promise<Uint8Array<ArrayBuffer>>;
|
|
14
|
+
isNode: () => boolean;
|
|
15
|
+
toBigInt: (...nums: number[]) => bigint[];
|
|
14
16
|
};
|
|
15
|
-
declare const
|
|
17
|
+
declare const Calc: {
|
|
16
18
|
getRndInt: (min: number, max: number) => number;
|
|
17
19
|
residue: (n: bigint, mod: bigint) => bigint;
|
|
18
|
-
toBigInt: (...nums: number[]) => bigint[];
|
|
19
20
|
getRandBIByBitLength: (length: number, fixed?: boolean) => bigint;
|
|
20
21
|
getRandBIByRange: (min: bigint, max: bigint) => bigint;
|
|
21
22
|
modPow: (base: bigint, power: bigint, mod: bigint) => bigint;
|
|
@@ -29,8 +30,6 @@ declare const AdMath: {
|
|
|
29
30
|
rot32BI: (value: bigint, rot: bigint) => bigint;
|
|
30
31
|
jacobiSymbol: (a: bigint, n: bigint) => bigint;
|
|
31
32
|
isSquare: (n: bigint) => boolean;
|
|
32
|
-
};
|
|
33
|
-
declare const Prime: {
|
|
34
33
|
bailliePSW: (n: bigint) => boolean;
|
|
35
34
|
getRandPrimeByBitLength: (bitLength: number, fixed?: boolean) => bigint;
|
|
36
35
|
getRandPrimeByRange: (min: bigint, max: bigint) => bigint;
|
|
@@ -43,4 +42,4 @@ declare const U8Ext: {
|
|
|
43
42
|
BItoU8Arr: (n: bigint) => Uint8Array<ArrayBuffer>;
|
|
44
43
|
u8ArrToBI: (buf: Uint8Array) => bigint;
|
|
45
44
|
};
|
|
46
|
-
export {
|
|
45
|
+
export { isEqual, sleep, lazify, parseCSV, getHash, getRndInt, residue, toBigInt, isNode, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare, bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange, base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI, PCGMinimal, Fraction, Util, Calc, U8Ext, type Brand, };
|
package/dist/bundle.js
CHANGED
|
@@ -1,13 +1,37 @@
|
|
|
1
|
-
const R = new TextEncoder(),
|
|
2
|
-
if (n
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
const R = new TextEncoder(), p = (n, t) => {
|
|
2
|
+
if (typeof n != typeof t) return !1;
|
|
3
|
+
const r = Object.prototype.toString.call(n), e = Object.prototype.toString.call(t);
|
|
4
|
+
if (r !== e) return !1;
|
|
5
|
+
if (typeof n == "string" || typeof n == "bigint" || typeof n == "boolean" || typeof n == "symbol" || typeof n > "u")
|
|
6
|
+
return n === t;
|
|
7
|
+
if (typeof n == "number")
|
|
8
|
+
return Number.isNaN(n) && Number.isNaN(t) || n === t;
|
|
9
|
+
if (n === null) return n === t;
|
|
10
|
+
if (typeof n == "function")
|
|
11
|
+
throw Error("comparing these objects is still unavailable");
|
|
12
|
+
if (Array.isArray(n) && Array.isArray(t)) {
|
|
13
|
+
if (n.length !== t.length) return !1;
|
|
14
|
+
for (let i = 0; i < n.length; i++)
|
|
15
|
+
if (!p(n[i], t[i])) return !1;
|
|
16
|
+
return !0;
|
|
17
|
+
}
|
|
18
|
+
if (r === "[object Object]") {
|
|
19
|
+
const i = n, s = t, o = Object.getOwnPropertyNames(n), c = Object.getOwnPropertyNames(t);
|
|
20
|
+
o.push(...Object.getOwnPropertySymbols(n)), c.push(...Object.getOwnPropertySymbols(t));
|
|
21
|
+
for (const l of o) {
|
|
22
|
+
const u = c.find((O) => O === l);
|
|
23
|
+
if (u === void 0) return !1;
|
|
24
|
+
const [h, d] = [i[l], s[u]];
|
|
25
|
+
if (!p(h, d)) return !1;
|
|
26
|
+
}
|
|
27
|
+
return !0;
|
|
28
|
+
}
|
|
29
|
+
throw Error("comparing these objects is still unavailable");
|
|
30
|
+
}, x = (n) => new Promise((t) => {
|
|
7
31
|
setTimeout(() => {
|
|
8
32
|
t();
|
|
9
33
|
}, n);
|
|
10
|
-
}),
|
|
34
|
+
}), M = (n) => (...t) => () => n(...t), T = (n) => {
|
|
11
35
|
const t = [];
|
|
12
36
|
let r = [], e = "", i = !1;
|
|
13
37
|
for (let s = 0; s < n.length; s++) {
|
|
@@ -19,10 +43,10 @@ const R = new TextEncoder(), x = (n, t) => {
|
|
|
19
43
|
}, _ = async (n, t) => {
|
|
20
44
|
const r = R.encode(n), e = await crypto.subtle.digest(t, r);
|
|
21
45
|
return new Uint8Array(e);
|
|
22
|
-
}, b = typeof process < "u" && typeof
|
|
46
|
+
}, L = (...n) => n.map((t) => BigInt(t)), b = () => typeof globalThis.process < "u" && typeof globalThis.require < "u", V = (n, t) => Math.floor(Math.random() * (t - n) + n), a = (n, t) => {
|
|
23
47
|
const r = n % t;
|
|
24
48
|
return r < 0n ? r + t : r;
|
|
25
|
-
},
|
|
49
|
+
}, B = (n, t = !1) => {
|
|
26
50
|
if (!Number.isFinite(n)) throw Error("`length` is not a valid number");
|
|
27
51
|
if (n <= 0) throw Error("`length` must be positive");
|
|
28
52
|
const r = Math.ceil(n / 8), e = crypto.getRandomValues(new Uint8Array(r));
|
|
@@ -32,14 +56,14 @@ const R = new TextEncoder(), x = (n, t) => {
|
|
|
32
56
|
if (n >= t) throw Error("rangeError");
|
|
33
57
|
const r = t - n, e = r.toString(2).length, i = (() => {
|
|
34
58
|
for (let o = 0; o < 1e5; o++) {
|
|
35
|
-
const c =
|
|
36
|
-
if (c >=
|
|
59
|
+
const c = B(e);
|
|
60
|
+
if (c >= g(2n, BigInt(e), r))
|
|
37
61
|
return c % r;
|
|
38
62
|
}
|
|
39
63
|
throw Error("Failed to generate a random bigint");
|
|
40
64
|
})();
|
|
41
65
|
return n + i;
|
|
42
|
-
},
|
|
66
|
+
}, g = (n, t, r) => {
|
|
43
67
|
if (r < 1n) throw Error("`mod` must be positive");
|
|
44
68
|
if (t < 0n) throw Error("`power` must not be negative");
|
|
45
69
|
for (; n < 0n; ) n += r;
|
|
@@ -58,9 +82,9 @@ const R = new TextEncoder(), x = (n, t) => {
|
|
|
58
82
|
return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
|
|
59
83
|
let [r, e, i] = [1n, 0n, n], [s, o, c] = [0n, -1n, t];
|
|
60
84
|
for (; ; ) {
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
[r, s] = [s, r -
|
|
85
|
+
const l = i / c, u = i - l * c;
|
|
86
|
+
if (u === 0n) break;
|
|
87
|
+
[r, s] = [s, r - l * s], [e, o] = [o, e - l * o], [i, c] = [c, u];
|
|
64
88
|
}
|
|
65
89
|
return c < 0n && (s *= -1n, o *= -1n, c *= -1n), { x: s, y: o, gcd: c };
|
|
66
90
|
}, m = (n, t) => {
|
|
@@ -68,8 +92,8 @@ const R = new TextEncoder(), x = (n, t) => {
|
|
|
68
92
|
const r = BigInt((t - 2n).toString(2).length), e = (t - n) / 2n;
|
|
69
93
|
if (r * e < 63n) {
|
|
70
94
|
let c = n;
|
|
71
|
-
for (let
|
|
72
|
-
c *=
|
|
95
|
+
for (let l = n + 2n; l < t; l += 2n)
|
|
96
|
+
c *= l;
|
|
73
97
|
return c;
|
|
74
98
|
}
|
|
75
99
|
const i = n + e | 1n, s = m(n, i), o = m(i, t);
|
|
@@ -103,7 +127,7 @@ const R = new TextEncoder(), x = (n, t) => {
|
|
|
103
127
|
[n, t] = [t, n], n % 4n === 3n && t % 4n === 3n && (r *= -1n), n %= t;
|
|
104
128
|
}
|
|
105
129
|
return t === 1n ? r : 0n;
|
|
106
|
-
},
|
|
130
|
+
}, P = (n) => {
|
|
107
131
|
if (n < 0n) return !1;
|
|
108
132
|
if (n === 0n) return !0;
|
|
109
133
|
let t = 1n, r = n;
|
|
@@ -112,8 +136,8 @@ const R = new TextEncoder(), x = (n, t) => {
|
|
|
112
136
|
e ** 2n < n ? t = e : r = e;
|
|
113
137
|
}
|
|
114
138
|
return n === t ** 2n || n === (t + 1n) ** 2n;
|
|
115
|
-
},
|
|
116
|
-
class
|
|
139
|
+
}, N = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], $ = 0x5851f42d4c957f2dn;
|
|
140
|
+
class Z {
|
|
117
141
|
/**
|
|
118
142
|
* length = 2, `[state, increment]`
|
|
119
143
|
*/
|
|
@@ -126,11 +150,11 @@ class Y {
|
|
|
126
150
|
* @param seeds 64bit整数の配列 (長さ2以上), 省略した場合, 常に同じ値によって初期化される
|
|
127
151
|
*/
|
|
128
152
|
constructor(t) {
|
|
129
|
-
t ? (this.#t[1] = t[1] << 1n | 1n, this.#n(), this.#t[0] = t[0], this.#n()) : (this.#t[0] =
|
|
153
|
+
t ? (this.#t[1] = t[1] << 1n | 1n, this.#n(), this.#t[0] = t[0], this.#n()) : (this.#t[0] = N[0], this.#t[1] = N[1]);
|
|
130
154
|
}
|
|
131
155
|
/** 内部状態を1サイクル進める */
|
|
132
156
|
#n() {
|
|
133
|
-
this.#t[0] = this.#t[0] *
|
|
157
|
+
this.#t[0] = this.#t[0] * $ + this.#t[1];
|
|
134
158
|
}
|
|
135
159
|
/** 32bit 乱数を返す (内部状態は変わらない) */
|
|
136
160
|
get #r() {
|
|
@@ -164,49 +188,49 @@ class Y {
|
|
|
164
188
|
yield typeof r == "number" ? this.getBoundedRand(r) : this.getRand();
|
|
165
189
|
}
|
|
166
190
|
}
|
|
167
|
-
const
|
|
191
|
+
const K = (n) => {
|
|
168
192
|
if (n <= 1n) return !1;
|
|
169
193
|
if (n % 2n === 0n) return n === 2n;
|
|
170
194
|
let t = n - 1n, r = 0n;
|
|
171
195
|
for (; t % 2n === 0n; )
|
|
172
196
|
t >>= 1n, r += 1n;
|
|
173
197
|
const [e, i] = [t, r];
|
|
174
|
-
let o =
|
|
198
|
+
let o = g(2n, e, n);
|
|
175
199
|
if (o === 1n) return !0;
|
|
176
200
|
for (let c = 0n; c < i; c++) {
|
|
177
201
|
if (o === n - 1n) return !0;
|
|
178
202
|
o = o * o % n;
|
|
179
203
|
}
|
|
180
204
|
return !1;
|
|
181
|
-
},
|
|
205
|
+
}, v = (n) => {
|
|
182
206
|
let t = 5n, r = w(t, n);
|
|
183
207
|
for (; r > 0n; ) {
|
|
184
|
-
if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n &&
|
|
208
|
+
if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && P(n))
|
|
185
209
|
return [0n, 0n];
|
|
186
210
|
r = w(t, n);
|
|
187
211
|
}
|
|
188
212
|
return [t, r];
|
|
189
|
-
},
|
|
213
|
+
}, y = (n, t) => (n & 1n) === 1n ? a(n + t >> 1n, t) : a(n >> 1n, t), J = (n, t, r, e) => {
|
|
190
214
|
let i = 1n, s = r;
|
|
191
215
|
const o = n.toString(2).slice(1);
|
|
192
216
|
for (const c of o)
|
|
193
|
-
[i, s] = [
|
|
217
|
+
[i, s] = [a(i * s, t), y(s * s + e * i * i, t)], c === "1" && ([i, s] = [y(r * i + s, t), y(e * i + r * s, t)]);
|
|
194
218
|
return [i, s];
|
|
195
|
-
},
|
|
219
|
+
}, k = (n, t, r, e) => {
|
|
196
220
|
if (n % 2n !== 1n) throw Error("`n` must be odd");
|
|
197
221
|
let i = n + 1n, s = 0n;
|
|
198
222
|
for (; i % 2n === 0n; )
|
|
199
223
|
i >>= 1n, s += 1n;
|
|
200
|
-
const [o, c] =
|
|
201
|
-
let
|
|
224
|
+
const [o, c] = J(i, n, r, t);
|
|
225
|
+
let l = c;
|
|
202
226
|
if (o === 0n) return !0;
|
|
203
|
-
e =
|
|
204
|
-
for (let
|
|
205
|
-
if (
|
|
206
|
-
|
|
227
|
+
e = g(e, i, n);
|
|
228
|
+
for (let u = 0n; u < s; u++) {
|
|
229
|
+
if (l === 0n) return !0;
|
|
230
|
+
l = a(l * l - 2n * e, n), e = g(e, 2n, n);
|
|
207
231
|
}
|
|
208
232
|
return !1;
|
|
209
|
-
},
|
|
233
|
+
}, I = (n) => {
|
|
210
234
|
if (n <= 1n) return !1;
|
|
211
235
|
if (n % 2n === 0n) return n === 2n;
|
|
212
236
|
const t = [
|
|
@@ -240,30 +264,30 @@ const $ = (n) => {
|
|
|
240
264
|
for (const s of t)
|
|
241
265
|
if (n % s === 0n)
|
|
242
266
|
return n === s;
|
|
243
|
-
if (
|
|
267
|
+
if (!K(n))
|
|
244
268
|
return !1;
|
|
245
|
-
const [r, e] =
|
|
269
|
+
const [r, e] = v(n);
|
|
246
270
|
if (e === 0n) return !1;
|
|
247
271
|
const i = (1n - r) / 4n;
|
|
248
|
-
return
|
|
272
|
+
return k(n, r, 1n, i);
|
|
249
273
|
}, z = (n, t) => {
|
|
250
274
|
if (t < 2n)
|
|
251
275
|
throw Error("noPrimesFound");
|
|
252
276
|
for (let e = 0; e < 1e5; e++) {
|
|
253
277
|
const i = S(n, t);
|
|
254
|
-
if (
|
|
278
|
+
if (I(i)) return i;
|
|
255
279
|
}
|
|
256
280
|
throw Error("noPrimesFound");
|
|
257
281
|
}, G = (n, t = !1) => {
|
|
258
282
|
if (n < 2)
|
|
259
283
|
throw Error("noPrimesFound");
|
|
260
284
|
for (let e = 0; e < 1e5; e++) {
|
|
261
|
-
const i =
|
|
262
|
-
if (
|
|
285
|
+
const i = B(n, t);
|
|
286
|
+
if (I(i)) return i;
|
|
263
287
|
}
|
|
264
288
|
throw Error("noPrimesFound");
|
|
265
289
|
};
|
|
266
|
-
class
|
|
290
|
+
class f {
|
|
267
291
|
#t;
|
|
268
292
|
#n;
|
|
269
293
|
/**
|
|
@@ -281,23 +305,23 @@ class l {
|
|
|
281
305
|
*/
|
|
282
306
|
static fromDecimal(t, r = 20) {
|
|
283
307
|
if (Number.isNaN(t))
|
|
284
|
-
return new
|
|
308
|
+
return new f(0n, 0n);
|
|
285
309
|
if (Math.abs(t) === 1 / 0)
|
|
286
|
-
return new
|
|
310
|
+
return new f(t > 0 ? 1n : -1n, 0n);
|
|
287
311
|
const e = t < 0;
|
|
288
312
|
e && (t *= -1);
|
|
289
313
|
const i = BigInt(Math.floor(t)), s = t - Number(i);
|
|
290
314
|
if (s === 0)
|
|
291
|
-
return new
|
|
315
|
+
return new f(e ? -i : i, 1n);
|
|
292
316
|
t = 1 / s;
|
|
293
|
-
let [o, c] = [1n, i], [
|
|
294
|
-
for (; `${
|
|
295
|
-
const
|
|
296
|
-
if ([o, c] = [c,
|
|
297
|
-
return new
|
|
298
|
-
t = 1 /
|
|
317
|
+
let [o, c] = [1n, i], [l, u] = [0n, 1n];
|
|
318
|
+
for (; `${u}`.length < r + 1; ) {
|
|
319
|
+
const h = BigInt(Math.floor(t)), d = t - Number(h);
|
|
320
|
+
if ([o, c] = [c, h * c + o], [l, u] = [u, h * u + l], d === 0)
|
|
321
|
+
return new f(e ? -c : c, u);
|
|
322
|
+
t = 1 / d;
|
|
299
323
|
}
|
|
300
|
-
return new
|
|
324
|
+
return new f(e ? -o : o, l);
|
|
301
325
|
}
|
|
302
326
|
/**
|
|
303
327
|
* reduction
|
|
@@ -311,14 +335,14 @@ class l {
|
|
|
311
335
|
* @returns
|
|
312
336
|
*/
|
|
313
337
|
minus() {
|
|
314
|
-
return new
|
|
338
|
+
return new f(-this.#t, this.#n);
|
|
315
339
|
}
|
|
316
340
|
/**
|
|
317
341
|
* returns inverse of this fraction
|
|
318
342
|
* @returns
|
|
319
343
|
*/
|
|
320
344
|
inverse() {
|
|
321
|
-
return new
|
|
345
|
+
return new f(this.#n, this.#t);
|
|
322
346
|
}
|
|
323
347
|
/**
|
|
324
348
|
* returns `this + right`
|
|
@@ -327,7 +351,7 @@ class l {
|
|
|
327
351
|
*/
|
|
328
352
|
add(t) {
|
|
329
353
|
const r = this.#n * t.#n, e = this.#t * t.#n + t.#t * this.#n;
|
|
330
|
-
return new
|
|
354
|
+
return new f(e, r);
|
|
331
355
|
}
|
|
332
356
|
/**
|
|
333
357
|
* returns `this - right`
|
|
@@ -335,7 +359,7 @@ class l {
|
|
|
335
359
|
* @returns
|
|
336
360
|
*/
|
|
337
361
|
substr(t) {
|
|
338
|
-
return new
|
|
362
|
+
return new f(this.#t, this.#n).add(t.minus());
|
|
339
363
|
}
|
|
340
364
|
/**
|
|
341
365
|
* returns `this * right`
|
|
@@ -344,7 +368,7 @@ class l {
|
|
|
344
368
|
*/
|
|
345
369
|
multiply(t) {
|
|
346
370
|
const r = this.#n * t.#n, e = this.#t * t.#t;
|
|
347
|
-
return new
|
|
371
|
+
return new f(e, r);
|
|
348
372
|
}
|
|
349
373
|
/**
|
|
350
374
|
* returns `this / right`
|
|
@@ -352,7 +376,7 @@ class l {
|
|
|
352
376
|
* @returns
|
|
353
377
|
*/
|
|
354
378
|
divide(t) {
|
|
355
|
-
return new
|
|
379
|
+
return new f(this.#t, this.#n).multiply(
|
|
356
380
|
t.inverse()
|
|
357
381
|
);
|
|
358
382
|
}
|
|
@@ -363,7 +387,7 @@ class l {
|
|
|
363
387
|
*/
|
|
364
388
|
mediant(t) {
|
|
365
389
|
const r = this.#n + t.#n, e = this.#t + t.#t;
|
|
366
|
-
return new
|
|
390
|
+
return new f(e, r);
|
|
367
391
|
}
|
|
368
392
|
/**
|
|
369
393
|
* `number` 型の小数に変換
|
|
@@ -394,7 +418,7 @@ class l {
|
|
|
394
418
|
if (e !== "Fraction") throw Error("cannot parse");
|
|
395
419
|
if (typeof i == "string" && typeof s == "string") {
|
|
396
420
|
const o = BigInt(i), c = BigInt(s);
|
|
397
|
-
return new
|
|
421
|
+
return new f(o, c);
|
|
398
422
|
} else
|
|
399
423
|
throw Error("cannot parse");
|
|
400
424
|
}
|
|
@@ -408,85 +432,85 @@ const H = (n) => {
|
|
|
408
432
|
const t = Array.from(n, (r) => r.toString(16).padStart(2, "0")).join("") || "00";
|
|
409
433
|
return BigInt("0x" + t);
|
|
410
434
|
}, U = (n) => {
|
|
411
|
-
if (b)
|
|
435
|
+
if (b())
|
|
412
436
|
return Buffer.copyBytesFrom(n).toString("base64");
|
|
413
437
|
{
|
|
414
438
|
const t = Array.from(n, (r) => String.fromCharCode(r)).join("");
|
|
415
439
|
return btoa(t);
|
|
416
440
|
}
|
|
417
|
-
},
|
|
418
|
-
if (b) {
|
|
441
|
+
}, j = (n) => {
|
|
442
|
+
if (b()) {
|
|
419
443
|
const t = Buffer.from(n, "base64");
|
|
420
444
|
return new Uint8Array(t.buffer, t.byteOffset, t.byteLength);
|
|
421
445
|
} else
|
|
422
446
|
return Uint8Array.from(atob(n), (t) => t.charCodeAt(0));
|
|
423
|
-
},
|
|
447
|
+
}, X = (n) => U(n).replaceAll(/\+/g, "-").replaceAll(/\//g, "_").replaceAll(/=*$/g, ""), Y = (n) => {
|
|
424
448
|
const t = n.length & 3;
|
|
425
449
|
t > 0 && (n = n + "=".repeat(4 - t));
|
|
426
450
|
const r = n.replaceAll(/-/g, "+").replaceAll(/_/g, "/");
|
|
427
|
-
return
|
|
428
|
-
}, Z = {
|
|
429
|
-
isEqArray: x,
|
|
430
|
-
sleep: M,
|
|
431
|
-
lazify: T,
|
|
432
|
-
parseCSV: L,
|
|
433
|
-
getHash: _
|
|
451
|
+
return j(r);
|
|
434
452
|
}, F = {
|
|
453
|
+
isEqual: p,
|
|
454
|
+
sleep: x,
|
|
455
|
+
lazify: M,
|
|
456
|
+
parseCSV: T,
|
|
457
|
+
getHash: _,
|
|
458
|
+
isNode: b,
|
|
459
|
+
toBigInt: L
|
|
460
|
+
}, Q = {
|
|
435
461
|
getRndInt: V,
|
|
436
|
-
residue:
|
|
437
|
-
|
|
438
|
-
getRandBIByBitLength: p,
|
|
462
|
+
residue: a,
|
|
463
|
+
getRandBIByBitLength: B,
|
|
439
464
|
getRandBIByRange: S,
|
|
440
|
-
modPow:
|
|
465
|
+
modPow: g,
|
|
441
466
|
exEuclidean: A,
|
|
442
467
|
factorial: q,
|
|
443
468
|
rot32: E,
|
|
444
469
|
rot32BI: D,
|
|
445
470
|
jacobiSymbol: w,
|
|
446
|
-
isSquare:
|
|
447
|
-
|
|
448
|
-
bailliePSW: y,
|
|
471
|
+
isSquare: P,
|
|
472
|
+
bailliePSW: I,
|
|
449
473
|
getRandPrimeByBitLength: G,
|
|
450
474
|
getRandPrimeByRange: z
|
|
451
475
|
}, tt = {
|
|
452
|
-
base64ToU8Arr:
|
|
453
|
-
base64UrlToU8Arr:
|
|
476
|
+
base64ToU8Arr: j,
|
|
477
|
+
base64UrlToU8Arr: Y,
|
|
454
478
|
u8ArrToBase64: U,
|
|
455
|
-
u8ArrToBase64Url:
|
|
479
|
+
u8ArrToBase64Url: X,
|
|
456
480
|
BItoU8Arr: H,
|
|
457
481
|
u8ArrToBI: W
|
|
458
482
|
};
|
|
459
483
|
export {
|
|
460
|
-
F as AdMath,
|
|
461
484
|
H as BItoU8Arr,
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
485
|
+
Q as Calc,
|
|
486
|
+
f as Fraction,
|
|
487
|
+
Z as PCGMinimal,
|
|
465
488
|
tt as U8Ext,
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
489
|
+
F as Util,
|
|
490
|
+
I as bailliePSW,
|
|
491
|
+
j as base64ToU8Arr,
|
|
492
|
+
Y as base64UrlToU8Arr,
|
|
470
493
|
A as exEuclidean,
|
|
471
494
|
q as factorial,
|
|
472
495
|
_ as getHash,
|
|
473
|
-
|
|
496
|
+
B as getRandBIByBitLength,
|
|
474
497
|
S as getRandBIByRange,
|
|
475
498
|
G as getRandPrimeByBitLength,
|
|
476
499
|
z as getRandPrimeByRange,
|
|
477
500
|
V as getRndInt,
|
|
478
|
-
|
|
479
|
-
|
|
501
|
+
p as isEqual,
|
|
502
|
+
b as isNode,
|
|
503
|
+
P as isSquare,
|
|
480
504
|
w as jacobiSymbol,
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
505
|
+
M as lazify,
|
|
506
|
+
g as modPow,
|
|
507
|
+
T as parseCSV,
|
|
508
|
+
a as residue,
|
|
485
509
|
E as rot32,
|
|
486
510
|
D as rot32BI,
|
|
487
|
-
|
|
488
|
-
|
|
511
|
+
x as sleep,
|
|
512
|
+
L as toBigInt,
|
|
489
513
|
W as u8ArrToBI,
|
|
490
514
|
U as u8ArrToBase64,
|
|
491
|
-
|
|
515
|
+
X as u8ArrToBase64Url
|
|
492
516
|
};
|
package/dist/bundle.umd.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
`&&!i?(e.push(r.trim()),
|
|
1
|
+
(function(l,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(l=typeof globalThis<"u"?globalThis:l||self,g(l["util-fns"]={}))})(this,(function(l){"use strict";const g=new TextEncoder,m=(n,t)=>{if(typeof n!=typeof t)return!1;const e=Object.prototype.toString.call(n),r=Object.prototype.toString.call(t);if(e!==r)return!1;if(typeof n=="string"||typeof n=="bigint"||typeof n=="boolean"||typeof n=="symbol"||typeof n>"u")return n===t;if(typeof n=="number")return Number.isNaN(n)&&Number.isNaN(t)||n===t;if(n===null)return n===t;if(typeof n=="function")throw Error("comparing these objects is still unavailable");if(Array.isArray(n)&&Array.isArray(t)){if(n.length!==t.length)return!1;for(let i=0;i<n.length;i++)if(!m(n[i],t[i]))return!1;return!0}if(e==="[object Object]"){const i=n,s=t,o=Object.getOwnPropertyNames(n),c=Object.getOwnPropertyNames(t);o.push(...Object.getOwnPropertySymbols(n)),c.push(...Object.getOwnPropertySymbols(t));for(const u of o){const a=c.find(nt=>nt===u);if(a===void 0)return!1;const[y,I]=[i[u],s[a]];if(!m(y,I))return!1}return!0}throw Error("comparing these objects is still unavailable")},j=n=>new Promise(t=>{setTimeout(()=>{t()},n)}),O=n=>(...t)=>()=>n(...t),M=n=>{const t=[];let e=[],r="",i=!1;for(let s=0;s<n.length;s++){const o=n[s];o==='"'&&(s===0||n[s-1]!=="\\")?i=!i:o===","&&!i?(e.push(r.trim()),r=""):o===`
|
|
2
|
+
`&&!i?(e.push(r.trim()),t.push(e),e=[],r=""):r+=o}return e.push(r.trim()),t.push(e),t},L=async(n,t)=>{const e=g.encode(n),r=await crypto.subtle.digest(t,e);return new Uint8Array(r)},_=(...n)=>n.map(t=>BigInt(t)),w=()=>typeof globalThis.process<"u"&&typeof globalThis.require<"u",C=(n,t)=>Math.floor(Math.random()*(t-n)+n),h=(n,t)=>{const e=n%t;return e<0n?e+t:e},b=(n,t=!1)=>{if(!Number.isFinite(n))throw Error("`length` is not a valid number");if(n<=0)throw Error("`length` must be positive");const e=Math.ceil(n/8),r=crypto.getRandomValues(new Uint8Array(e));let i=Array.from(r,s=>s.toString(2).padStart(8,"0")).join("").slice(0,n);return t&&(i=i.replace(/^./,"1")),BigInt("0b"+i)},S=(n,t)=>{if(n>=t)throw Error("rangeError");const e=t-n,r=e.toString(2).length,i=(()=>{for(let o=0;o<1e5;o++){const c=b(r);if(c>=d(2n,BigInt(r),e))return c%e}throw Error("Failed to generate a random bigint")})();return n+i},d=(n,t,e)=>{if(e<1n)throw Error("`mod` must be positive");if(t<0n)throw Error("`power` must not be negative");for(;n<0n;)n+=e;if(e===1n)return 0n;if(n%e===1n||n%e===0n)return n;if(n===e-1n)return t&1n?e-1n:1n;let r=1n;for(;t>0n;)t&1n&&(r=r*n%e),n=n*n%e,t>>=1n;return r},N=(n,t)=>{if(n===0n&&t===0n)return{x:0n,y:0n,gcd:0n};if(n===0n)return t>0n?{x:0n,y:-1n,gcd:t}:{x:0n,y:1n,gcd:-t};if(t===0n)return n>0n?{x:1n,y:0n,gcd:n}:{x:-1n,y:0n,gcd:-n};let[e,r,i]=[1n,0n,n],[s,o,c]=[0n,-1n,t];for(;;){const u=i/c,a=i-u*c;if(a===0n)break;[e,s]=[s,e-u*s],[r,o]=[o,r-u*o],[i,c]=[c,a]}return c<0n&&(s*=-1n,o*=-1n,c*=-1n),{x:s,y:o,gcd:c}},A=(n,t)=>{if(n>=t)return 1n;const e=BigInt((t-2n).toString(2).length),r=(t-n)/2n;if(e*r<63n){let c=n;for(let u=n+2n;u<t;u+=2n)c*=u;return c}const i=n+r|1n,s=A(n,i),o=A(i,t);return s*o},J=n=>{let t=3n,e=1n,r=1n;const i=BigInt(n.toString(2).length)-1n;for(let s=i-1n;s>-1n;--s){const o=(n>>s)+1n|1n;r*=A(t,o),t=o,e*=r}return e},V=n=>{if(n<0n)throw Error("'n' must be non-negative");if(n===0n)return 1n;const t=n-BigInt(n.toString(2).match(/1/g)?.length??0);return J(n)<<t},q=(n,t)=>BigInt.asUintN(32,n>>(t&31n)|n<<(-t&31n)),E=(n,t)=>(n>>>(t&31)|n<<(-t&31))>>>0,B=(n,t)=>{if(t<1n||t%2n===0n)throw Error("`n` is invalid");for(;n<0n;)n+=t;n%=t;let e=1n;for(;n!==0n;){for(;n%2n===0n;){n/=2n;const r=t%8n;(r===3n||r===5n)&&(e*=-1n)}[n,t]=[t,n],n%4n===3n&&t%4n===3n&&(e*=-1n),n%=t}return t===1n?e:0n},P=n=>{if(n<0n)return!1;if(n===0n)return!0;let t=1n,e=n;for(;t+1n<e;){const r=(t+e)/2n;r**2n<n?t=r:e=r}return n===t**2n||n===(t+1n)**2n},D=[0x853c49e6748fea9bn,0xda3e39cb94b95bdbn],W=0x5851f42d4c957f2dn;class k{#t=new BigUint64Array(2);static getSeed(){return crypto.getRandomValues(new BigUint64Array(2))}constructor(t){t?(this.#t[1]=t[1]<<1n|1n,this.#n(),this.#t[0]=t[0],this.#n()):(this.#t[0]=D[0],this.#t[1]=D[1])}#n(){this.#t[0]=this.#t[0]*W+this.#t[1]}get#e(){const t=this.#t[0];if(!t)throw Error("empty state");const e=t>>59n,r=BigInt.asUintN(32,(t^t>>18n)>>27n);return E(Number(r),Number(e))}getRand(){return this.#n(),this.#e}getBoundedRand(t){if(t>4294967296)throw Error("`bound` exceeded limit (2^32)");const r=4294967296%t;for(;;){const i=this.getRand();if(i>=r)return i%t}}*genRands(t,e){for(let r=0;r<t;r++)yield typeof e=="number"?this.getBoundedRand(e):this.getRand()}}const X=n=>{if(n<=1n)return!1;if(n%2n===0n)return n===2n;let t=n-1n,e=0n;for(;t%2n===0n;)t>>=1n,e+=1n;const[r,i]=[t,e];let o=d(2n,r,n);if(o===1n)return!0;for(let c=0n;c<i;c++){if(o===n-1n)return!0;o=o*o%n}return!1},Y=n=>{let t=5n,e=B(t,n);for(;e>0n;){if(t=t>0n?t+2n:t-2n,t*=-1n,t===-15n&&P(n))return[0n,0n];e=B(t,n)}return[t,e]},U=(n,t)=>(n&1n)===1n?h(n+t>>1n,t):h(n>>1n,t),Z=(n,t,e,r)=>{let i=1n,s=e;const o=n.toString(2).slice(1);for(const c of o)[i,s]=[h(i*s,t),U(s*s+r*i*i,t)],c==="1"&&([i,s]=[U(e*i+s,t),U(r*i+e*s,t)]);return[i,s]},F=(n,t,e,r)=>{if(n%2n!==1n)throw Error("`n` must be odd");let i=n+1n,s=0n;for(;i%2n===0n;)i>>=1n,s+=1n;const[o,c]=Z(i,n,e,t);let u=c;if(o===0n)return!0;r=d(r,i,n);for(let a=0n;a<s;a++){if(u===0n)return!0;u=h(u*u-2n*r,n),r=d(r,2n,n)}return!1},p=n=>{if(n<=1n)return!1;if(n%2n===0n)return n===2n;const t=[2n,3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n];for(const s of t)if(n%s===0n)return n===s;if(!X(n))return!1;const[e,r]=Y(n);if(r===0n)return!1;const i=(1n-e)/4n;return F(n,e,1n,i)},$=(n,t)=>{if(t<2n)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=S(n,t);if(p(i))return i}throw Error("noPrimesFound")},v=(n,t=!1)=>{if(n<2)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=b(n,t);if(p(i))return i}throw Error("noPrimesFound")};class f{#t;#n;constructor(t,e){e===0n?(this.#n=0n,this.#t=t===0n?0n:t>0n?1n:-1n):e>0n?(this.#t=t,this.#n=e):(this.#t=-t,this.#n=-e),this.#e()}static fromDecimal(t,e=20){if(Number.isNaN(t))return new f(0n,0n);if(Math.abs(t)===1/0)return new f(t>0?1n:-1n,0n);const r=t<0;r&&(t*=-1);const i=BigInt(Math.floor(t)),s=t-Number(i);if(s===0)return new f(r?-i:i,1n);t=1/s;let[o,c]=[1n,i],[u,a]=[0n,1n];for(;`${a}`.length<e+1;){const y=BigInt(Math.floor(t)),I=t-Number(y);if([o,c]=[c,y*c+o],[u,a]=[a,y*a+u],I===0)return new f(r?-c:c,a);t=1/I}return new f(r?-o:o,u)}#e(){const{gcd:t}=N(this.#t,this.#n);t!==0n&&(this.#t/=t,this.#n/=t)}minus(){return new f(-this.#t,this.#n)}inverse(){return new f(this.#n,this.#t)}add(t){const e=this.#n*t.#n,r=this.#t*t.#n+t.#t*this.#n;return new f(r,e)}substr(t){return new f(this.#t,this.#n).add(t.minus())}multiply(t){const e=this.#n*t.#n,r=this.#t*t.#t;return new f(r,e)}divide(t){return new f(this.#t,this.#n).multiply(t.inverse())}mediant(t){const e=this.#n+t.#n,r=this.#t+t.#t;return new f(r,e)}toDecimal(){return Number(this.#t)/Number(this.#n)}toString(){return this.#t===0n&&this.#n===0n?"NaN":this.#t===0n?"0":this.#n===0n?this.#t<0n?"-Infinity":"Infinity":this.#n===1n?`${this.#t}`:`${this.#t}/${this.#n}`}valueOf(){return this.toDecimal()}toJSON(){return{type:"Fraction",numerator:"0x"+this.#t.toString(16),denominator:"0x"+this.#n.toString(16)}}static parse(t){const e=JSON.parse(t),{type:r,numerator:i,denominator:s}=e;if(r!=="Fraction")throw Error("cannot parse");if(typeof i=="string"&&typeof s=="string"){const o=BigInt(i),c=BigInt(s);return new f(o,c)}else throw Error("cannot parse")}}const K=n=>{let t=n.toString(16);t.length&1&&(t="0"+t);const e=[...t.match(/.{2}/g)??[]];return Uint8Array.from(e,r=>Number.parseInt(r,16))},z=n=>{const t=Array.from(n,e=>e.toString(16).padStart(2,"0")).join("")||"00";return BigInt("0x"+t)},R=n=>{if(w())return Buffer.copyBytesFrom(n).toString("base64");{const t=Array.from(n,e=>String.fromCharCode(e)).join("");return btoa(t)}},T=n=>{if(w()){const t=Buffer.from(n,"base64");return new Uint8Array(t.buffer,t.byteOffset,t.byteLength)}else return Uint8Array.from(atob(n),t=>t.charCodeAt(0))},G=n=>R(n).replaceAll(/\+/g,"-").replaceAll(/\//g,"_").replaceAll(/=*$/g,""),H=n=>{const t=n.length&3;t>0&&(n=n+"=".repeat(4-t));const e=n.replaceAll(/-/g,"+").replaceAll(/_/g,"/");return T(e)},Q={isEqual:m,sleep:j,lazify:O,parseCSV:M,getHash:L,isNode:w,toBigInt:_},x={getRndInt:C,residue:h,getRandBIByBitLength:b,getRandBIByRange:S,modPow:d,exEuclidean:N,factorial:V,rot32:E,rot32BI:q,jacobiSymbol:B,isSquare:P,bailliePSW:p,getRandPrimeByBitLength:v,getRandPrimeByRange:$},tt={base64ToU8Arr:T,base64UrlToU8Arr:H,u8ArrToBase64:R,u8ArrToBase64Url:G,BItoU8Arr:K,u8ArrToBI:z};l.BItoU8Arr=K,l.Calc=x,l.Fraction=f,l.PCGMinimal=k,l.U8Ext=tt,l.Util=Q,l.bailliePSW=p,l.base64ToU8Arr=T,l.base64UrlToU8Arr=H,l.exEuclidean=N,l.factorial=V,l.getHash=L,l.getRandBIByBitLength=b,l.getRandBIByRange=S,l.getRandPrimeByBitLength=v,l.getRandPrimeByRange=$,l.getRndInt=C,l.isEqual=m,l.isNode=w,l.isSquare=P,l.jacobiSymbol=B,l.lazify=O,l.modPow=d,l.parseCSV=M,l.residue=h,l.rot32=E,l.rot32BI=q,l.sleep=j,l.toBigInt=_,l.u8ArrToBI=z,l.u8ArrToBase64=R,l.u8ArrToBase64Url=G,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/fraction.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type FractionData = {
|
|
|
3
3
|
numerator: string;
|
|
4
4
|
denominator: string;
|
|
5
5
|
};
|
|
6
|
-
|
|
6
|
+
declare class Fraction {
|
|
7
7
|
#private;
|
|
8
8
|
/**
|
|
9
9
|
*
|
|
@@ -71,3 +71,4 @@ export default class Fraction {
|
|
|
71
71
|
toJSON(): FractionData;
|
|
72
72
|
static parse(text: string): Fraction;
|
|
73
73
|
}
|
|
74
|
+
export default Fraction;
|
package/dist/math.d.ts
CHANGED
|
@@ -12,12 +12,6 @@ export declare const getRndInt: (min: number, max: number) => number;
|
|
|
12
12
|
* @returns
|
|
13
13
|
*/
|
|
14
14
|
export declare const residue: (n: bigint, mod: bigint) => bigint;
|
|
15
|
-
/**
|
|
16
|
-
* number を bigint に変換
|
|
17
|
-
* @param nums
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
|
-
export declare const toBigInt: (...nums: number[]) => bigint[];
|
|
21
15
|
/**
|
|
22
16
|
* `length` ビットの乱数 or `length` ビット以下の乱数を出力する
|
|
23
17
|
* @param length ビット長
|
package/dist/util.d.ts
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* オブジェクトが等しいかどうかの真偽値を返す
|
|
3
3
|
* @returns
|
|
4
4
|
*/
|
|
5
|
-
declare const
|
|
5
|
+
declare const isEqual: (a: unknown, b: unknown) => boolean;
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @param delay
|
|
7
|
+
* sleep
|
|
8
|
+
* @param delay milliseconds
|
|
9
9
|
* @returns
|
|
10
10
|
*/
|
|
11
11
|
declare const sleep: (delay: number) => Promise<void>;
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* makes function lazy
|
|
14
|
+
*/
|
|
13
15
|
declare const lazify: <ArgT extends unknown[], RetT>(func: (...args: ArgT) => RetT) => (...args: ArgT) => () => RetT;
|
|
14
16
|
/**
|
|
15
|
-
* CSV
|
|
16
|
-
*
|
|
17
|
+
* parses CSV string \
|
|
18
|
+
* can deal with CSV with escaping doublequote
|
|
17
19
|
* @param csv CSV
|
|
18
20
|
* @returns 2次元配列
|
|
19
21
|
*/
|
|
20
22
|
declare const parseCSV: (csv: string) => string[][];
|
|
21
23
|
/**
|
|
22
|
-
*
|
|
24
|
+
* returns hash of a string
|
|
23
25
|
* @param str 文字列
|
|
24
26
|
* @param algorithm アルゴリズム
|
|
25
27
|
* @returns ハッシュ値
|
|
26
28
|
*/
|
|
27
29
|
declare const getHash: (str: string, algorithm: AlgorithmIdentifier) => Promise<Uint8Array<ArrayBuffer>>;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
/**
|
|
31
|
+
* number を bigint に変換
|
|
32
|
+
* @param nums
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
declare const toBigInt: (...nums: number[]) => bigint[];
|
|
36
|
+
declare const isNode: () => boolean;
|
|
37
|
+
export { isEqual, sleep, lazify, parseCSV, getHash, isNode, toBigInt };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tktb-tess/util-fns",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Utility functions for personal use",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"type": "git",
|
|
24
24
|
"url": "https://github.com/tktb-tess/util-fns"
|
|
25
25
|
},
|
|
26
|
+
"homepage": "https://github.com/tktb-tess/util-fns/blob/main/README.md",
|
|
26
27
|
"scripts": {
|
|
27
28
|
"dev": "vite",
|
|
28
29
|
"build": "tsc && vite build",
|