cafe-utility 35.0.1 → 36.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +22 -3
- package/index.js +1607 -1478
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,31 +2,31 @@
|
|
|
2
2
|
var _a
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: !0 }), (exports.Vector = exports.Cache = exports.Assertions = exports.Strings = exports.Types = exports.Objects = exports.Dates = exports.Promises = exports.Numbers = exports.System = exports.Arrays = exports.Random = exports.Elliptic = exports.Binary = exports.Lock = exports.Solver = exports.RollingValueProvider = exports.TrieRouter = exports.AsyncQueue = exports.PubSubChannel = exports.FixedPointNumber = exports.ChunkJoiner = exports.ChunkSplitter = exports.Chunk = exports.Uint8ArrayWriter = exports.Uint8ArrayReader = exports.AsyncLazy = exports.Lazy = exports.Optional = void 0)
|
|
4
4
|
async function invertPromise(n) {
|
|
5
|
-
return new Promise((
|
|
5
|
+
return new Promise((t, e) => n.then(e, t))
|
|
6
6
|
}
|
|
7
7
|
async function raceFulfilled(n) {
|
|
8
8
|
return invertPromise(Promise.all(n.map(invertPromise)))
|
|
9
9
|
}
|
|
10
|
-
async function runInParallelBatches(n,
|
|
11
|
-
const
|
|
10
|
+
async function runInParallelBatches(n, t = 1) {
|
|
11
|
+
const e = splitByCount(n, t),
|
|
12
12
|
r = [],
|
|
13
|
-
i =
|
|
13
|
+
i = e.map(async o => {
|
|
14
14
|
for (const c of o) r.push(await c())
|
|
15
15
|
})
|
|
16
16
|
return await Promise.all(i), r
|
|
17
17
|
}
|
|
18
18
|
async function sleepMillis(n) {
|
|
19
|
-
return new Promise(
|
|
19
|
+
return new Promise(t =>
|
|
20
20
|
setTimeout(() => {
|
|
21
|
-
|
|
21
|
+
t(!0)
|
|
22
22
|
}, n)
|
|
23
23
|
)
|
|
24
24
|
}
|
|
25
|
-
function shuffle(n,
|
|
26
|
-
for (let
|
|
27
|
-
const r = Math.floor(
|
|
28
|
-
i = n[
|
|
29
|
-
;(n[
|
|
25
|
+
function shuffle(n, t = Math.random) {
|
|
26
|
+
for (let e = n.length - 1; e > 0; e--) {
|
|
27
|
+
const r = Math.floor(t() * (e + 1)),
|
|
28
|
+
i = n[e]
|
|
29
|
+
;(n[e] = n[r]), (n[r] = i)
|
|
30
30
|
}
|
|
31
31
|
return n
|
|
32
32
|
}
|
|
@@ -44,229 +44,229 @@ function firstOrThrow(n) {
|
|
|
44
44
|
if (!n.length) throw Error('Received empty array')
|
|
45
45
|
return n[0]
|
|
46
46
|
}
|
|
47
|
-
function initializeArray(n,
|
|
48
|
-
const
|
|
49
|
-
for (let r = 0; r < n; r++)
|
|
50
|
-
return
|
|
47
|
+
function initializeArray(n, t) {
|
|
48
|
+
const e = []
|
|
49
|
+
for (let r = 0; r < n; r++) e.push(t(r))
|
|
50
|
+
return e
|
|
51
51
|
}
|
|
52
52
|
function rotate2DArray(n) {
|
|
53
|
-
const
|
|
54
|
-
for (let
|
|
55
|
-
|
|
56
|
-
for (let r = 0; r < n.length; r++) e
|
|
53
|
+
const t = []
|
|
54
|
+
for (let e = 0; e < n[0].length; e++) {
|
|
55
|
+
t.push([])
|
|
56
|
+
for (let r = 0; r < n.length; r++) t[e].push(n[r][e])
|
|
57
57
|
}
|
|
58
|
-
return
|
|
58
|
+
return t
|
|
59
59
|
}
|
|
60
|
-
function initialize2DArray(n,
|
|
60
|
+
function initialize2DArray(n, t, e) {
|
|
61
61
|
const r = []
|
|
62
62
|
for (let i = 0; i < n; i++) {
|
|
63
63
|
r.push([])
|
|
64
|
-
for (let o = 0; o <
|
|
64
|
+
for (let o = 0; o < t; o++) r[i].push(e)
|
|
65
65
|
}
|
|
66
66
|
return r
|
|
67
67
|
}
|
|
68
|
-
function containsShape(n,
|
|
69
|
-
if (
|
|
70
|
-
for (let i = 0; i <
|
|
68
|
+
function containsShape(n, t, e, r) {
|
|
69
|
+
if (e < 0 || r < 0 || r + t[0].length > n[0].length || e + t.length > n.length) return !1
|
|
70
|
+
for (let i = 0; i < t.length; i++) for (let o = 0; o < t[i].length; o++) if (t[i][o] !== void 0 && n[e + i][r + o] !== t[i][o]) return !1
|
|
71
71
|
return !0
|
|
72
72
|
}
|
|
73
|
-
function pickRandomIndices(n,
|
|
74
|
-
return shuffle(range(0, n.length - 1),
|
|
73
|
+
function pickRandomIndices(n, t, e = Math.random) {
|
|
74
|
+
return shuffle(range(0, n.length - 1), e).slice(0, t)
|
|
75
75
|
}
|
|
76
|
-
function pluck(n,
|
|
77
|
-
return n.map(
|
|
76
|
+
function pluck(n, t) {
|
|
77
|
+
return n.map(e => e[t])
|
|
78
78
|
}
|
|
79
79
|
function makeSeededRng(n) {
|
|
80
|
-
let
|
|
81
|
-
|
|
80
|
+
let t = n,
|
|
81
|
+
e = 3405648695,
|
|
82
82
|
r = 3735928559
|
|
83
83
|
return function () {
|
|
84
|
-
return (
|
|
84
|
+
return (t += e), (e ^= t << 7), (t *= r), (r ^= t << 13), (t ^= e ^ r), (t >>> 0) / 4294967296
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
|
-
function intBetween(n,
|
|
88
|
-
return Math.floor(
|
|
87
|
+
function intBetween(n, t, e = Math.random) {
|
|
88
|
+
return Math.floor(e() * (t - n + 1)) + n
|
|
89
89
|
}
|
|
90
|
-
function floatBetween(n,
|
|
91
|
-
return
|
|
90
|
+
function floatBetween(n, t, e = Math.random) {
|
|
91
|
+
return e() * (t - n) + n
|
|
92
92
|
}
|
|
93
93
|
function signedRandom() {
|
|
94
94
|
return Math.random() * 2 - 1
|
|
95
95
|
}
|
|
96
|
-
function containsPoint(n,
|
|
97
|
-
return
|
|
96
|
+
function containsPoint(n, t, e) {
|
|
97
|
+
return t >= n.x && t < n.x + n.width && e >= n.y && e < n.y + n.height
|
|
98
98
|
}
|
|
99
|
-
function randomPoint(n,
|
|
99
|
+
function randomPoint(n, t, e, r = Math.random) {
|
|
100
100
|
let i, o
|
|
101
|
-
do (i = intBetween(0, n - 1, r)), (o = intBetween(0,
|
|
102
|
-
while (
|
|
101
|
+
do (i = intBetween(0, n - 1, r)), (o = intBetween(0, t - 1, r))
|
|
102
|
+
while (e && containsPoint(e, i, o))
|
|
103
103
|
return [i, o]
|
|
104
104
|
}
|
|
105
|
-
function procs(n,
|
|
106
|
-
const
|
|
107
|
-
r = n -
|
|
108
|
-
return chance(r,
|
|
105
|
+
function procs(n, t = Math.random) {
|
|
106
|
+
const e = Math.floor(n),
|
|
107
|
+
r = n - e
|
|
108
|
+
return chance(r, t) ? e + 1 : e
|
|
109
109
|
}
|
|
110
|
-
function chance(n,
|
|
111
|
-
return
|
|
110
|
+
function chance(n, t = Math.random) {
|
|
111
|
+
return t() < n
|
|
112
112
|
}
|
|
113
|
-
function pick(n,
|
|
114
|
-
return n[Math.floor(n.length *
|
|
113
|
+
function pick(n, t = Math.random) {
|
|
114
|
+
return n[Math.floor(n.length * t())]
|
|
115
115
|
}
|
|
116
|
-
function pickMany(n,
|
|
117
|
-
if (
|
|
118
|
-
return pickRandomIndices(n,
|
|
116
|
+
function pickMany(n, t, e = Math.random) {
|
|
117
|
+
if (t > n.length) throw new Error(`Count (${t}) is greater than array length (${n.length})`)
|
|
118
|
+
return pickRandomIndices(n, t, e).map(i => n[i])
|
|
119
119
|
}
|
|
120
|
-
function pickManyUnique(n,
|
|
121
|
-
if (
|
|
120
|
+
function pickManyUnique(n, t, e, r = Math.random) {
|
|
121
|
+
if (t > n.length) throw new Error(`Count (${t}) is greater than array length (${n.length})`)
|
|
122
122
|
const i = []
|
|
123
|
-
for (; i.length <
|
|
123
|
+
for (; i.length < t; ) {
|
|
124
124
|
const o = pick(n, r)
|
|
125
|
-
i.some(c =>
|
|
125
|
+
i.some(c => e(c, o)) || i.push(o)
|
|
126
126
|
}
|
|
127
127
|
return i
|
|
128
128
|
}
|
|
129
|
-
function pickGuaranteed(n,
|
|
130
|
-
const c = n.filter(
|
|
131
|
-
|
|
132
|
-
for (
|
|
133
|
-
const
|
|
134
|
-
i(c[
|
|
129
|
+
function pickGuaranteed(n, t, e, r, i, o = Math.random) {
|
|
130
|
+
const c = n.filter(u => u !== t && u !== e),
|
|
131
|
+
s = []
|
|
132
|
+
for (t !== null && s.push(t); c.length && s.length < r; ) {
|
|
133
|
+
const u = exports.Random.intBetween(0, c.length - 1, o)
|
|
134
|
+
i(c[u], s) && s.push(c[u]), c.splice(u, 1)
|
|
135
135
|
}
|
|
136
|
-
return shuffle(
|
|
136
|
+
return shuffle(s, o), { values: s, indexOfGuaranteed: t !== null ? s.indexOf(t) : -1 }
|
|
137
137
|
}
|
|
138
138
|
function last(n) {
|
|
139
139
|
if (!n.length) throw Error('Received empty array')
|
|
140
140
|
return n[n.length - 1]
|
|
141
141
|
}
|
|
142
|
-
function pipe(n,
|
|
143
|
-
return t
|
|
142
|
+
function pipe(n, t, e) {
|
|
143
|
+
return e(t.reduce((r, i) => i(r), n))
|
|
144
144
|
}
|
|
145
|
-
function makePipe(n,
|
|
146
|
-
return
|
|
145
|
+
function makePipe(n, t) {
|
|
146
|
+
return e => pipe(e, n, t)
|
|
147
147
|
}
|
|
148
|
-
function pickWeighted(n,
|
|
149
|
-
if ((
|
|
150
|
-
let r =
|
|
151
|
-
const i =
|
|
152
|
-
for (let o = 0; o < n.length - 1; o++) if (((r -=
|
|
148
|
+
function pickWeighted(n, t, e) {
|
|
149
|
+
if ((e === void 0 && (e = Math.random()), n.length !== t.length)) throw new Error('Array length mismatch')
|
|
150
|
+
let r = t.reduce((o, c) => o + c, 0)
|
|
151
|
+
const i = e * r
|
|
152
|
+
for (let o = 0; o < n.length - 1; o++) if (((r -= t[o]), i >= r)) return n[o]
|
|
153
153
|
return last(n)
|
|
154
154
|
}
|
|
155
|
-
function sortWeighted(n,
|
|
156
|
-
const r =
|
|
155
|
+
function sortWeighted(n, t, e = Math.random) {
|
|
156
|
+
const r = t.map(o => e() * o),
|
|
157
157
|
i = []
|
|
158
158
|
for (let o = 0; o < n.length; o++) i.push([n[o], r[o]])
|
|
159
159
|
return i.sort((o, c) => c[1] - o[1]).map(o => o[0])
|
|
160
160
|
}
|
|
161
|
-
function getDeep(n,
|
|
161
|
+
function getDeep(n, t) {
|
|
162
162
|
if (n == null) return null
|
|
163
|
-
const
|
|
163
|
+
const e = t.split('.')
|
|
164
164
|
let r = n
|
|
165
|
-
for (const i of
|
|
165
|
+
for (const i of e) {
|
|
166
166
|
if (r[i] === null || r[i] === void 0) return null
|
|
167
167
|
r = r[i]
|
|
168
168
|
}
|
|
169
169
|
return r
|
|
170
170
|
}
|
|
171
|
-
function setDeep(n,
|
|
172
|
-
const r =
|
|
171
|
+
function setDeep(n, t, e) {
|
|
172
|
+
const r = t.split(/\.|\[/)
|
|
173
173
|
let i = n
|
|
174
174
|
for (let o = 0; o < r.length; o++) {
|
|
175
175
|
const c = r[o],
|
|
176
|
-
|
|
176
|
+
s = o < r.length - 1 && r[o + 1].includes(']'),
|
|
177
177
|
f = c.includes(']') ? c.replace(/\[|\]/g, '') : c
|
|
178
|
-
if (o === r.length - 1) return (i[f] =
|
|
179
|
-
isObject(i[f]) || (
|
|
178
|
+
if (o === r.length - 1) return (i[f] = e), e
|
|
179
|
+
isObject(i[f]) || (s ? (i[f] = []) : (i[f] = {})), (i = i[f])
|
|
180
180
|
}
|
|
181
|
-
return
|
|
181
|
+
return e
|
|
182
182
|
}
|
|
183
|
-
function incrementDeep(n,
|
|
184
|
-
const r = getDeep(n,
|
|
185
|
-
return setDeep(n,
|
|
183
|
+
function incrementDeep(n, t, e = 1) {
|
|
184
|
+
const r = getDeep(n, t) || 0
|
|
185
|
+
return setDeep(n, t, r + e), r
|
|
186
186
|
}
|
|
187
|
-
function ensureDeep(n,
|
|
188
|
-
return getDeep(n,
|
|
187
|
+
function ensureDeep(n, t, e) {
|
|
188
|
+
return getDeep(n, t) || setDeep(n, t, e)
|
|
189
189
|
}
|
|
190
|
-
function deleteDeep(n,
|
|
191
|
-
const
|
|
192
|
-
r = afterLast(
|
|
193
|
-
if (!
|
|
194
|
-
const i = getDeep(n,
|
|
190
|
+
function deleteDeep(n, t) {
|
|
191
|
+
const e = beforeLast(t, '.'),
|
|
192
|
+
r = afterLast(t, '.')
|
|
193
|
+
if (!e || !r) return
|
|
194
|
+
const i = getDeep(n, e)
|
|
195
195
|
i && delete i[r]
|
|
196
196
|
}
|
|
197
|
-
function replaceDeep(n,
|
|
198
|
-
const r = getDeep(n,
|
|
199
|
-
if (!r) throw new Error("Key '" +
|
|
200
|
-
return setDeep(n,
|
|
197
|
+
function replaceDeep(n, t, e) {
|
|
198
|
+
const r = getDeep(n, t)
|
|
199
|
+
if (!r) throw new Error("Key '" + t + "' does not exist.")
|
|
200
|
+
return setDeep(n, t, e), r
|
|
201
201
|
}
|
|
202
|
-
function getFirstDeep(n,
|
|
203
|
-
for (const r of
|
|
202
|
+
function getFirstDeep(n, t, e) {
|
|
203
|
+
for (const r of t) {
|
|
204
204
|
const i = getDeep(n, r)
|
|
205
205
|
if (i) return i
|
|
206
206
|
}
|
|
207
|
-
if (
|
|
207
|
+
if (e) {
|
|
208
208
|
const r = Object.values(n)
|
|
209
209
|
if (r.length) return r[0]
|
|
210
210
|
}
|
|
211
211
|
return null
|
|
212
212
|
}
|
|
213
|
-
async function forever(n,
|
|
213
|
+
async function forever(n, t, e) {
|
|
214
214
|
for (;;) {
|
|
215
215
|
try {
|
|
216
216
|
await n()
|
|
217
217
|
} catch (r) {
|
|
218
|
-
|
|
218
|
+
e && e('Error in forever', r)
|
|
219
219
|
}
|
|
220
|
-
await sleepMillis(
|
|
220
|
+
await sleepMillis(t)
|
|
221
221
|
}
|
|
222
222
|
}
|
|
223
|
-
function runAndSetInterval(n,
|
|
223
|
+
function runAndSetInterval(n, t) {
|
|
224
224
|
n()
|
|
225
|
-
const
|
|
225
|
+
const e = setInterval(() => {
|
|
226
226
|
n()
|
|
227
|
-
},
|
|
228
|
-
return () => clearInterval(
|
|
227
|
+
}, t)
|
|
228
|
+
return () => clearInterval(e)
|
|
229
229
|
}
|
|
230
230
|
function whereAmI() {
|
|
231
231
|
const n = globalThis.process
|
|
232
232
|
return n ? (n.browser === !0 ? 'browser' : 'node') : 'browser'
|
|
233
233
|
}
|
|
234
|
-
async function withRetries(n,
|
|
234
|
+
async function withRetries(n, t, e, r, i, o) {
|
|
235
235
|
let c = null
|
|
236
|
-
for (let
|
|
236
|
+
for (let s = 0; s <= t; s++)
|
|
237
237
|
try {
|
|
238
238
|
return await n()
|
|
239
|
-
} catch (
|
|
240
|
-
if (((c =
|
|
241
|
-
const f =
|
|
242
|
-
i && i('Error in withRetries, retrying', { attempt:
|
|
239
|
+
} catch (u) {
|
|
240
|
+
if (((c = u), s === t)) break
|
|
241
|
+
const f = e + (r - e) * (s / (t - 1))
|
|
242
|
+
i && i('Error in withRetries, retrying', { attempt: s + 1, allowedFailures: t, delayMillis: f, error: u }), o && o(), await sleepMillis(f)
|
|
243
243
|
}
|
|
244
244
|
throw c
|
|
245
245
|
}
|
|
246
246
|
function asMegabytes(n) {
|
|
247
247
|
return n / 1024 / 1024
|
|
248
248
|
}
|
|
249
|
-
function convertBytes(n,
|
|
250
|
-
return n >=
|
|
249
|
+
function convertBytes(n, t = 1024) {
|
|
250
|
+
return n >= t * t * t * t ? (n / t / t / t / t).toFixed(3) + ' TB' : n >= t * t * t ? (n / t / t / t).toFixed(3) + ' GB' : n >= t * t ? (n / t / t).toFixed(3) + ' MB' : n >= t ? (n / t).toFixed(3) + ' KB' : n + ' B'
|
|
251
251
|
}
|
|
252
252
|
function hexToRgb(n) {
|
|
253
|
-
const
|
|
254
|
-
if (!
|
|
255
|
-
return [parseInt(
|
|
253
|
+
const t = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(n.toLowerCase())
|
|
254
|
+
if (!t) throw new Error('Invalid hex color: ' + n)
|
|
255
|
+
return [parseInt(t[1], 16), parseInt(t[2], 16), parseInt(t[3], 16)]
|
|
256
256
|
}
|
|
257
257
|
function rgbToHex(n) {
|
|
258
|
-
return '#' + n.map(
|
|
258
|
+
return '#' + n.map(t => t.toString(16).padStart(2, '0')).join('')
|
|
259
259
|
}
|
|
260
|
-
function haversineDistanceToMeters(n,
|
|
260
|
+
function haversineDistanceToMeters(n, t, e, r) {
|
|
261
261
|
const o = (n * Math.PI) / 180,
|
|
262
|
-
c = (
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
f = Math.sin(
|
|
262
|
+
c = (e * Math.PI) / 180,
|
|
263
|
+
s = ((e - n) * Math.PI) / 180,
|
|
264
|
+
u = ((r - t) * Math.PI) / 180,
|
|
265
|
+
f = Math.sin(s / 2) * Math.sin(s / 2) + Math.cos(o) * Math.cos(c) * Math.sin(u / 2) * Math.sin(u / 2)
|
|
266
266
|
return 6371e3 * (2 * Math.atan2(Math.sqrt(f), Math.sqrt(1 - f)))
|
|
267
267
|
}
|
|
268
|
-
function roundToNearest(n,
|
|
269
|
-
return Math.round(n /
|
|
268
|
+
function roundToNearest(n, t) {
|
|
269
|
+
return Math.round(n / t) * t
|
|
270
270
|
}
|
|
271
271
|
function formatDistance(n) {
|
|
272
272
|
return n > 1e3 ? (n / 1e3).toFixed(0) + ' km' : n >= 500 ? roundToNearest(n, 100) + ' m' : n >= 100 ? roundToNearest(n, 50) + ' m' : roundToNearest(n, 10) + ' m'
|
|
@@ -275,35 +275,35 @@ function triangularNumber(n) {
|
|
|
275
275
|
return (n * (n + 1)) / 2
|
|
276
276
|
}
|
|
277
277
|
function searchFloat(n) {
|
|
278
|
-
const
|
|
279
|
-
if (!
|
|
280
|
-
return parseFloat(
|
|
281
|
-
}
|
|
282
|
-
function binomialSample(n,
|
|
283
|
-
const r = n *
|
|
284
|
-
i = Math.sqrt(n *
|
|
285
|
-
c = (
|
|
286
|
-
|
|
287
|
-
return Math.max(0, Math.min(n,
|
|
288
|
-
}
|
|
289
|
-
function toSignificantDigits(n,
|
|
278
|
+
const t = n.match(/-?\d+(\.\d+)?/)
|
|
279
|
+
if (!t) throw Error('No float found in ' + n)
|
|
280
|
+
return parseFloat(t[0])
|
|
281
|
+
}
|
|
282
|
+
function binomialSample(n, t, e = Math.random) {
|
|
283
|
+
const r = n * t,
|
|
284
|
+
i = Math.sqrt(n * t * (1 - t)),
|
|
285
|
+
c = (e() + e() + e() + e() + e() + e() - 3) * Math.SQRT2,
|
|
286
|
+
s = Math.round(r + i * c)
|
|
287
|
+
return Math.max(0, Math.min(n, s))
|
|
288
|
+
}
|
|
289
|
+
function toSignificantDigits(n, t) {
|
|
290
290
|
if (!n.includes('.')) return n
|
|
291
291
|
if (parseFloat(n) === 0) return '0'
|
|
292
|
-
const [
|
|
293
|
-
i =
|
|
292
|
+
const [e, r] = n.split('.'),
|
|
293
|
+
i = e.replace('-', ''),
|
|
294
294
|
o = i.length
|
|
295
|
-
if (o >=
|
|
295
|
+
if (o >= t) return e
|
|
296
296
|
if (!(i === '0')) {
|
|
297
|
-
const f =
|
|
297
|
+
const f = t - o,
|
|
298
298
|
l = r.slice(0, f)
|
|
299
|
-
return /[1-9]/.test(l) ? `${
|
|
299
|
+
return /[1-9]/.test(l) ? `${e}.${l.replace(/0+$/, '')}` : e
|
|
300
300
|
}
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
return `${
|
|
301
|
+
const s = r.match(/^0*/)?.[0].length ?? 0,
|
|
302
|
+
u = t + s
|
|
303
|
+
return `${e}.${r.slice(0, u)}`
|
|
304
304
|
}
|
|
305
|
-
function isObject(n,
|
|
306
|
-
return !n || (
|
|
305
|
+
function isObject(n, t = !0) {
|
|
306
|
+
return !n || (t && !isUndefined(n._readableState)) || (t && n.constructor && (n.constructor.isBuffer || n.constructor.name == 'AbortController' || n.constructor.name == 'AbortSignal' || n.constructor.name == 'Uint8Array' || n.constructor.name === 'ArrayBuffer' || n.constructor.name === 'ReadableStream')) ? !1 : typeof n == 'object'
|
|
307
307
|
}
|
|
308
308
|
function isStrictlyObject(n) {
|
|
309
309
|
return isObject(n) && !Array.isArray(n)
|
|
@@ -353,259 +353,259 @@ const symbols = '!@#$%^&*()_+-=[]{}|;:<>?,./',
|
|
|
353
353
|
richAsciiAlphabet = alphanumericAlphabet + symbols,
|
|
354
354
|
unicodeTestingAlphabet = ['\u2014', '\\', '\u6771', '\u4EAC', '\u90FD', '\u{1D586}', '\u{1D587}', '\u{1D588}', '\u{1F47E}', '\u{1F647}', '\u{1F481}', '\u{1F645}', '\u16A0', '\u16C7', '\u16BB', '\u16E6'],
|
|
355
355
|
hexAlphabet = '0123456789abcdef'
|
|
356
|
-
function randomLetterString(n,
|
|
357
|
-
let
|
|
358
|
-
for (let r = 0; r < n; r++)
|
|
359
|
-
return
|
|
356
|
+
function randomLetterString(n, t = Math.random) {
|
|
357
|
+
let e = ''
|
|
358
|
+
for (let r = 0; r < n; r++) e += lowercaseAlphabet[Math.floor(t() * lowercaseAlphabet.length)]
|
|
359
|
+
return e
|
|
360
360
|
}
|
|
361
|
-
function randomAlphanumericString(n,
|
|
362
|
-
let
|
|
363
|
-
for (let r = 0; r < n; r++)
|
|
364
|
-
return
|
|
361
|
+
function randomAlphanumericString(n, t = Math.random) {
|
|
362
|
+
let e = ''
|
|
363
|
+
for (let r = 0; r < n; r++) e += alphanumericAlphabet[Math.floor(t() * alphanumericAlphabet.length)]
|
|
364
|
+
return e
|
|
365
365
|
}
|
|
366
|
-
function randomRichAsciiString(n,
|
|
367
|
-
let
|
|
368
|
-
for (let r = 0; r < n; r++)
|
|
369
|
-
return
|
|
366
|
+
function randomRichAsciiString(n, t = Math.random) {
|
|
367
|
+
let e = ''
|
|
368
|
+
for (let r = 0; r < n; r++) e += richAsciiAlphabet[Math.floor(t() * richAsciiAlphabet.length)]
|
|
369
|
+
return e
|
|
370
370
|
}
|
|
371
|
-
function randomUnicodeString(n,
|
|
372
|
-
let
|
|
373
|
-
for (let r = 0; r < n; r++)
|
|
374
|
-
return
|
|
371
|
+
function randomUnicodeString(n, t = Math.random) {
|
|
372
|
+
let e = ''
|
|
373
|
+
for (let r = 0; r < n; r++) e += unicodeTestingAlphabet[Math.floor(t() * unicodeTestingAlphabet.length)]
|
|
374
|
+
return e
|
|
375
375
|
}
|
|
376
|
-
function searchHex(n,
|
|
377
|
-
const
|
|
378
|
-
r = n.match(
|
|
376
|
+
function searchHex(n, t) {
|
|
377
|
+
const e = new RegExp(`[0-9a-f]{${t}}`, 'i'),
|
|
378
|
+
r = n.match(e)
|
|
379
379
|
return r ? r[0] : null
|
|
380
380
|
}
|
|
381
|
-
function searchSubstring(n,
|
|
382
|
-
const r = splitAll(n,
|
|
383
|
-
for (const i of r) if (
|
|
381
|
+
function searchSubstring(n, t, e = symbolsArray) {
|
|
382
|
+
const r = splitAll(n, e)
|
|
383
|
+
for (const i of r) if (t(i)) return i
|
|
384
384
|
return null
|
|
385
385
|
}
|
|
386
|
-
function randomHexString(n,
|
|
387
|
-
let
|
|
388
|
-
for (let r = 0; r < n; r++)
|
|
389
|
-
return
|
|
386
|
+
function randomHexString(n, t = Math.random) {
|
|
387
|
+
let e = ''
|
|
388
|
+
for (let r = 0; r < n; r++) e += hexAlphabet[Math.floor(t() * hexAlphabet.length)]
|
|
389
|
+
return e
|
|
390
390
|
}
|
|
391
|
-
function asIntegerString(n,
|
|
392
|
-
if (!isIntegerString(n)) throw new TypeError(`Expected integer string${
|
|
393
|
-
const
|
|
394
|
-
if (
|
|
391
|
+
function asIntegerString(n, t) {
|
|
392
|
+
if (!isIntegerString(n)) throw new TypeError(`Expected integer string${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
393
|
+
const e = BigInt(n)
|
|
394
|
+
if (t && ((t.min && e < t.min) || (t.max && e > t.max))) throw RangeError(`Expected integer string${t?.name ? ` for ${t.name}` : ''} in range: ${t.min ?? '-inf'}..${t.max ?? 'inf'}; got: ` + e)
|
|
395
395
|
return n
|
|
396
396
|
}
|
|
397
|
-
function asString(n,
|
|
398
|
-
if (isBlank(n)) throw new TypeError(`Expected string${
|
|
399
|
-
if (
|
|
397
|
+
function asString(n, t) {
|
|
398
|
+
if (isBlank(n)) throw new TypeError(`Expected string${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
399
|
+
if (t && ((t.min !== void 0 && n.length < t.min) || (t.max !== void 0 && n.length > t.max))) throw RangeError(`Expected string${t?.name ? ` for ${t.name}` : ''} length in range: ${t.min ?? '-inf'}..${t.max ?? 'inf'}; got: ${n.length}`)
|
|
400
400
|
return n
|
|
401
401
|
}
|
|
402
|
-
function asHexString(n,
|
|
403
|
-
if (!isHexString(n)) throw new TypeError(`Expected hex string${
|
|
404
|
-
if (
|
|
405
|
-
const
|
|
406
|
-
if (
|
|
407
|
-
if (
|
|
408
|
-
return `0x${
|
|
402
|
+
function asHexString(n, t) {
|
|
403
|
+
if (!isHexString(n)) throw new TypeError(`Expected hex string${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
404
|
+
if (t?.strictPrefix && !n.startsWith('0x') && !n.startsWith('0X')) throw new TypeError(`Expected hex string with 0x prefix${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
405
|
+
const e = n.replace(/^0x/i, '')
|
|
406
|
+
if (e.length % 2 !== 0 && !t?.uneven) throw RangeError(`Expected even number of hex digits${t?.name ? ` for ${t.name}` : ''}; got: ` + n)
|
|
407
|
+
if (t && t.byteLength && e.length !== t.byteLength * 2) throw RangeError(`Expected hex string${t?.name ? ` for ${t.name}` : ''} of byte length ${t.byteLength}; got: ` + e)
|
|
408
|
+
return `0x${e}`
|
|
409
409
|
}
|
|
410
|
-
function asSafeString(n,
|
|
410
|
+
function asSafeString(n, t) {
|
|
411
411
|
if (
|
|
412
|
-
!asString(n,
|
|
412
|
+
!asString(n, t)
|
|
413
413
|
.split('')
|
|
414
|
-
.every(
|
|
414
|
+
.every(e => e === '_' || isLetterOrDigit(e))
|
|
415
415
|
)
|
|
416
|
-
throw new TypeError(`Expected safe string${
|
|
416
|
+
throw new TypeError(`Expected safe string${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
417
417
|
return n
|
|
418
418
|
}
|
|
419
|
-
function checkLimits(n,
|
|
420
|
-
if ((
|
|
419
|
+
function checkLimits(n, t) {
|
|
420
|
+
if ((t.min !== void 0 && n < t.min) || (t.max !== void 0 && n > t.max)) throw RangeError(`Expected value${t?.name ? ` for ${t.name}` : ''} in range: ${t.min ?? '-inf'}..${t.max ?? 'inf'}; got: ${n}`)
|
|
421
421
|
}
|
|
422
|
-
function asFunction(n,
|
|
423
|
-
if (!isFunction(n)) throw new TypeError(`Expected function${
|
|
422
|
+
function asFunction(n, t) {
|
|
423
|
+
if (!isFunction(n)) throw new TypeError(`Expected function${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
424
424
|
return n
|
|
425
425
|
}
|
|
426
|
-
function asNumber(n,
|
|
427
|
-
if (isNumber(n)) return
|
|
428
|
-
if (!isString(n) || !n.match(/^-?\d+(\.\d+)?$/)) throw new TypeError(`Expected number${
|
|
429
|
-
const
|
|
430
|
-
return
|
|
426
|
+
function asNumber(n, t) {
|
|
427
|
+
if (isNumber(n)) return t && checkLimits(n, t), n
|
|
428
|
+
if (!isString(n) || !n.match(/^-?\d+(\.\d+)?$/)) throw new TypeError(`Expected number${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
429
|
+
const e = parseFloat(n)
|
|
430
|
+
return t && checkLimits(e, t), e
|
|
431
431
|
}
|
|
432
|
-
function asInteger(n,
|
|
433
|
-
return Math.trunc(asNumber(n,
|
|
432
|
+
function asInteger(n, t) {
|
|
433
|
+
return Math.trunc(asNumber(n, t))
|
|
434
434
|
}
|
|
435
|
-
function asBoolean(n,
|
|
435
|
+
function asBoolean(n, t) {
|
|
436
436
|
if (n === 'true') return !0
|
|
437
437
|
if (n === 'false') return !1
|
|
438
|
-
if (!isBoolean(n)) throw new TypeError(`Expected boolean${
|
|
438
|
+
if (!isBoolean(n)) throw new TypeError(`Expected boolean${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
439
439
|
return n
|
|
440
440
|
}
|
|
441
|
-
function asDate(n,
|
|
442
|
-
if (!isDate(n)) throw new TypeError(`Expected date${
|
|
441
|
+
function asDate(n, t) {
|
|
442
|
+
if (!isDate(n)) throw new TypeError(`Expected date${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
443
443
|
return n
|
|
444
444
|
}
|
|
445
445
|
function asNullableString(n) {
|
|
446
446
|
return isBlank(n) ? null : n
|
|
447
447
|
}
|
|
448
|
-
function asEmptiableString(n,
|
|
449
|
-
if (!isString(n)) throw new TypeError(`Expected string${
|
|
448
|
+
function asEmptiableString(n, t) {
|
|
449
|
+
if (!isString(n)) throw new TypeError(`Expected string${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
450
450
|
return n
|
|
451
451
|
}
|
|
452
|
-
function asId(n,
|
|
452
|
+
function asId(n, t) {
|
|
453
453
|
if (isId(n)) return n
|
|
454
|
-
const
|
|
455
|
-
if (!isId(
|
|
456
|
-
return
|
|
454
|
+
const e = parseInt(n, 10)
|
|
455
|
+
if (!isId(e)) throw new TypeError(`Expected id${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
456
|
+
return e
|
|
457
457
|
}
|
|
458
|
-
function asTime(n,
|
|
459
|
-
if (!isString(n)) throw new TypeError(`Expected time${
|
|
460
|
-
const
|
|
461
|
-
if (
|
|
462
|
-
const r = parseInt(
|
|
463
|
-
i = parseInt(
|
|
464
|
-
if (!isNumber(r) || !isNumber(i) || r < 0 || r > 23 || i < 0 || i > 59) throw new TypeError(`Expected time, got${
|
|
458
|
+
function asTime(n, t) {
|
|
459
|
+
if (!isString(n)) throw new TypeError(`Expected time${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
460
|
+
const e = n.split(':')
|
|
461
|
+
if (e.length !== 2) throw new TypeError(`Expected time${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
462
|
+
const r = parseInt(e[0], 10),
|
|
463
|
+
i = parseInt(e[1], 10)
|
|
464
|
+
if (!isNumber(r) || !isNumber(i) || r < 0 || r > 23 || i < 0 || i > 59) throw new TypeError(`Expected time, got${t?.name ? ` for ${t.name}` : ''}: ` + n)
|
|
465
465
|
return `${String(r).padStart(2, '0')}:${String(i).padStart(2, '0')}`
|
|
466
466
|
}
|
|
467
|
-
function asArray(n,
|
|
468
|
-
if (!Array.isArray(n)) throw new TypeError(`Expected array${
|
|
467
|
+
function asArray(n, t) {
|
|
468
|
+
if (!Array.isArray(n)) throw new TypeError(`Expected array${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
469
469
|
return n
|
|
470
470
|
}
|
|
471
|
-
function asObject(n,
|
|
472
|
-
if (!isStrictlyObject(n)) throw new TypeError(`Expected object${
|
|
471
|
+
function asObject(n, t) {
|
|
472
|
+
if (!isStrictlyObject(n)) throw new TypeError(`Expected object${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
473
473
|
return n
|
|
474
474
|
}
|
|
475
|
-
function asNullableObject(n,
|
|
476
|
-
return n === null ? null : asObject(n,
|
|
475
|
+
function asNullableObject(n, t) {
|
|
476
|
+
return n === null ? null : asObject(n, t)
|
|
477
477
|
}
|
|
478
|
-
function asStringMap(n,
|
|
479
|
-
const
|
|
480
|
-
for (const r of Object.keys(
|
|
481
|
-
return
|
|
478
|
+
function asStringMap(n, t) {
|
|
479
|
+
const e = asObject(n, t)
|
|
480
|
+
for (const r of Object.keys(e)) if (!isString(e[r])) throw new TypeError(`Expected string map${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
481
|
+
return e
|
|
482
482
|
}
|
|
483
|
-
function asNumericDictionary(n,
|
|
484
|
-
const
|
|
485
|
-
r = Object.keys(
|
|
486
|
-
i = Object.values(
|
|
487
|
-
if (!r.every(isString) || !i.every(isNumber)) throw new TypeError(`Expected numeric dictionary${
|
|
488
|
-
return
|
|
483
|
+
function asNumericDictionary(n, t) {
|
|
484
|
+
const e = asObject(n),
|
|
485
|
+
r = Object.keys(e),
|
|
486
|
+
i = Object.values(e)
|
|
487
|
+
if (!r.every(isString) || !i.every(isNumber)) throw new TypeError(`Expected numeric dictionary${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
488
|
+
return e
|
|
489
489
|
}
|
|
490
490
|
function isUrl(n) {
|
|
491
491
|
return isString(n) && n.match(/^https?:\/\/.+/) !== null
|
|
492
492
|
}
|
|
493
|
-
function asUrl(n,
|
|
494
|
-
if (!isUrl(n)) throw new TypeError(`Expected url${
|
|
493
|
+
function asUrl(n, t) {
|
|
494
|
+
if (!isUrl(n)) throw new TypeError(`Expected url${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
495
495
|
return n
|
|
496
496
|
}
|
|
497
497
|
function isBigint(n) {
|
|
498
498
|
return typeof n == 'bigint'
|
|
499
499
|
}
|
|
500
|
-
function asBigint(n,
|
|
501
|
-
if (!isBigint(n)) throw new TypeError(`Expected bigint${
|
|
502
|
-
if ((
|
|
500
|
+
function asBigint(n, t) {
|
|
501
|
+
if (!isBigint(n)) throw new TypeError(`Expected bigint${t?.name ? ` for ${t.name}` : ''}, got: ` + n)
|
|
502
|
+
if ((t?.min !== void 0 && n < t?.min) || (t?.max !== void 0 && n > t?.max)) throw RangeError(`Expected value${t?.name ? ` for ${t.name}` : ''} in range: ${t.min ?? '-inf'}..${t.max ?? 'inf'}; got: ${n}`)
|
|
503
503
|
return n
|
|
504
504
|
}
|
|
505
|
-
function isNullable(n,
|
|
506
|
-
return
|
|
505
|
+
function isNullable(n, t) {
|
|
506
|
+
return t == null ? !0 : n(t)
|
|
507
507
|
}
|
|
508
|
-
function asNullable(n,
|
|
509
|
-
return
|
|
508
|
+
function asNullable(n, t) {
|
|
509
|
+
return t == null ? null : n(t)
|
|
510
510
|
}
|
|
511
|
-
function asEmptiable(n,
|
|
512
|
-
return
|
|
511
|
+
function asEmptiable(n, t) {
|
|
512
|
+
return t === '' ? void 0 : n(t)
|
|
513
513
|
}
|
|
514
|
-
function asOptional(n,
|
|
515
|
-
return
|
|
514
|
+
function asOptional(n, t) {
|
|
515
|
+
return t == null ? void 0 : n(t)
|
|
516
516
|
}
|
|
517
|
-
function enforceObjectShape(n,
|
|
518
|
-
for (const [
|
|
519
|
-
for (const
|
|
517
|
+
function enforceObjectShape(n, t) {
|
|
518
|
+
for (const [e, r] of Object.entries(t)) if (!r(n[e])) throw TypeError(`${e} in value does not exist or match shape`)
|
|
519
|
+
for (const e of Object.keys(n)) if (!t[e]) throw TypeError(`${e} exists in value but not in shape`)
|
|
520
520
|
return !0
|
|
521
521
|
}
|
|
522
|
-
function enforceArrayShape(n,
|
|
523
|
-
return n.every(
|
|
522
|
+
function enforceArrayShape(n, t) {
|
|
523
|
+
return n.every(e => enforceObjectShape(e, t))
|
|
524
524
|
}
|
|
525
|
-
function represent(n,
|
|
525
|
+
function represent(n, t = 'json', e = 0) {
|
|
526
526
|
if (n && isFunction(n.represent)) {
|
|
527
527
|
const r = n.represent()
|
|
528
|
-
if (isString(r)) return
|
|
528
|
+
if (isString(r)) return t === 'json' && e === 0 ? JSON.stringify(r) : r
|
|
529
529
|
}
|
|
530
530
|
if (isObject(n, !1)) {
|
|
531
|
-
if (
|
|
532
|
-
if (
|
|
531
|
+
if (e > 1) return '[object Object]'
|
|
532
|
+
if (t === 'json') {
|
|
533
533
|
if (Array.isArray(n)) {
|
|
534
|
-
const i = n.map(o => represent(o, 'json',
|
|
535
|
-
return
|
|
534
|
+
const i = n.map(o => represent(o, 'json', e + 1))
|
|
535
|
+
return e === 0 ? JSON.stringify(i) : i
|
|
536
536
|
}
|
|
537
537
|
const r = {}
|
|
538
|
-
n.message && (r.message = represent(n.message, 'json',
|
|
539
|
-
for (const [i, o] of Object.entries(n)) r[i] = represent(o, 'json',
|
|
540
|
-
return
|
|
541
|
-
} else if (
|
|
538
|
+
n.message && (r.message = represent(n.message, 'json', e + 1))
|
|
539
|
+
for (const [i, o] of Object.entries(n)) r[i] = represent(o, 'json', e + 1)
|
|
540
|
+
return e === 0 ? JSON.stringify(r) : r
|
|
541
|
+
} else if (t === 'key-value') {
|
|
542
542
|
const r = Object.keys(n)
|
|
543
|
-
return n.message && !r.includes('message') && r.unshift('message'), r.map(i => `${i}=${JSON.stringify(represent(n[i], 'json',
|
|
543
|
+
return n.message && !r.includes('message') && r.unshift('message'), r.map(i => `${i}=${JSON.stringify(represent(n[i], 'json', e + 1))}`).join(' ')
|
|
544
544
|
}
|
|
545
545
|
}
|
|
546
|
-
return isUndefined(n) && (n = 'undefined'),
|
|
546
|
+
return isUndefined(n) && (n = 'undefined'), e === 0 ? JSON.stringify(n) : n
|
|
547
547
|
}
|
|
548
|
-
function expandError(n,
|
|
548
|
+
function expandError(n, t) {
|
|
549
549
|
if (isString(n)) return n
|
|
550
|
-
const
|
|
551
|
-
n.message && !
|
|
552
|
-
const r =
|
|
553
|
-
return
|
|
550
|
+
const e = Object.keys(n)
|
|
551
|
+
n.message && !e.includes('message') && e.push('message')
|
|
552
|
+
const r = e.map(i => `${i}: ${n[i]}`).join('; ')
|
|
553
|
+
return t && n.stack
|
|
554
554
|
? r +
|
|
555
555
|
`
|
|
556
556
|
` +
|
|
557
557
|
n.stack
|
|
558
558
|
: r
|
|
559
559
|
}
|
|
560
|
-
function deepMergeInPlace(n,
|
|
561
|
-
if (isStrictlyObject(n) && isStrictlyObject(
|
|
560
|
+
function deepMergeInPlace(n, t) {
|
|
561
|
+
if (isStrictlyObject(n) && isStrictlyObject(t)) for (const e in t) isStrictlyObject(t[e]) ? (n[e] || (n[e] = {}), deepMergeInPlace(n[e], t[e])) : Array.isArray(t[e]) ? (n[e] = [...t[e]]) : ((t[e] !== null && t[e] !== void 0) || n[e] === null || n[e] === void 0) && (n[e] = t[e])
|
|
562
562
|
return n
|
|
563
563
|
}
|
|
564
|
-
function deepMerge2(n,
|
|
565
|
-
const
|
|
566
|
-
return deepMergeInPlace(
|
|
564
|
+
function deepMerge2(n, t) {
|
|
565
|
+
const e = {}
|
|
566
|
+
return deepMergeInPlace(e, n), deepMergeInPlace(e, t), e
|
|
567
567
|
}
|
|
568
|
-
function deepMerge3(n,
|
|
568
|
+
function deepMerge3(n, t, e) {
|
|
569
569
|
const r = {}
|
|
570
|
-
return deepMergeInPlace(r, n), deepMergeInPlace(r,
|
|
570
|
+
return deepMergeInPlace(r, n), deepMergeInPlace(r, t), deepMergeInPlace(r, e), r
|
|
571
571
|
}
|
|
572
|
-
function zip(n,
|
|
573
|
-
const
|
|
574
|
-
for (const r of n) for (const i of Object.keys(r))
|
|
575
|
-
return
|
|
572
|
+
function zip(n, t) {
|
|
573
|
+
const e = {}
|
|
574
|
+
for (const r of n) for (const i of Object.keys(r)) e[i] ? (e[i] = t(e[i], r[i])) : (e[i] = r[i])
|
|
575
|
+
return e
|
|
576
576
|
}
|
|
577
577
|
function zipSum(n) {
|
|
578
|
-
return zip(n, (
|
|
578
|
+
return zip(n, (t, e) => t + e)
|
|
579
579
|
}
|
|
580
|
-
function pushToBucket(n,
|
|
581
|
-
n[
|
|
580
|
+
function pushToBucket(n, t, e) {
|
|
581
|
+
n[t] || (n[t] = []), n[t].push(e)
|
|
582
582
|
}
|
|
583
|
-
function unshiftAndLimit(n,
|
|
584
|
-
for (n.unshift(
|
|
583
|
+
function unshiftAndLimit(n, t, e) {
|
|
584
|
+
for (n.unshift(t); n.length > e; ) n.pop()
|
|
585
585
|
}
|
|
586
|
-
function atRolling(n,
|
|
587
|
-
let
|
|
588
|
-
return
|
|
586
|
+
function atRolling(n, t) {
|
|
587
|
+
let e = t % n.length
|
|
588
|
+
return e < 0 && (e += n.length), n[e]
|
|
589
589
|
}
|
|
590
|
-
function pushAll(n,
|
|
591
|
-
Array.prototype.push.apply(n,
|
|
590
|
+
function pushAll(n, t) {
|
|
591
|
+
Array.prototype.push.apply(n, t)
|
|
592
592
|
}
|
|
593
|
-
function unshiftAll(n,
|
|
594
|
-
Array.prototype.unshift.apply(n,
|
|
593
|
+
function unshiftAll(n, t) {
|
|
594
|
+
Array.prototype.unshift.apply(n, t)
|
|
595
595
|
}
|
|
596
|
-
async function mapAllAsync(n,
|
|
597
|
-
const
|
|
598
|
-
for (const r of n)
|
|
599
|
-
return
|
|
596
|
+
async function mapAllAsync(n, t) {
|
|
597
|
+
const e = []
|
|
598
|
+
for (const r of n) e.push(await t(r))
|
|
599
|
+
return e
|
|
600
600
|
}
|
|
601
|
-
function glue(n,
|
|
602
|
-
const
|
|
603
|
-
for (let r = 0; r < n.length; r++)
|
|
604
|
-
return
|
|
601
|
+
function glue(n, t) {
|
|
602
|
+
const e = []
|
|
603
|
+
for (let r = 0; r < n.length; r++) e.push(n[r]), r < n.length - 1 && (isFunction(t) ? e.push(t()) : e.push(t))
|
|
604
|
+
return e
|
|
605
605
|
}
|
|
606
|
-
function asEqual(n,
|
|
607
|
-
if (n !==
|
|
608
|
-
return [n,
|
|
606
|
+
function asEqual(n, t) {
|
|
607
|
+
if (n !== t) throw Error(`Expected [${n}] to equal [${t}]`)
|
|
608
|
+
return [n, t]
|
|
609
609
|
}
|
|
610
610
|
function asTrue(n) {
|
|
611
611
|
if (n !== !0) throw Error(`Expected [true], got: [${n}]`)
|
|
@@ -623,53 +623,53 @@ function asFalsy(n) {
|
|
|
623
623
|
if (n) throw Error(`Expected falsy value, got: [${n}]`)
|
|
624
624
|
return n
|
|
625
625
|
}
|
|
626
|
-
function asEither(n,
|
|
627
|
-
if (!
|
|
626
|
+
function asEither(n, t) {
|
|
627
|
+
if (!t.includes(n)) throw Error(`Expected any of [${t.join(', ')}], got: [${n}]`)
|
|
628
628
|
return n
|
|
629
629
|
}
|
|
630
|
-
function scheduleMany(n,
|
|
631
|
-
for (let
|
|
632
|
-
const r = n[
|
|
633
|
-
i = e
|
|
630
|
+
function scheduleMany(n, t) {
|
|
631
|
+
for (let e = 0; e < n.length; e++) {
|
|
632
|
+
const r = n[e],
|
|
633
|
+
i = t[e],
|
|
634
634
|
o = Math.max(0, i.getTime() - Date.now())
|
|
635
635
|
setTimeout(r, o)
|
|
636
636
|
}
|
|
637
637
|
}
|
|
638
|
-
function interpolate(n,
|
|
639
|
-
return n + (
|
|
638
|
+
function interpolate(n, t, e) {
|
|
639
|
+
return n + (t - n) * e
|
|
640
640
|
}
|
|
641
641
|
function sum(n) {
|
|
642
|
-
return n.reduce((
|
|
642
|
+
return n.reduce((t, e) => t + e, 0)
|
|
643
643
|
}
|
|
644
644
|
function average(n) {
|
|
645
|
-
return n.reduce((
|
|
645
|
+
return n.reduce((t, e) => t + e, 0) / n.length
|
|
646
646
|
}
|
|
647
647
|
function median(n) {
|
|
648
|
-
const
|
|
649
|
-
|
|
650
|
-
return
|
|
648
|
+
const t = [...n].sort((r, i) => r - i),
|
|
649
|
+
e = Math.floor(t.length / 2)
|
|
650
|
+
return t.length % 2 === 0 ? (t[e] + t[e - 1]) / 2 : t[e]
|
|
651
651
|
}
|
|
652
|
-
function getDistanceFromMidpoint(n,
|
|
653
|
-
return n - (
|
|
652
|
+
function getDistanceFromMidpoint(n, t) {
|
|
653
|
+
return n - (t - 1) / 2
|
|
654
654
|
}
|
|
655
|
-
function range(n,
|
|
656
|
-
const
|
|
657
|
-
for (let r = n; r <=
|
|
658
|
-
return
|
|
655
|
+
function range(n, t) {
|
|
656
|
+
const e = []
|
|
657
|
+
for (let r = n; r <= t; r++) e.push(r)
|
|
658
|
+
return e
|
|
659
659
|
}
|
|
660
|
-
function includesAny(n,
|
|
661
|
-
return
|
|
660
|
+
function includesAny(n, t) {
|
|
661
|
+
return t.some(e => n.includes(e))
|
|
662
662
|
}
|
|
663
663
|
function isChinese(n) {
|
|
664
664
|
return /^[\u4E00-\u9FA5]+$/.test(n)
|
|
665
665
|
}
|
|
666
|
-
function slugify(n,
|
|
666
|
+
function slugify(n, t = () => !1) {
|
|
667
667
|
return n
|
|
668
668
|
.toLowerCase()
|
|
669
669
|
.normalize('NFD')
|
|
670
670
|
.replace(/[\u0300-\u036f]/g, '')
|
|
671
671
|
.split('')
|
|
672
|
-
.map(
|
|
672
|
+
.map(e => (/[a-z0-9]/.test(e) || t(e) ? e : '-'))
|
|
673
673
|
.join('')
|
|
674
674
|
.replace(/-+/g, '-')
|
|
675
675
|
.replace(/^-|-$/g, '')
|
|
@@ -686,107 +686,107 @@ function slugToTitle(n) {
|
|
|
686
686
|
function slugToCamel(n) {
|
|
687
687
|
return decapitalize(n.split('-').map(capitalize).join(''))
|
|
688
688
|
}
|
|
689
|
-
function joinHumanly(n,
|
|
690
|
-
return !n || !n.length ? '' : n.length === 1 ? n[0] : n.length === 2 ? `${n[0]}${
|
|
689
|
+
function joinHumanly(n, t = ', ', e = ' and ') {
|
|
690
|
+
return !n || !n.length ? '' : n.length === 1 ? n[0] : n.length === 2 ? `${n[0]}${e}${n[1]}` : `${n.slice(0, n.length - 1).join(t)}${e}${n[n.length - 1]}`
|
|
691
691
|
}
|
|
692
|
-
function surroundInOut(n,
|
|
693
|
-
return
|
|
692
|
+
function surroundInOut(n, t) {
|
|
693
|
+
return t + n.split('').join(t) + t
|
|
694
694
|
}
|
|
695
695
|
function enumify(n) {
|
|
696
696
|
return slugify(n).replace(/-/g, '_').toUpperCase()
|
|
697
697
|
}
|
|
698
|
-
function getFuzzyMatchScore(n,
|
|
699
|
-
if (
|
|
700
|
-
const
|
|
701
|
-
r =
|
|
702
|
-
return n ===
|
|
698
|
+
function getFuzzyMatchScore(n, t) {
|
|
699
|
+
if (t.length === 0) return 0
|
|
700
|
+
const e = n.toLowerCase(),
|
|
701
|
+
r = t.toLowerCase()
|
|
702
|
+
return n === t ? 1e4 : e.startsWith(r) ? 1e4 - n.length : e.includes(r) ? 5e3 - n.length : new RegExp('.*' + r.split('').join('.*') + '.*').test(e) ? 1e3 - n.length : 0
|
|
703
703
|
}
|
|
704
|
-
function sortByFuzzyScore(n,
|
|
705
|
-
return n.filter(
|
|
704
|
+
function sortByFuzzyScore(n, t) {
|
|
705
|
+
return n.filter(e => getFuzzyMatchScore(e, t)).sort((e, r) => getFuzzyMatchScore(r, t) - getFuzzyMatchScore(e, t))
|
|
706
706
|
}
|
|
707
707
|
function escapeHtml(n) {
|
|
708
708
|
return n.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
|
|
709
709
|
}
|
|
710
710
|
const htmlEntityMap = { '&': '&', '"': '"', ''': "'", '>': '>', '<': '<' }
|
|
711
711
|
function decodeHtmlEntities(n) {
|
|
712
|
-
let
|
|
713
|
-
for (const [
|
|
714
|
-
return
|
|
712
|
+
let t = n.replace(/&#(\d+);/g, (e, r) => String.fromCharCode(r)).replace(/&#x(\d+);/g, (e, r) => String.fromCharCode(parseInt(r, 16)))
|
|
713
|
+
for (const [e, r] of Object.entries(htmlEntityMap)) t = t.replaceAll(e, r)
|
|
714
|
+
return t
|
|
715
715
|
}
|
|
716
|
-
function before(n,
|
|
717
|
-
const
|
|
718
|
-
return
|
|
716
|
+
function before(n, t) {
|
|
717
|
+
const e = n.indexOf(t)
|
|
718
|
+
return e === -1 ? null : n.slice(0, e)
|
|
719
719
|
}
|
|
720
|
-
function after(n,
|
|
721
|
-
const
|
|
722
|
-
return
|
|
720
|
+
function after(n, t) {
|
|
721
|
+
const e = n.indexOf(t)
|
|
722
|
+
return e === -1 ? null : n.slice(e + t.length)
|
|
723
723
|
}
|
|
724
|
-
function beforeLast(n,
|
|
725
|
-
const
|
|
726
|
-
return
|
|
724
|
+
function beforeLast(n, t) {
|
|
725
|
+
const e = n.lastIndexOf(t)
|
|
726
|
+
return e === -1 ? null : n.slice(0, e)
|
|
727
727
|
}
|
|
728
|
-
function afterLast(n,
|
|
729
|
-
const
|
|
730
|
-
return
|
|
728
|
+
function afterLast(n, t) {
|
|
729
|
+
const e = n.lastIndexOf(t)
|
|
730
|
+
return e === -1 ? null : n.slice(e + t.length)
|
|
731
731
|
}
|
|
732
|
-
function betweenWide(n,
|
|
733
|
-
const r = beforeLast(n,
|
|
734
|
-
return r ? after(r,
|
|
732
|
+
function betweenWide(n, t, e) {
|
|
733
|
+
const r = beforeLast(n, e)
|
|
734
|
+
return r ? after(r, t) : null
|
|
735
735
|
}
|
|
736
|
-
function betweenNarrow(n,
|
|
737
|
-
const r = after(n,
|
|
738
|
-
return r ? before(r,
|
|
736
|
+
function betweenNarrow(n, t, e) {
|
|
737
|
+
const r = after(n, t)
|
|
738
|
+
return r ? before(r, e) : null
|
|
739
739
|
}
|
|
740
|
-
function splitOnce(n,
|
|
741
|
-
const r =
|
|
742
|
-
return r === -1 ? (
|
|
740
|
+
function splitOnce(n, t, e = !1) {
|
|
741
|
+
const r = e ? n.lastIndexOf(t) : n.indexOf(t)
|
|
742
|
+
return r === -1 ? (e ? [null, n] : [n, null]) : [n.slice(0, r), n.slice(r + t.length)]
|
|
743
743
|
}
|
|
744
|
-
function splitAll(n,
|
|
745
|
-
let
|
|
746
|
-
for (const r of
|
|
747
|
-
return
|
|
744
|
+
function splitAll(n, t) {
|
|
745
|
+
let e = [n]
|
|
746
|
+
for (const r of t) e = e.flatMap(i => i.split(r))
|
|
747
|
+
return e.filter(r => r)
|
|
748
748
|
}
|
|
749
749
|
function getExtension(n) {
|
|
750
|
-
const
|
|
751
|
-
|
|
752
|
-
return
|
|
750
|
+
const t = last(n.split(/\\|\//g)),
|
|
751
|
+
e = t.lastIndexOf('.', t.length - 1)
|
|
752
|
+
return e <= 0 ? '' : t.slice(e + 1)
|
|
753
753
|
}
|
|
754
754
|
function getBasename(n) {
|
|
755
|
-
const
|
|
756
|
-
|
|
757
|
-
return
|
|
755
|
+
const t = last(n.split(/\\|\//g)),
|
|
756
|
+
e = t.lastIndexOf('.', t.length - 1)
|
|
757
|
+
return e <= 0 ? t : t.slice(0, e)
|
|
758
758
|
}
|
|
759
759
|
function normalizeEmail(n) {
|
|
760
|
-
let [
|
|
761
|
-
|
|
762
|
-
const [r] =
|
|
763
|
-
if (!r || !
|
|
764
|
-
return (
|
|
760
|
+
let [t, e] = n.split('@')
|
|
761
|
+
t = shrinkTrim(t.replaceAll('.', '').toLowerCase()).replaceAll(' ', '')
|
|
762
|
+
const [r] = t.split('+')
|
|
763
|
+
if (!r || !e || e.indexOf('.') === -1 || e.indexOf('.') === e.length - 1) throw new Error('Invalid email')
|
|
764
|
+
return (e = shrinkTrim(e.toLowerCase()).replaceAll(' ', '')), `${r}@${e}`
|
|
765
765
|
}
|
|
766
766
|
function normalizeFilename(n) {
|
|
767
|
-
const
|
|
768
|
-
|
|
769
|
-
return
|
|
767
|
+
const t = getBasename(n),
|
|
768
|
+
e = getExtension(n)
|
|
769
|
+
return e ? `${t}.${e}` : t
|
|
770
770
|
}
|
|
771
771
|
function parseFilename(n) {
|
|
772
|
-
const
|
|
773
|
-
|
|
774
|
-
return { basename:
|
|
772
|
+
const t = getBasename(n),
|
|
773
|
+
e = getExtension(n)
|
|
774
|
+
return { basename: t, extension: e, filename: e ? `${t}.${e}` : t }
|
|
775
775
|
}
|
|
776
|
-
function randomize(n,
|
|
777
|
-
return n.replace(/\{(.+?)\}/g, (
|
|
776
|
+
function randomize(n, t = Math.random) {
|
|
777
|
+
return n.replace(/\{(.+?)\}/g, (e, r) => pick(r.split('|'), t))
|
|
778
778
|
}
|
|
779
779
|
function expand(n) {
|
|
780
|
-
const
|
|
781
|
-
|
|
782
|
-
if (!
|
|
783
|
-
const r =
|
|
784
|
-
i = n.slice(0,
|
|
785
|
-
o = n.slice(
|
|
780
|
+
const t = /\{(.+?)\}/,
|
|
781
|
+
e = n.match(t)
|
|
782
|
+
if (!e || !e.index) return [n]
|
|
783
|
+
const r = e[1].split(','),
|
|
784
|
+
i = n.slice(0, e.index),
|
|
785
|
+
o = n.slice(e.index + e[0].length)
|
|
786
786
|
let c = []
|
|
787
|
-
for (const
|
|
788
|
-
const
|
|
789
|
-
c = c.concat(
|
|
787
|
+
for (const s of r) {
|
|
788
|
+
const u = expand(i + s + o)
|
|
789
|
+
c = c.concat(u)
|
|
790
790
|
}
|
|
791
791
|
return c
|
|
792
792
|
}
|
|
@@ -796,8 +796,8 @@ function shrinkTrim(n) {
|
|
|
796
796
|
`
|
|
797
797
|
`
|
|
798
798
|
)
|
|
799
|
-
.map(
|
|
800
|
-
.filter(
|
|
799
|
+
.map(t => t.trim().replace(/\s+/g, ' '))
|
|
800
|
+
.filter(t => t.length > 0).join(`
|
|
801
801
|
`)
|
|
802
802
|
}
|
|
803
803
|
function capitalize(n) {
|
|
@@ -808,13 +808,13 @@ function decapitalize(n) {
|
|
|
808
808
|
}
|
|
809
809
|
function isLetter(n) {
|
|
810
810
|
if (!n) return !1
|
|
811
|
-
const
|
|
812
|
-
return (
|
|
811
|
+
const t = n.charCodeAt(0)
|
|
812
|
+
return (t >= 65 && t <= 90) || (t >= 97 && t <= 122)
|
|
813
813
|
}
|
|
814
814
|
function isDigit(n) {
|
|
815
815
|
if (!n) return !1
|
|
816
|
-
const
|
|
817
|
-
return
|
|
816
|
+
const t = n.charCodeAt(0)
|
|
817
|
+
return t >= 48 && t <= 57
|
|
818
818
|
}
|
|
819
819
|
function isLetterOrDigit(n) {
|
|
820
820
|
return isLetter(n) || isDigit(n)
|
|
@@ -827,17 +827,17 @@ function isWordBreakCharacter(n) {
|
|
|
827
827
|
function isValidObjectPathCharacter(n) {
|
|
828
828
|
return isLetterOrDigit(n) || n === '.' || n === '[' || n === ']' || n === '_'
|
|
829
829
|
}
|
|
830
|
-
function insertString(n,
|
|
831
|
-
return n.slice(0,
|
|
830
|
+
function insertString(n, t, e, r, i) {
|
|
831
|
+
return n.slice(0, t) + r + n.slice(t, t + e) + i + n.slice(t + e)
|
|
832
832
|
}
|
|
833
|
-
function indexOfRegex(n,
|
|
834
|
-
const r =
|
|
833
|
+
function indexOfRegex(n, t, e = 0) {
|
|
834
|
+
const r = t.exec(n.slice(e))
|
|
835
835
|
return r ? { index: r.index, match: r[0] } : null
|
|
836
836
|
}
|
|
837
|
-
function lineMatches(n,
|
|
838
|
-
if (!
|
|
837
|
+
function lineMatches(n, t, e = !0) {
|
|
838
|
+
if (!e) return t.every(i => (i instanceof RegExp ? i.test(n) : n.indexOf(i, 0) !== -1))
|
|
839
839
|
let r = 0
|
|
840
|
-
for (const i of
|
|
840
|
+
for (const i of t)
|
|
841
841
|
if (i instanceof RegExp) {
|
|
842
842
|
const o = indexOfRegex(n, i, r)
|
|
843
843
|
if (!o) return !1
|
|
@@ -849,11 +849,11 @@ function lineMatches(n, e, t = !0) {
|
|
|
849
849
|
}
|
|
850
850
|
return !0
|
|
851
851
|
}
|
|
852
|
-
function linesMatchInOrder(n,
|
|
852
|
+
function linesMatchInOrder(n, t, e = !0) {
|
|
853
853
|
let r = 0
|
|
854
|
-
for (const i of
|
|
854
|
+
for (const i of t) {
|
|
855
855
|
let o = !1
|
|
856
|
-
for (; !o && r < n.length; ) lineMatches(n[r], i,
|
|
856
|
+
for (; !o && r < n.length; ) lineMatches(n[r], i, e) && (o = !0), r++
|
|
857
857
|
if (!o) return !1
|
|
858
858
|
}
|
|
859
859
|
return !0
|
|
@@ -861,138 +861,138 @@ function linesMatchInOrder(n, e, t = !0) {
|
|
|
861
861
|
function csvEscape(n) {
|
|
862
862
|
return n.match(/"|,/) ? `"${n.replace(/"/g, '""')}"` : n
|
|
863
863
|
}
|
|
864
|
-
function allIndexOf(n,
|
|
864
|
+
function allIndexOf(n, t, e = 0) {
|
|
865
865
|
const r = []
|
|
866
|
-
let i = n.indexOf(
|
|
867
|
-
for (; i !== -1; ) r.push(i), (i = n.indexOf(
|
|
866
|
+
let i = n.indexOf(t, e)
|
|
867
|
+
for (; i !== -1; ) r.push(i), (i = n.indexOf(t, i + t.length))
|
|
868
868
|
return r
|
|
869
869
|
}
|
|
870
|
-
function indexOfEarliest(n,
|
|
870
|
+
function indexOfEarliest(n, t, e = 0) {
|
|
871
871
|
let r = -1
|
|
872
|
-
for (const i of
|
|
873
|
-
const o = n.indexOf(i,
|
|
872
|
+
for (const i of t) {
|
|
873
|
+
const o = n.indexOf(i, e)
|
|
874
874
|
o !== -1 && (r === -1 || o < r) && (r = o)
|
|
875
875
|
}
|
|
876
876
|
return r
|
|
877
877
|
}
|
|
878
|
-
function indexOfWordBreak(n,
|
|
879
|
-
for (let
|
|
878
|
+
function indexOfWordBreak(n, t = 0) {
|
|
879
|
+
for (let e = t; e < n.length; e++) if (isWordBreakCharacter(n[e])) return e
|
|
880
880
|
return -1
|
|
881
881
|
}
|
|
882
|
-
function indexOfHashtag(n,
|
|
883
|
-
for (let
|
|
882
|
+
function indexOfHashtag(n, t = 0) {
|
|
883
|
+
for (let e = t; e < n.length; e++) if (n[e] === '#' && isLetterOrDigit(n[e + 1])) return e
|
|
884
884
|
return -1
|
|
885
885
|
}
|
|
886
|
-
function lastIndexOfBefore(n,
|
|
887
|
-
return n.slice(0,
|
|
886
|
+
function lastIndexOfBefore(n, t, e = 0) {
|
|
887
|
+
return n.slice(0, e).lastIndexOf(t)
|
|
888
888
|
}
|
|
889
|
-
function findWeightedPair(n,
|
|
889
|
+
function findWeightedPair(n, t = 0, e = '{', r = '}') {
|
|
890
890
|
let i = 1
|
|
891
|
-
for (let o =
|
|
891
|
+
for (let o = t; o < n.length; o++)
|
|
892
892
|
if (n.slice(o, o + r.length) === r) {
|
|
893
893
|
if (--i === 0) return o
|
|
894
|
-
} else n.slice(o, o +
|
|
894
|
+
} else n.slice(o, o + e.length) === e && i++
|
|
895
895
|
return -1
|
|
896
896
|
}
|
|
897
|
-
function extractBlock(n,
|
|
898
|
-
const
|
|
897
|
+
function extractBlock(n, t) {
|
|
898
|
+
const e = t.wordBoundary
|
|
899
899
|
? indexOfEarliest(
|
|
900
900
|
n,
|
|
901
901
|
[
|
|
902
|
-
`${
|
|
903
|
-
`${
|
|
902
|
+
`${t.opening} `,
|
|
903
|
+
`${t.opening}
|
|
904
904
|
`
|
|
905
905
|
],
|
|
906
|
-
|
|
906
|
+
t.start || 0
|
|
907
907
|
)
|
|
908
|
-
: n.indexOf(
|
|
909
|
-
if (
|
|
910
|
-
const r = findWeightedPair(n,
|
|
911
|
-
return r === -1 ? null :
|
|
908
|
+
: n.indexOf(t.opening, t.start || 0)
|
|
909
|
+
if (e === -1) return null
|
|
910
|
+
const r = findWeightedPair(n, e + t.opening.length, t.opening, t.closing)
|
|
911
|
+
return r === -1 ? null : t.exclusive ? n.slice(e + t.opening.length, r) : n.slice(e, r + t.closing.length)
|
|
912
912
|
}
|
|
913
|
-
function extractAllBlocks(n,
|
|
914
|
-
const
|
|
915
|
-
let r =
|
|
913
|
+
function extractAllBlocks(n, t) {
|
|
914
|
+
const e = []
|
|
915
|
+
let r = t.wordBoundary
|
|
916
916
|
? indexOfEarliest(
|
|
917
917
|
n,
|
|
918
918
|
[
|
|
919
|
-
`${
|
|
920
|
-
`${
|
|
919
|
+
`${t.opening} `,
|
|
920
|
+
`${t.opening}
|
|
921
921
|
`
|
|
922
922
|
],
|
|
923
|
-
|
|
923
|
+
t.start || 0
|
|
924
924
|
)
|
|
925
|
-
: n.indexOf(
|
|
925
|
+
: n.indexOf(t.opening, t.start || 0)
|
|
926
926
|
for (;;) {
|
|
927
|
-
if (r === -1) return
|
|
928
|
-
const i = extractBlock(n, { ...
|
|
929
|
-
if (!i) return
|
|
930
|
-
|
|
931
|
-
(r =
|
|
927
|
+
if (r === -1) return e
|
|
928
|
+
const i = extractBlock(n, { ...t, start: r })
|
|
929
|
+
if (!i) return e
|
|
930
|
+
e.push(i),
|
|
931
|
+
(r = t.wordBoundary
|
|
932
932
|
? indexOfEarliest(
|
|
933
933
|
n,
|
|
934
934
|
[
|
|
935
|
-
`${
|
|
936
|
-
`${
|
|
935
|
+
`${t.opening} `,
|
|
936
|
+
`${t.opening}
|
|
937
937
|
`
|
|
938
938
|
],
|
|
939
939
|
r + i.length
|
|
940
940
|
)
|
|
941
|
-
: n.indexOf(
|
|
941
|
+
: n.indexOf(t.opening, r + i.length))
|
|
942
942
|
}
|
|
943
943
|
}
|
|
944
|
-
function replaceBlocks(n,
|
|
944
|
+
function replaceBlocks(n, t, e) {
|
|
945
945
|
let r = 0
|
|
946
946
|
for (;;) {
|
|
947
|
-
const i = exports.Strings.extractBlock(n, { ...
|
|
947
|
+
const i = exports.Strings.extractBlock(n, { ...e, start: r })
|
|
948
948
|
if (!i) return n
|
|
949
|
-
const o =
|
|
949
|
+
const o = t(i)
|
|
950
950
|
;(r = n.indexOf(i, r) + o.length), (n = n.replace(i, o))
|
|
951
951
|
}
|
|
952
952
|
}
|
|
953
|
-
function splitFormatting(n,
|
|
954
|
-
const
|
|
953
|
+
function splitFormatting(n, t) {
|
|
954
|
+
const e = []
|
|
955
955
|
let r = 0
|
|
956
956
|
for (; r < n.length; ) {
|
|
957
|
-
const i = n.indexOf(
|
|
957
|
+
const i = n.indexOf(t, r)
|
|
958
958
|
if (i === -1) {
|
|
959
|
-
|
|
959
|
+
e.push({ string: n.slice(r), symbol: null })
|
|
960
960
|
break
|
|
961
961
|
}
|
|
962
|
-
const o = n.indexOf(
|
|
963
|
-
if ((i > r && o !== -1 &&
|
|
964
|
-
|
|
962
|
+
const o = n.indexOf(t, i + t.length)
|
|
963
|
+
if ((i > r && o !== -1 && e.push({ string: n.slice(r, i), symbol: null }), o === -1)) {
|
|
964
|
+
e.push({ string: n.slice(r), symbol: null })
|
|
965
965
|
break
|
|
966
966
|
}
|
|
967
|
-
|
|
967
|
+
e.push({ string: n.slice(i + t.length, o), symbol: t }), (r = o + t.length)
|
|
968
968
|
}
|
|
969
|
-
return
|
|
969
|
+
return e
|
|
970
970
|
}
|
|
971
971
|
function splitHashtags(n) {
|
|
972
|
-
const
|
|
973
|
-
let
|
|
974
|
-
for (;
|
|
975
|
-
const r = indexOfHashtag(n,
|
|
972
|
+
const t = []
|
|
973
|
+
let e = 0
|
|
974
|
+
for (; e < n.length; ) {
|
|
975
|
+
const r = indexOfHashtag(n, e)
|
|
976
976
|
if (r === -1) {
|
|
977
|
-
|
|
977
|
+
t.push({ string: n.slice(e), symbol: null })
|
|
978
978
|
break
|
|
979
979
|
}
|
|
980
980
|
const i = indexOfWordBreak(n, r + 1)
|
|
981
981
|
if (i === -1) {
|
|
982
|
-
|
|
982
|
+
t.push({ string: n.slice(e, r), symbol: null }), t.push({ string: n.slice(r + 1), symbol: '#' })
|
|
983
983
|
break
|
|
984
984
|
}
|
|
985
|
-
r >
|
|
985
|
+
r > e && t.push({ string: n.slice(e, r), symbol: null }), t.push({ string: n.slice(r + 1, i), symbol: '#' }), (e = i)
|
|
986
986
|
}
|
|
987
|
-
return
|
|
987
|
+
return t
|
|
988
988
|
}
|
|
989
989
|
function splitUrls(n) {
|
|
990
|
-
const
|
|
991
|
-
let
|
|
992
|
-
for (;
|
|
993
|
-
const r = indexOfEarliest(n, ['http://', 'https://'],
|
|
990
|
+
const t = []
|
|
991
|
+
let e = 0
|
|
992
|
+
for (; e < n.length; ) {
|
|
993
|
+
const r = indexOfEarliest(n, ['http://', 'https://'], e)
|
|
994
994
|
if (r === -1) {
|
|
995
|
-
|
|
995
|
+
t.push({ string: n.slice(e), symbol: null })
|
|
996
996
|
break
|
|
997
997
|
}
|
|
998
998
|
const i = indexOfEarliest(
|
|
@@ -1005,12 +1005,12 @@ function splitUrls(n) {
|
|
|
1005
1005
|
r
|
|
1006
1006
|
)
|
|
1007
1007
|
if (i === -1) {
|
|
1008
|
-
r >
|
|
1008
|
+
r > e && t.push({ string: n.slice(e, r), symbol: null }), t.push({ string: n.slice(r), symbol: 'http' })
|
|
1009
1009
|
break
|
|
1010
1010
|
}
|
|
1011
|
-
r >
|
|
1011
|
+
r > e && t.push({ string: n.slice(e, r), symbol: null }), t.push({ string: n.slice(r, i), symbol: 'http' }), (e = i)
|
|
1012
1012
|
}
|
|
1013
|
-
return
|
|
1013
|
+
return t
|
|
1014
1014
|
}
|
|
1015
1015
|
const BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
|
1016
1016
|
BASE32_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
|
|
@@ -1026,80 +1026,80 @@ function base32ToUint8Array(n) {
|
|
|
1026
1026
|
function uint8ArrayToBase32(n) {
|
|
1027
1027
|
return uint8ArrayToBase(n, BASE32_CHARS)
|
|
1028
1028
|
}
|
|
1029
|
-
function baseToUint8Array(n,
|
|
1030
|
-
const
|
|
1031
|
-
r =
|
|
1029
|
+
function baseToUint8Array(n, t) {
|
|
1030
|
+
const e = '=',
|
|
1031
|
+
r = t.length
|
|
1032
1032
|
let i = 0,
|
|
1033
1033
|
o = 0
|
|
1034
1034
|
const c = []
|
|
1035
|
-
for (let
|
|
1036
|
-
const
|
|
1037
|
-
if (
|
|
1038
|
-
const f =
|
|
1039
|
-
if (f === -1) throw new Error(`Invalid character: ${
|
|
1035
|
+
for (let s = 0; s < n.length; s++) {
|
|
1036
|
+
const u = n[s]
|
|
1037
|
+
if (u === e) break
|
|
1038
|
+
const f = t.indexOf(u)
|
|
1039
|
+
if (f === -1) throw new Error(`Invalid character: ${u}`)
|
|
1040
1040
|
;(o = (o << Math.log2(r)) | f), (i += Math.log2(r)), i >= 8 && ((i -= 8), c.push((o >> i) & 255))
|
|
1041
1041
|
}
|
|
1042
1042
|
return new Uint8Array(c)
|
|
1043
1043
|
}
|
|
1044
|
-
function uint8ArrayToBase(n,
|
|
1045
|
-
const
|
|
1044
|
+
function uint8ArrayToBase(n, t) {
|
|
1045
|
+
const e = t.length
|
|
1046
1046
|
let r = 0,
|
|
1047
1047
|
i = 0,
|
|
1048
1048
|
o = ''
|
|
1049
|
-
for (let c = 0; c < n.length; c++) for (i = (i << 8) | n[c], r += 8; r >= Math.log2(
|
|
1050
|
-
return r > 0 && (o +=
|
|
1049
|
+
for (let c = 0; c < n.length; c++) for (i = (i << 8) | n[c], r += 8; r >= Math.log2(e); ) (r -= Math.log2(e)), (o += t[(i >> r) & (e - 1)])
|
|
1050
|
+
return r > 0 && (o += t[(i << (Math.log2(e) - r)) & (e - 1)]), o.length % 4 !== 0 && (o += '='.repeat(4 - (o.length % 4))), o
|
|
1051
1051
|
}
|
|
1052
1052
|
function hexToUint8Array(n) {
|
|
1053
1053
|
;(n.startsWith('0x') || n.startsWith('0X')) && (n = n.slice(2))
|
|
1054
|
-
const
|
|
1055
|
-
|
|
1056
|
-
for (let r = 0; r <
|
|
1054
|
+
const t = n.length / 2,
|
|
1055
|
+
e = new Uint8Array(t)
|
|
1056
|
+
for (let r = 0; r < t; r++) {
|
|
1057
1057
|
const i = parseInt(n.slice(r * 2, r * 2 + 2), 16)
|
|
1058
|
-
|
|
1058
|
+
e[r] = i
|
|
1059
1059
|
}
|
|
1060
|
-
return
|
|
1060
|
+
return e
|
|
1061
1061
|
}
|
|
1062
1062
|
function uint8ArrayToHex(n) {
|
|
1063
1063
|
return Array.from(n)
|
|
1064
|
-
.map(
|
|
1064
|
+
.map(t => t.toString(16).padStart(2, '0'))
|
|
1065
1065
|
.join('')
|
|
1066
1066
|
}
|
|
1067
1067
|
function uint8ArrayToBinary(n) {
|
|
1068
1068
|
return Array.from(n)
|
|
1069
|
-
.map(
|
|
1069
|
+
.map(t => t.toString(2).padStart(8, '0'))
|
|
1070
1070
|
.join('')
|
|
1071
1071
|
}
|
|
1072
1072
|
function binaryToUint8Array(n) {
|
|
1073
|
-
const
|
|
1074
|
-
|
|
1075
|
-
for (let r = 0; r <
|
|
1073
|
+
const t = Math.ceil(n.length / 8),
|
|
1074
|
+
e = new Uint8Array(t)
|
|
1075
|
+
for (let r = 0; r < t; r++) {
|
|
1076
1076
|
const i = parseInt(n.slice(r * 8, r * 8 + 8), 2)
|
|
1077
|
-
|
|
1077
|
+
e[r] = i
|
|
1078
1078
|
}
|
|
1079
|
-
return
|
|
1079
|
+
return e
|
|
1080
1080
|
}
|
|
1081
|
-
function route(n,
|
|
1082
|
-
const
|
|
1083
|
-
r =
|
|
1084
|
-
if (
|
|
1081
|
+
function route(n, t) {
|
|
1082
|
+
const e = n.split('/').filter(o => o),
|
|
1083
|
+
r = t.split('/').filter(o => o)
|
|
1084
|
+
if (e.length !== r.length) return null
|
|
1085
1085
|
const i = {}
|
|
1086
|
-
for (let o = 0; o <
|
|
1087
|
-
const c =
|
|
1086
|
+
for (let o = 0; o < e.length; o++) {
|
|
1087
|
+
const c = e[o]
|
|
1088
1088
|
if (c.startsWith(':')) i[c.slice(1)] = r[o]
|
|
1089
1089
|
else if (c !== r[o]) return null
|
|
1090
1090
|
}
|
|
1091
1091
|
return i
|
|
1092
1092
|
}
|
|
1093
|
-
function explodeReplace(n,
|
|
1093
|
+
function explodeReplace(n, t, e) {
|
|
1094
1094
|
const r = []
|
|
1095
|
-
for (const i of
|
|
1095
|
+
for (const i of e) i !== t && r.push(n.replace(t, i))
|
|
1096
1096
|
return r
|
|
1097
1097
|
}
|
|
1098
|
-
function generateVariants(n,
|
|
1098
|
+
function generateVariants(n, t, e, r = Math.random) {
|
|
1099
1099
|
const i = exports.Arrays.shuffle(
|
|
1100
|
-
|
|
1100
|
+
t.map(c => ({
|
|
1101
1101
|
variants: exports.Arrays.shuffle(
|
|
1102
|
-
c.variants.map(
|
|
1102
|
+
c.variants.map(s => s),
|
|
1103
1103
|
r
|
|
1104
1104
|
),
|
|
1105
1105
|
avoid: c.avoid
|
|
@@ -1108,241 +1108,241 @@ function generateVariants(n, e, t, r = Math.random) {
|
|
|
1108
1108
|
),
|
|
1109
1109
|
o = []
|
|
1110
1110
|
for (const c of i) {
|
|
1111
|
-
const
|
|
1112
|
-
|
|
1113
|
-
if (
|
|
1111
|
+
const s = c.variants.filter(f => f !== c.avoid),
|
|
1112
|
+
u = s.find(f => n.includes(f))
|
|
1113
|
+
if (u && (pushAll(o, explodeReplace(n, u, s)), o.length >= e)) break
|
|
1114
1114
|
}
|
|
1115
|
-
if (o.length <
|
|
1115
|
+
if (o.length < e)
|
|
1116
1116
|
for (const c of i) {
|
|
1117
|
-
const
|
|
1118
|
-
if (
|
|
1117
|
+
const s = c.variants.find(u => n.includes(u))
|
|
1118
|
+
if (s && (pushAll(o, explodeReplace(n, s, c.variants)), o.length >= e)) break
|
|
1119
1119
|
}
|
|
1120
|
-
return o.slice(0,
|
|
1120
|
+
return o.slice(0, e)
|
|
1121
1121
|
}
|
|
1122
|
-
function replaceWord(n,
|
|
1123
|
-
const i = new RegExp(r ? `(?<=\\s|^)${
|
|
1124
|
-
return n.replace(i,
|
|
1122
|
+
function replaceWord(n, t, e, r = !1) {
|
|
1123
|
+
const i = new RegExp(r ? `(?<=\\s|^)${t}(?=\\s|$)` : `\\b${t}\\b`, 'g')
|
|
1124
|
+
return n.replace(i, e)
|
|
1125
1125
|
}
|
|
1126
|
-
function replacePascalCaseWords(n,
|
|
1127
|
-
const
|
|
1128
|
-
return n.replace(
|
|
1126
|
+
function replacePascalCaseWords(n, t) {
|
|
1127
|
+
const e = /\b[A-Z][a-zA-Z0-9]*\b/g
|
|
1128
|
+
return n.replace(e, r => (r.toUpperCase() === r ? r : t(r)))
|
|
1129
1129
|
}
|
|
1130
1130
|
function stripHtml(n) {
|
|
1131
1131
|
return n.replace(/<[^>]*>/g, '')
|
|
1132
1132
|
}
|
|
1133
1133
|
function breakLine(n) {
|
|
1134
|
-
const
|
|
1135
|
-
if (
|
|
1136
|
-
const
|
|
1137
|
-
r = n.slice(
|
|
1138
|
-
return { line:
|
|
1134
|
+
const t = n.lastIndexOf(' ')
|
|
1135
|
+
if (t === -1) return { line: n, rest: '' }
|
|
1136
|
+
const e = n.slice(0, t),
|
|
1137
|
+
r = n.slice(t + 1)
|
|
1138
|
+
return { line: e, rest: r }
|
|
1139
1139
|
}
|
|
1140
|
-
function measureTextWidth(n,
|
|
1141
|
-
return [...n].reduce((
|
|
1140
|
+
function measureTextWidth(n, t = {}) {
|
|
1141
|
+
return [...n].reduce((e, r) => e + (t[r] || 1), 0)
|
|
1142
1142
|
}
|
|
1143
|
-
function toLines(n,
|
|
1143
|
+
function toLines(n, t, e = {}) {
|
|
1144
1144
|
const r = []
|
|
1145
1145
|
let i = '',
|
|
1146
1146
|
o = 0
|
|
1147
1147
|
for (let c = 0; c < n.length; c++) {
|
|
1148
|
-
const
|
|
1149
|
-
|
|
1150
|
-
if (((i +=
|
|
1148
|
+
const s = n[c],
|
|
1149
|
+
u = e[s] || 1
|
|
1150
|
+
if (((i += s), (o += u), o > t)) {
|
|
1151
1151
|
const { line: f, rest: l } = breakLine(i)
|
|
1152
1152
|
r.push(f),
|
|
1153
1153
|
(i = l),
|
|
1154
1154
|
(o = l
|
|
1155
1155
|
.split('')
|
|
1156
|
-
.map(a =>
|
|
1156
|
+
.map(a => e[a] || 1)
|
|
1157
1157
|
.reduce((a, h) => a + h, 0))
|
|
1158
1158
|
}
|
|
1159
1159
|
}
|
|
1160
1160
|
return i && r.push(i), r
|
|
1161
1161
|
}
|
|
1162
|
-
function levenshteinDistance(n,
|
|
1163
|
-
const
|
|
1164
|
-
for (let r = 0; r <= n.length; r++)
|
|
1165
|
-
for (let r = 0; r <=
|
|
1162
|
+
function levenshteinDistance(n, t) {
|
|
1163
|
+
const e = []
|
|
1164
|
+
for (let r = 0; r <= n.length; r++) e[r] = [r]
|
|
1165
|
+
for (let r = 0; r <= t.length; r++) e[0][r] = r
|
|
1166
1166
|
for (let r = 1; r <= n.length; r++)
|
|
1167
|
-
for (let i = 1; i <=
|
|
1168
|
-
const o = n[r - 1] ===
|
|
1169
|
-
|
|
1167
|
+
for (let i = 1; i <= t.length; i++) {
|
|
1168
|
+
const o = n[r - 1] === t[i - 1] ? 0 : 1
|
|
1169
|
+
e[r][i] = Math.min(e[r - 1][i] + 1, e[r][i - 1] + 1, e[r - 1][i - 1] + o)
|
|
1170
1170
|
}
|
|
1171
|
-
return
|
|
1171
|
+
return e[n.length][t.length]
|
|
1172
1172
|
}
|
|
1173
1173
|
function findCommonPrefix(n) {
|
|
1174
|
-
const
|
|
1175
|
-
let
|
|
1176
|
-
for (let r = 0; r <
|
|
1177
|
-
const i =
|
|
1178
|
-
if (n.every(o => o[r] === i))
|
|
1174
|
+
const t = n.reduce((r, i) => (r.length < i.length ? r : i))
|
|
1175
|
+
let e = ''
|
|
1176
|
+
for (let r = 0; r < t.length; r++) {
|
|
1177
|
+
const i = t[r]
|
|
1178
|
+
if (n.every(o => o[r] === i)) e += i
|
|
1179
1179
|
else break
|
|
1180
1180
|
}
|
|
1181
|
-
return
|
|
1181
|
+
return e
|
|
1182
1182
|
}
|
|
1183
1183
|
function findCommonDirectory(n) {
|
|
1184
|
-
const
|
|
1185
|
-
|
|
1186
|
-
return
|
|
1184
|
+
const t = findCommonPrefix(n),
|
|
1185
|
+
e = t.lastIndexOf('/')
|
|
1186
|
+
return e === -1 ? '' : t.slice(0, e + 1)
|
|
1187
1187
|
}
|
|
1188
|
-
function containsWord(n,
|
|
1189
|
-
return new RegExp(`\\b${
|
|
1188
|
+
function containsWord(n, t) {
|
|
1189
|
+
return new RegExp(`\\b${t}\\b`).test(n)
|
|
1190
1190
|
}
|
|
1191
|
-
function containsWords(n,
|
|
1192
|
-
return
|
|
1191
|
+
function containsWords(n, t, e) {
|
|
1192
|
+
return e === 'any' ? t.some(r => containsWord(n, r)) : t.every(r => containsWord(n, r))
|
|
1193
1193
|
}
|
|
1194
1194
|
function parseHtmlAttributes(n) {
|
|
1195
|
-
const
|
|
1196
|
-
|
|
1197
|
-
if (
|
|
1198
|
-
for (const r of
|
|
1195
|
+
const t = {},
|
|
1196
|
+
e = n.match(/([a-z\-]+)="([^"]+)"/g)
|
|
1197
|
+
if (e)
|
|
1198
|
+
for (const r of e) {
|
|
1199
1199
|
const [i, o] = splitOnce(r, '=')
|
|
1200
|
-
|
|
1200
|
+
t[i] = o.slice(1, o.length - 1)
|
|
1201
1201
|
}
|
|
1202
|
-
return
|
|
1202
|
+
return t
|
|
1203
1203
|
}
|
|
1204
|
-
function readNextWord(n,
|
|
1204
|
+
function readNextWord(n, t, e = []) {
|
|
1205
1205
|
let r = ''
|
|
1206
|
-
for (;
|
|
1206
|
+
for (; t < n.length && (isLetterOrDigit(n[t]) || e.includes(n[t])); ) r += n[t++]
|
|
1207
1207
|
return r
|
|
1208
1208
|
}
|
|
1209
|
-
function readWordsAfterAll(n,
|
|
1210
|
-
const r = allIndexOf(n,
|
|
1209
|
+
function readWordsAfterAll(n, t, e = []) {
|
|
1210
|
+
const r = allIndexOf(n, t),
|
|
1211
1211
|
i = []
|
|
1212
|
-
for (const o of r) i.push(readNextWord(n, o +
|
|
1212
|
+
for (const o of r) i.push(readNextWord(n, o + t.length, e))
|
|
1213
1213
|
return i
|
|
1214
1214
|
}
|
|
1215
|
-
function resolveVariables(n,
|
|
1216
|
-
for (const i in
|
|
1215
|
+
function resolveVariables(n, t, e = '$', r = ':') {
|
|
1216
|
+
for (const i in t) n = resolveVariableWithDefaultSyntax(n, i, t[i], e, r)
|
|
1217
1217
|
return (n = resolveRemainingVariablesWithDefaults(n)), n
|
|
1218
1218
|
}
|
|
1219
|
-
function resolveVariableWithDefaultSyntax(n,
|
|
1220
|
-
if (
|
|
1221
|
-
let o = n.indexOf(`${r}${
|
|
1219
|
+
function resolveVariableWithDefaultSyntax(n, t, e, r = '$', i = ':') {
|
|
1220
|
+
if (e === '') return n
|
|
1221
|
+
let o = n.indexOf(`${r}${t}`)
|
|
1222
1222
|
for (; o !== -1; ) {
|
|
1223
|
-
if (n[o +
|
|
1224
|
-
if (n[o +
|
|
1223
|
+
if (n[o + t.length + 1] === i)
|
|
1224
|
+
if (n[o + t.length + 2] === i) n = n.replace(`${r}${t}${i}${i}`, e)
|
|
1225
1225
|
else {
|
|
1226
|
-
const
|
|
1227
|
-
n = n.replace(`${r}${
|
|
1226
|
+
const s = readNextWord(n, o + t.length + 2, ['_'])
|
|
1227
|
+
n = n.replace(`${r}${t}${i}${s}`, e)
|
|
1228
1228
|
}
|
|
1229
|
-
else n = n.replace(`${r}${
|
|
1230
|
-
o = n.indexOf(`${r}${
|
|
1229
|
+
else n = n.replace(`${r}${t}`, e)
|
|
1230
|
+
o = n.indexOf(`${r}${t}`, o + e.length)
|
|
1231
1231
|
}
|
|
1232
1232
|
return n
|
|
1233
1233
|
}
|
|
1234
|
-
function resolveRemainingVariablesWithDefaults(n,
|
|
1235
|
-
let r = n.indexOf(
|
|
1234
|
+
function resolveRemainingVariablesWithDefaults(n, t = '$', e = ':') {
|
|
1235
|
+
let r = n.indexOf(t)
|
|
1236
1236
|
for (; r !== -1; ) {
|
|
1237
1237
|
const i = readNextWord(n, r + 1)
|
|
1238
|
-
if (n[r + i.length + 1] ===
|
|
1239
|
-
if (n[r + i.length + 2] ===
|
|
1238
|
+
if (n[r + i.length + 1] === e)
|
|
1239
|
+
if (n[r + i.length + 2] === e) n = n.replace(`${t}${i}${e}${e}`, '')
|
|
1240
1240
|
else {
|
|
1241
1241
|
const c = readNextWord(n, r + i.length + 2)
|
|
1242
|
-
n = n.replace(`${
|
|
1242
|
+
n = n.replace(`${t}${i}${e}${c}`, c)
|
|
1243
1243
|
}
|
|
1244
|
-
r = n.indexOf(
|
|
1244
|
+
r = n.indexOf(t, r + 1)
|
|
1245
1245
|
}
|
|
1246
1246
|
return n
|
|
1247
1247
|
}
|
|
1248
|
-
function resolveMarkdownLinks(n,
|
|
1249
|
-
let
|
|
1250
|
-
for (;
|
|
1251
|
-
const r = lastIndexOfBefore(n, '[',
|
|
1252
|
-
i = n.indexOf(')',
|
|
1248
|
+
function resolveMarkdownLinks(n, t) {
|
|
1249
|
+
let e = n.indexOf('](')
|
|
1250
|
+
for (; e !== -1; ) {
|
|
1251
|
+
const r = lastIndexOfBefore(n, '[', e),
|
|
1252
|
+
i = n.indexOf(')', e)
|
|
1253
1253
|
if (r !== -1 && i !== -1) {
|
|
1254
1254
|
const [o, c] = n.slice(r + 1, i).split(']('),
|
|
1255
|
-
|
|
1256
|
-
n = n.slice(0, r) +
|
|
1255
|
+
s = t(o, c)
|
|
1256
|
+
n = n.slice(0, r) + s + n.slice(i + 1)
|
|
1257
1257
|
}
|
|
1258
|
-
|
|
1258
|
+
e = n.indexOf('](', e + 1)
|
|
1259
1259
|
}
|
|
1260
1260
|
return n
|
|
1261
1261
|
}
|
|
1262
|
-
function toQueryString(n,
|
|
1263
|
-
const
|
|
1262
|
+
function toQueryString(n, t = !0) {
|
|
1263
|
+
const e = Object.entries(n)
|
|
1264
1264
|
.filter(([r, i]) => i != null)
|
|
1265
1265
|
.map(([r, i]) => `${r}=${encodeURIComponent(i)}`)
|
|
1266
1266
|
.join('&')
|
|
1267
|
-
return
|
|
1267
|
+
return e ? (t ? '?' : '') + e : ''
|
|
1268
1268
|
}
|
|
1269
1269
|
function parseQueryString(n) {
|
|
1270
|
-
const
|
|
1271
|
-
|
|
1272
|
-
for (const r of
|
|
1270
|
+
const t = {},
|
|
1271
|
+
e = n.split('&')
|
|
1272
|
+
for (const r of e) {
|
|
1273
1273
|
const [i, o] = r.split('=')
|
|
1274
|
-
i && o && (
|
|
1274
|
+
i && o && (t[i] = decodeURIComponent(o))
|
|
1275
1275
|
}
|
|
1276
|
-
return
|
|
1276
|
+
return t
|
|
1277
1277
|
}
|
|
1278
|
-
function hasKey(n,
|
|
1279
|
-
return Object.prototype.hasOwnProperty.call(n,
|
|
1278
|
+
function hasKey(n, t) {
|
|
1279
|
+
return Object.prototype.hasOwnProperty.call(n, t)
|
|
1280
1280
|
}
|
|
1281
|
-
function selectMax(n,
|
|
1282
|
-
let
|
|
1281
|
+
function selectMax(n, t) {
|
|
1282
|
+
let e = null,
|
|
1283
1283
|
r = -1 / 0
|
|
1284
1284
|
for (const [i, o] of Object.entries(n)) {
|
|
1285
|
-
const c =
|
|
1286
|
-
c > r && ((r = c), (
|
|
1285
|
+
const c = t(o)
|
|
1286
|
+
c > r && ((r = c), (e = i))
|
|
1287
1287
|
}
|
|
1288
|
-
return
|
|
1288
|
+
return e ? [e, n[e]] : null
|
|
1289
1289
|
}
|
|
1290
|
-
function reposition(n,
|
|
1291
|
-
const i = n.find(c => c[
|
|
1292
|
-
o = n.find(c => c[
|
|
1293
|
-
i && o ? ((i[
|
|
1290
|
+
function reposition(n, t, e, r) {
|
|
1291
|
+
const i = n.find(c => c[t] === e),
|
|
1292
|
+
o = n.find(c => c[t] === e + r)
|
|
1293
|
+
i && o ? ((i[t] = e + r), (o[t] = e)) : i && (i[t] = e + r), n.sort((c, s) => asNumber(c[t]) - asNumber(s[t])), n.forEach((c, s) => (c[t] = s + 1))
|
|
1294
1294
|
}
|
|
1295
1295
|
function unwrapSingleKey(n) {
|
|
1296
|
-
const
|
|
1297
|
-
if (
|
|
1296
|
+
const t = Object.keys(n)
|
|
1297
|
+
if (t.length === 1) return n[t[0]]
|
|
1298
1298
|
throw new Error('Expected object to have a single key')
|
|
1299
1299
|
}
|
|
1300
|
-
function parseKeyValues(n,
|
|
1300
|
+
function parseKeyValues(n, t = ':') {
|
|
1301
1301
|
return Object.fromEntries(
|
|
1302
1302
|
n
|
|
1303
|
-
.map(
|
|
1304
|
-
.map(
|
|
1305
|
-
.filter(
|
|
1303
|
+
.map(e => splitOnce(e, t))
|
|
1304
|
+
.map(e => (e[0] && e[1] ? [shrinkTrim(e[0]), shrinkTrim(e[1])] : null))
|
|
1305
|
+
.filter(e => e)
|
|
1306
1306
|
)
|
|
1307
1307
|
}
|
|
1308
|
-
function errorMatches(n,
|
|
1308
|
+
function errorMatches(n, t) {
|
|
1309
1309
|
if (!n) return !1
|
|
1310
|
-
const
|
|
1311
|
-
return typeof
|
|
1310
|
+
const e = n.message
|
|
1311
|
+
return typeof e == 'string' && e.includes(t)
|
|
1312
1312
|
}
|
|
1313
|
-
function buildUrl(n,
|
|
1314
|
-
return joinUrl([n,
|
|
1313
|
+
function buildUrl(n, t, e) {
|
|
1314
|
+
return joinUrl([n, t]) + toQueryString(e || {})
|
|
1315
1315
|
}
|
|
1316
|
-
function parseCsv(n,
|
|
1316
|
+
function parseCsv(n, t = ',', e = '"') {
|
|
1317
1317
|
const r = []
|
|
1318
1318
|
let i = '',
|
|
1319
1319
|
o = !1
|
|
1320
1320
|
const c = n.split('')
|
|
1321
|
-
for (const
|
|
1321
|
+
for (const s of c) s === t && !o ? (r.push(i), (i = '')) : s === e && ((!i && !o) || o) ? (o = !o) : (i += s)
|
|
1322
1322
|
return r.push(i), r
|
|
1323
1323
|
}
|
|
1324
1324
|
function humanizeProgress(n) {
|
|
1325
1325
|
return `[${Math.floor(n.progress * 100)}%] ${humanizeTime(n.deltaMs)} out of ${humanizeTime(n.totalTimeMs)} (${humanizeTime(n.remainingTimeMs)} left) [${Math.round(n.baseTimeMs)} ms each]`
|
|
1326
1326
|
}
|
|
1327
|
-
async function waitFor(n,
|
|
1328
|
-
let
|
|
1327
|
+
async function waitFor(n, t) {
|
|
1328
|
+
let e = t.requiredConsecutivePasses || 1,
|
|
1329
1329
|
r = 0
|
|
1330
|
-
for (let i = 0; i <
|
|
1330
|
+
for (let i = 0; i < t.attempts; i++) {
|
|
1331
1331
|
try {
|
|
1332
1332
|
if (await n()) {
|
|
1333
|
-
if ((r++, r >=
|
|
1333
|
+
if ((r++, r >= e)) return
|
|
1334
1334
|
} else r = 0
|
|
1335
1335
|
} catch {
|
|
1336
1336
|
r = 0
|
|
1337
1337
|
}
|
|
1338
|
-
i <
|
|
1338
|
+
i < t.attempts - 1 && (await sleepMillis(t.waitMillis))
|
|
1339
1339
|
}
|
|
1340
1340
|
throw Error('Timed out waiting for predicate')
|
|
1341
1341
|
}
|
|
1342
|
-
function filterAndRemove(n,
|
|
1343
|
-
const
|
|
1344
|
-
for (let r = n.length - 1; r >= 0; r--)
|
|
1345
|
-
return
|
|
1342
|
+
function filterAndRemove(n, t) {
|
|
1343
|
+
const e = []
|
|
1344
|
+
for (let r = n.length - 1; r >= 0; r--) t(n[r]) && e.push(n.splice(r, 1)[0])
|
|
1345
|
+
return e
|
|
1346
1346
|
}
|
|
1347
1347
|
function cloneWithJson(n) {
|
|
1348
1348
|
return JSON.parse(JSON.stringify(n))
|
|
@@ -1357,8 +1357,8 @@ function dateTimeSlug(n) {
|
|
|
1357
1357
|
return (n || new Date()).toISOString().slice(0, 19).replace(/T|:/g, '-')
|
|
1358
1358
|
}
|
|
1359
1359
|
function fromUtcString(n) {
|
|
1360
|
-
const
|
|
1361
|
-
return new Date(
|
|
1360
|
+
const t = new Date(n)
|
|
1361
|
+
return new Date(t.getTime() - t.getTimezoneOffset() * 6e4)
|
|
1362
1362
|
}
|
|
1363
1363
|
function fromMillis(n) {
|
|
1364
1364
|
return new Date(n)
|
|
@@ -1367,66 +1367,66 @@ function createTimeDigits(n) {
|
|
|
1367
1367
|
return String(Math.floor(n)).padStart(2, '0')
|
|
1368
1368
|
}
|
|
1369
1369
|
function normalizeTime(n) {
|
|
1370
|
-
let [
|
|
1371
|
-
isNumber(parseInt(
|
|
1372
|
-
let r = clamp(asInteger(
|
|
1373
|
-
i = clamp(asInteger(
|
|
1370
|
+
let [t, e] = n.split(':')
|
|
1371
|
+
isNumber(parseInt(t, 10)) || (t = '0'), isNumber(parseInt(e, 10)) || (e = '0')
|
|
1372
|
+
let r = clamp(asInteger(t), 0, 23),
|
|
1373
|
+
i = clamp(asInteger(e), 0, 59)
|
|
1374
1374
|
return `${createTimeDigits(r)}:${createTimeDigits(i)}`
|
|
1375
1375
|
}
|
|
1376
1376
|
function humanizeTime(n) {
|
|
1377
|
-
const
|
|
1377
|
+
const t = Math.floor(n / 36e5)
|
|
1378
1378
|
n = n % 36e5
|
|
1379
|
-
const
|
|
1379
|
+
const e = Math.floor(n / 6e4)
|
|
1380
1380
|
n = n % 6e4
|
|
1381
1381
|
const r = Math.floor(n / 1e3)
|
|
1382
|
-
return
|
|
1382
|
+
return t ? `${createTimeDigits(t)}:${createTimeDigits(e)}:${createTimeDigits(r)}` : `${createTimeDigits(e)}:${createTimeDigits(r)}`
|
|
1383
1383
|
}
|
|
1384
1384
|
function absoluteDays(n) {
|
|
1385
1385
|
return Math.floor((isDate(n) ? n.getTime() : n) / 864e5)
|
|
1386
1386
|
}
|
|
1387
|
-
const DefaultTimestampLabels = { today: (n,
|
|
1388
|
-
function getTimestamp(n,
|
|
1389
|
-
const
|
|
1390
|
-
r =
|
|
1387
|
+
const DefaultTimestampLabels = { today: (n, t) => createTimeDigits(n) + ':' + createTimeDigits(t), yesterday: () => 'Yesterday', monday: () => 'Mon', tuesday: () => 'Tue', wednesday: () => 'Wed', thursday: () => 'Thu', friday: () => 'Fri', saturday: () => 'Sat', sunday: () => 'Sun', weeks: n => `${n}w` }
|
|
1388
|
+
function getTimestamp(n, t) {
|
|
1389
|
+
const e = new Date(t?.now || Date.now()),
|
|
1390
|
+
r = t?.labels || DefaultTimestampLabels,
|
|
1391
1391
|
i = isDate(n) ? n : new Date(n)
|
|
1392
|
-
if (absoluteDays(
|
|
1393
|
-
if (absoluteDays(
|
|
1392
|
+
if (absoluteDays(e) === absoluteDays(i)) return r.today(i.getUTCHours(), i.getUTCMinutes(), i.getUTCHours() > 12)
|
|
1393
|
+
if (absoluteDays(e) - absoluteDays(i) === 1) return r.yesterday()
|
|
1394
1394
|
const o = getDayInfoFromDate(i)
|
|
1395
|
-
return absoluteDays(
|
|
1395
|
+
return absoluteDays(e) - absoluteDays(i) < 7 ? r[o.day]() : r.weeks(Math.round((e.getTime() - i.getTime()) / 6048e5))
|
|
1396
1396
|
}
|
|
1397
1397
|
const DefaultTimeDeltaLabels = { now: () => 'A few seconds', seconds: n => `${n} seconds`, minutes: n => `${n} minutes`, hours: n => `${n} hours`, days: n => `${n} days`, weeks: n => `${n} weeks` }
|
|
1398
|
-
function getTimeDelta(n,
|
|
1399
|
-
const
|
|
1400
|
-
r =
|
|
1398
|
+
function getTimeDelta(n, t) {
|
|
1399
|
+
const e = t?.now ?? Date.now(),
|
|
1400
|
+
r = t?.labels || DefaultTimeDeltaLabels,
|
|
1401
1401
|
i = exports.Types.isDate(n) ? n.getTime() : n
|
|
1402
|
-
let o = (
|
|
1402
|
+
let o = (e - i) / 1e3
|
|
1403
1403
|
return o < 10 ? r.now() : o < 120 ? r.seconds(Math.floor(o)) : ((o /= 60), o < 120 ? r.minutes(Math.floor(o)) : ((o /= 60), o < 48 ? r.hours(Math.floor(o)) : ((o /= 24), o < 14 ? r.days(Math.floor(o)) : ((o /= 7), r.weeks(Math.floor(o))))))
|
|
1404
1404
|
}
|
|
1405
|
-
function secondsToHumanTime(n,
|
|
1406
|
-
return getTimeDelta(0, { now: n * 1e3, labels:
|
|
1405
|
+
function secondsToHumanTime(n, t = DefaultTimeDeltaLabels) {
|
|
1406
|
+
return getTimeDelta(0, { now: n * 1e3, labels: t })
|
|
1407
1407
|
}
|
|
1408
|
-
function countCycles(n,
|
|
1409
|
-
const i = (
|
|
1410
|
-
o = Math.floor(i /
|
|
1411
|
-
c =
|
|
1408
|
+
function countCycles(n, t, e) {
|
|
1409
|
+
const i = (e?.now ?? Date.now()) - n,
|
|
1410
|
+
o = Math.floor(i / t),
|
|
1411
|
+
c = t / (e?.precision ?? 1) - Math.ceil((i % t) / (e?.precision ?? 1))
|
|
1412
1412
|
return { cycles: o, remaining: c }
|
|
1413
1413
|
}
|
|
1414
1414
|
const throttleTimers = {}
|
|
1415
|
-
function throttle(n,
|
|
1416
|
-
return !throttleTimers[n] || Date.now() > throttleTimers[n] ? ((throttleTimers[n] = Date.now() +
|
|
1415
|
+
function throttle(n, t) {
|
|
1416
|
+
return !throttleTimers[n] || Date.now() > throttleTimers[n] ? ((throttleTimers[n] = Date.now() + t), !0) : !1
|
|
1417
1417
|
}
|
|
1418
1418
|
const timeUnits = { s: 1e3, m: 6e4, h: 36e5, d: 864e5 }
|
|
1419
|
-
function timeSince(n,
|
|
1420
|
-
return (
|
|
1419
|
+
function timeSince(n, t, e) {
|
|
1420
|
+
return (t = isDate(t) ? t.getTime() : t), (e = e ? (isDate(e) ? e.getTime() : e) : Date.now()), (e - t) / timeUnits[n]
|
|
1421
1421
|
}
|
|
1422
|
-
function getProgress(n,
|
|
1422
|
+
function getProgress(n, t, e, r) {
|
|
1423
1423
|
r || (r = Date.now())
|
|
1424
|
-
const i =
|
|
1424
|
+
const i = t / e,
|
|
1425
1425
|
o = r - n,
|
|
1426
|
-
c = o /
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
return { deltaMs: o, progress: i, baseTimeMs: c, totalTimeMs:
|
|
1426
|
+
c = o / t,
|
|
1427
|
+
s = c * e,
|
|
1428
|
+
u = s - o
|
|
1429
|
+
return { deltaMs: o, progress: i, baseTimeMs: c, totalTimeMs: s, remainingTimeMs: u }
|
|
1430
1430
|
}
|
|
1431
1431
|
const dayNumberIndex = { 0: 'sunday', 1: 'monday', 2: 'tuesday', 3: 'wednesday', 4: 'thursday', 5: 'friday', 6: 'saturday' }
|
|
1432
1432
|
function mapDayNumber(n) {
|
|
@@ -1452,54 +1452,54 @@ function days(n) {
|
|
|
1452
1452
|
}
|
|
1453
1453
|
const dateUnits = { ms: 1, milli: 1, millis: 1, millisecond: 1, milliseconds: 1, s: 1e3, sec: 1e3, second: 1e3, seconds: 1e3, m: 6e4, min: 6e4, minute: 6e4, minutes: 6e4, h: 36e5, hour: 36e5, hours: 36e5, d: 864e5, day: 864e5, days: 864e5, w: 6048e5, week: 6048e5, weeks: 6048e5, month: 2592e6, months: 2592e6, y: 31536e6, year: 31536e6, years: 31536e6 }
|
|
1454
1454
|
function makeDate(n) {
|
|
1455
|
-
const
|
|
1456
|
-
if (isNaN(
|
|
1457
|
-
const
|
|
1455
|
+
const t = parseFloat(n)
|
|
1456
|
+
if (isNaN(t)) throw Error('makeDate got NaN for input')
|
|
1457
|
+
const e = n
|
|
1458
1458
|
.replace(/^-?[0-9.]+/, '')
|
|
1459
1459
|
.trim()
|
|
1460
1460
|
.toLowerCase(),
|
|
1461
|
-
r =
|
|
1462
|
-
if (!r) throw Error(`Unknown unit: "${
|
|
1463
|
-
return Math.ceil(
|
|
1461
|
+
r = e === '' ? 1 : dateUnits[e]
|
|
1462
|
+
if (!r) throw Error(`Unknown unit: "${e}"`)
|
|
1463
|
+
return Math.ceil(t * r)
|
|
1464
1464
|
}
|
|
1465
1465
|
const storageUnitExponents = { b: 0, byte: 0, bytes: 0, kb: 1, kilobyte: 1, kilobytes: 1, mb: 2, megabyte: 2, megabytes: 2, gb: 3, gigabyte: 3, gigabytes: 3, tb: 4, terabyte: 4, terabytes: 4 }
|
|
1466
|
-
function makeStorage(n,
|
|
1467
|
-
const
|
|
1468
|
-
if (isNaN(
|
|
1466
|
+
function makeStorage(n, t = 1024) {
|
|
1467
|
+
const e = parseFloat(n)
|
|
1468
|
+
if (isNaN(e)) throw Error('makeStorage got NaN for input')
|
|
1469
1469
|
const r = n
|
|
1470
1470
|
.replace(/^-?[0-9.]+/, '')
|
|
1471
1471
|
.trim()
|
|
1472
1472
|
.toLowerCase(),
|
|
1473
1473
|
i = r === '' ? 0 : storageUnitExponents[r]
|
|
1474
1474
|
if (i == null) throw Error(`Unknown unit: "${r}"`)
|
|
1475
|
-
return Math.ceil(
|
|
1475
|
+
return Math.ceil(e * t ** i)
|
|
1476
1476
|
}
|
|
1477
1477
|
function getPreLine(n) {
|
|
1478
1478
|
return n.replace(/ +/g, ' ').replace(/^ /gm, '')
|
|
1479
1479
|
}
|
|
1480
1480
|
const tinyCache = new Map()
|
|
1481
|
-
async function getCached(n,
|
|
1481
|
+
async function getCached(n, t, e, r) {
|
|
1482
1482
|
const i = Date.now(),
|
|
1483
1483
|
o = tinyCache.get(n)
|
|
1484
1484
|
if (o && o.validUntil > i) return o.value
|
|
1485
1485
|
r?.onMiss?.()
|
|
1486
1486
|
try {
|
|
1487
|
-
const c = await
|
|
1488
|
-
return tinyCache.set(n, { value: c, validUntil: i +
|
|
1487
|
+
const c = await e()
|
|
1488
|
+
return tinyCache.set(n, { value: c, validUntil: i + t }), c
|
|
1489
1489
|
} catch (c) {
|
|
1490
1490
|
throw (r?.onFailure?.(c), c)
|
|
1491
1491
|
}
|
|
1492
1492
|
}
|
|
1493
|
-
function getCachedDeferred(n,
|
|
1493
|
+
function getCachedDeferred(n, t, e, r) {
|
|
1494
1494
|
const i = Date.now(),
|
|
1495
1495
|
o = tinyCache.get(n)
|
|
1496
1496
|
if (o && o.validUntil > i) return o.value
|
|
1497
1497
|
r?.onMiss?.()
|
|
1498
|
-
const c =
|
|
1498
|
+
const c = e()
|
|
1499
1499
|
return (
|
|
1500
|
-
tinyCache.set(n, { value: c, validUntil: i +
|
|
1501
|
-
c.catch(
|
|
1502
|
-
tinyCache.delete(n), r?.onFailure?.(
|
|
1500
|
+
tinyCache.set(n, { value: c, validUntil: i + t }),
|
|
1501
|
+
c.catch(s => {
|
|
1502
|
+
tinyCache.delete(n), r?.onFailure?.(s)
|
|
1503
1503
|
}),
|
|
1504
1504
|
c
|
|
1505
1505
|
)
|
|
@@ -1512,64 +1512,64 @@ function clearCache() {
|
|
|
1512
1512
|
}
|
|
1513
1513
|
function deleteExpiredFromCache() {
|
|
1514
1514
|
const n = Date.now()
|
|
1515
|
-
for (const [
|
|
1515
|
+
for (const [t, e] of tinyCache.entries()) e.validUntil <= n && tinyCache.delete(t)
|
|
1516
1516
|
}
|
|
1517
1517
|
function cacheSize() {
|
|
1518
1518
|
return tinyCache.size
|
|
1519
1519
|
}
|
|
1520
|
-
function joinUrl(n,
|
|
1521
|
-
;(n = n.filter(o => o)),
|
|
1522
|
-
let
|
|
1523
|
-
isString(n[0]) && n[0].includes('://') && ((
|
|
1520
|
+
function joinUrl(n, t = !1) {
|
|
1521
|
+
;(n = n.filter(o => o)), t && isString(n[1]) && (n[1] = '../' + n[1])
|
|
1522
|
+
let e = ''
|
|
1523
|
+
isString(n[0]) && n[0].includes('://') && ((e = before(n[0], '://') ?? ''), (n[0] = after(n[0], '://') ?? ''))
|
|
1524
1524
|
const r = n.map(o => String(o)).flatMap(o => o.split('/')),
|
|
1525
1525
|
i = []
|
|
1526
|
-
for (let o = 0; o < r.length; o++) r[o] !== '.' && (r[o] === '..' ? (!
|
|
1527
|
-
return (
|
|
1526
|
+
for (let o = 0; o < r.length; o++) r[o] !== '.' && (r[o] === '..' ? (!e || i.length > 1) && i.pop() : i.push(r[o]))
|
|
1527
|
+
return (e ? e + '://' : '') + i.join('/').replaceAll(/\/{2,}/g, '/')
|
|
1528
1528
|
}
|
|
1529
|
-
function replaceBetweenStrings(n,
|
|
1530
|
-
const o = n.indexOf(
|
|
1531
|
-
c = n.indexOf(
|
|
1529
|
+
function replaceBetweenStrings(n, t, e, r, i = !0) {
|
|
1530
|
+
const o = n.indexOf(t),
|
|
1531
|
+
c = n.indexOf(e, o + t.length)
|
|
1532
1532
|
if (o === -1 || c === -1) throw Error('Start or end not found')
|
|
1533
|
-
return i ? n.substring(0, o +
|
|
1533
|
+
return i ? n.substring(0, o + t.length) + r + n.substring(c) : n.substring(0, o) + r + n.substring(c + e.length)
|
|
1534
1534
|
}
|
|
1535
1535
|
function describeMarkdown(n) {
|
|
1536
|
-
let
|
|
1537
|
-
n.startsWith('#') ? ((
|
|
1538
|
-
const
|
|
1536
|
+
let t = 'p'
|
|
1537
|
+
n.startsWith('#') ? ((t = 'h1'), (n = n.slice(1).trim())) : n.startsWith('-') && ((t = 'li'), (n = n.slice(1).trim()))
|
|
1538
|
+
const e = n[0] === n[0].toUpperCase(),
|
|
1539
1539
|
r = /[.?!]$/.test(n),
|
|
1540
1540
|
i = /:$/.test(n)
|
|
1541
|
-
return { type:
|
|
1541
|
+
return { type: t, isCapitalized: e, hasPunctuation: r, endsWithColon: i }
|
|
1542
1542
|
}
|
|
1543
|
-
function isBalanced(n,
|
|
1543
|
+
function isBalanced(n, t = '(', e = ')') {
|
|
1544
1544
|
let r = 0,
|
|
1545
1545
|
i = 0
|
|
1546
|
-
for (; i < n.length; ) if ((n.startsWith(
|
|
1547
|
-
return
|
|
1546
|
+
for (; i < n.length; ) if ((n.startsWith(t, i) ? (r++, (i += t.length)) : n.startsWith(e, i) ? (r--, (i += e.length)) : i++, r < 0)) return !1
|
|
1547
|
+
return t === e ? r % 2 === 0 : r === 0
|
|
1548
1548
|
}
|
|
1549
1549
|
function textToFormat(n) {
|
|
1550
1550
|
n = n.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
|
|
1551
|
-
let
|
|
1552
|
-
for (; (n = n.replace(/(\w+)[\s,']+\w+/g, '$1')), n.length !==
|
|
1551
|
+
let t = n.length
|
|
1552
|
+
for (; (n = n.replace(/(\w+)[\s,']+\w+/g, '$1')), n.length !== t; ) t = n.length
|
|
1553
1553
|
return (n = n.replaceAll(/[A-Z][a-zA-Z0-9]*/g, 'A')), (n = n.replaceAll(/[a-z][a-zA-Z0-9]*/g, 'a')), (n = n.replaceAll(/[\u4E00-\u9FA5]+/g, 'Z')), n
|
|
1554
1554
|
}
|
|
1555
1555
|
function sortObject(n) {
|
|
1556
|
-
const
|
|
1556
|
+
const e = Object.keys(n).sort((i, o) => i.localeCompare(o)),
|
|
1557
1557
|
r = {}
|
|
1558
|
-
for (const i of
|
|
1558
|
+
for (const i of e) r[i] = sortAny(n[i])
|
|
1559
1559
|
return r
|
|
1560
1560
|
}
|
|
1561
1561
|
function sortArray(n) {
|
|
1562
|
-
const
|
|
1563
|
-
return n.sort((
|
|
1562
|
+
const t = []
|
|
1563
|
+
return n.sort((e, r) => JSON.stringify(sortAny(e)).localeCompare(JSON.stringify(sortAny(r)))).forEach(e => t.push(sortAny(e))), t
|
|
1564
1564
|
}
|
|
1565
1565
|
function sortAny(n) {
|
|
1566
1566
|
return Array.isArray(n) ? sortArray(n) : isObject(n) ? sortObject(n) : n
|
|
1567
1567
|
}
|
|
1568
|
-
function deepEquals(n,
|
|
1569
|
-
return JSON.stringify(sortAny(n)) === JSON.stringify(sortAny(
|
|
1568
|
+
function deepEquals(n, t) {
|
|
1569
|
+
return JSON.stringify(sortAny(n)) === JSON.stringify(sortAny(t))
|
|
1570
1570
|
}
|
|
1571
1571
|
function deepEqualsEvery(...n) {
|
|
1572
|
-
for (let
|
|
1572
|
+
for (let t = 1; t < n.length; t++) if (!deepEquals(n[t - 1], n[t])) return !1
|
|
1573
1573
|
return !0
|
|
1574
1574
|
}
|
|
1575
1575
|
function safeParse(n) {
|
|
@@ -1584,39 +1584,39 @@ function createSequence() {
|
|
|
1584
1584
|
return { next: () => n++ }
|
|
1585
1585
|
}
|
|
1586
1586
|
function createOscillator(n) {
|
|
1587
|
-
let
|
|
1588
|
-
return { next: () => n[
|
|
1587
|
+
let t = 0
|
|
1588
|
+
return { next: () => n[t++ % n.length] }
|
|
1589
1589
|
}
|
|
1590
1590
|
function createStatefulToggle(n) {
|
|
1591
|
-
let
|
|
1592
|
-
return
|
|
1593
|
-
const r =
|
|
1594
|
-
return (
|
|
1591
|
+
let t
|
|
1592
|
+
return e => {
|
|
1593
|
+
const r = e === n && t !== n
|
|
1594
|
+
return (t = e), r
|
|
1595
1595
|
}
|
|
1596
1596
|
}
|
|
1597
|
-
function organiseWithLimits(n,
|
|
1597
|
+
function organiseWithLimits(n, t, e, r, i) {
|
|
1598
1598
|
const o = {}
|
|
1599
|
-
for (const c of Object.keys(
|
|
1599
|
+
for (const c of Object.keys(t)) o[c] = []
|
|
1600
1600
|
;(o[r] = []), i && (n = n.sort(i))
|
|
1601
1601
|
for (const c of n) {
|
|
1602
|
-
const
|
|
1603
|
-
|
|
1604
|
-
o[
|
|
1602
|
+
const s = c[e],
|
|
1603
|
+
u = t[s] ? s : r
|
|
1604
|
+
o[u].length >= t[u] ? o[r].push(c) : o[u].push(c)
|
|
1605
1605
|
}
|
|
1606
1606
|
return o
|
|
1607
1607
|
}
|
|
1608
|
-
function diffKeys(n,
|
|
1609
|
-
const
|
|
1610
|
-
r = Object.keys(
|
|
1611
|
-
return { uniqueToA:
|
|
1608
|
+
function diffKeys(n, t) {
|
|
1609
|
+
const e = Object.keys(n),
|
|
1610
|
+
r = Object.keys(t)
|
|
1611
|
+
return { uniqueToA: e.filter(i => !r.includes(i)), uniqueToB: r.filter(i => !e.includes(i)) }
|
|
1612
1612
|
}
|
|
1613
1613
|
function pickRandomKey(n) {
|
|
1614
|
-
const
|
|
1615
|
-
return
|
|
1614
|
+
const t = Object.keys(n)
|
|
1615
|
+
return t[Math.floor(Math.random() * t.length)]
|
|
1616
1616
|
}
|
|
1617
|
-
function mapRandomKey(n,
|
|
1618
|
-
const
|
|
1619
|
-
return (n[
|
|
1617
|
+
function mapRandomKey(n, t) {
|
|
1618
|
+
const e = pickRandomKey(n)
|
|
1619
|
+
return (n[e] = t(n[e])), e
|
|
1620
1620
|
}
|
|
1621
1621
|
function fromObjectString(n) {
|
|
1622
1622
|
return (
|
|
@@ -1625,14 +1625,14 @@ function fromObjectString(n) {
|
|
|
1625
1625
|
`
|
|
1626
1626
|
`
|
|
1627
1627
|
)),
|
|
1628
|
-
(n = n.replace(/(\w+)\((.+)\)/g, (
|
|
1628
|
+
(n = n.replace(/(\w+)\((.+)\)/g, (t, e, r) => `${e}(${r.replaceAll(',', ',')})`)),
|
|
1629
1629
|
(n = n.replace(/(,)(\s+})/g, '$2')),
|
|
1630
1630
|
(n = n.replace(/\.\.\..+?,/g, '')),
|
|
1631
1631
|
(n = n.replace(/({\s+)([a-zA-Z]\w+),/g, "$1$2: '$2',")),
|
|
1632
1632
|
(n = n.replace(/(,\s+)([a-zA-Z]\w+),/g, "$1$2: '$2',")),
|
|
1633
|
-
(n = n.replace(/:(.+)\?(.+):/g, (
|
|
1633
|
+
(n = n.replace(/:(.+)\?(.+):/g, (t, e, r) => `: (${e.trim()} && ${r.trim()}) ||`)),
|
|
1634
1634
|
(n = n.replace(/([a-zA-Z0-9]+)( ?: ?{)/g, '"$1"$2')),
|
|
1635
|
-
(n = n.replace(/([a-zA-Z0-9]+) ?: ?(.+?)(,|\n|})/g, (
|
|
1635
|
+
(n = n.replace(/([a-zA-Z0-9]+) ?: ?(.+?)(,|\n|})/g, (t, e, r, i) => `"${e}":"${r.trim()}"${i}`)),
|
|
1636
1636
|
(n = n.replace(/("'|'")/g, '"')),
|
|
1637
1637
|
(n = n.replaceAll(',', ',')),
|
|
1638
1638
|
JSON.parse(n)
|
|
@@ -1641,228 +1641,228 @@ function fromObjectString(n) {
|
|
|
1641
1641
|
const thresholds = [1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24, 1e27, 1e30, 1e9, 1e16, 1e18, 1e18, 1e18, 1e33],
|
|
1642
1642
|
longNumberUnits = ['thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'gwei', 'bzz', 'btc', 'eth', 'dai', 'decillion'],
|
|
1643
1643
|
shortNumberUnits = ['K', 'M', 'B', 'T', 'Qa', 'Qi', 'Sx', 'Sp', 'O', 'N', 'gwei', 'bzz', 'eth', 'btc', 'dai', 'D']
|
|
1644
|
-
function fromDecimals(n,
|
|
1645
|
-
let r = n.length -
|
|
1646
|
-
if (r <= 0) return '0.' + '0'.repeat(-r) + n + (
|
|
1644
|
+
function fromDecimals(n, t, e) {
|
|
1645
|
+
let r = n.length - t
|
|
1646
|
+
if (r <= 0) return '0.' + '0'.repeat(-r) + n + (e ? ' ' + e : '')
|
|
1647
1647
|
let i = n.substring(0, r),
|
|
1648
1648
|
o = n.substring(r)
|
|
1649
|
-
return i === '' && (i = '0'), i + '.' + o + (
|
|
1649
|
+
return i === '' && (i = '0'), i + '.' + o + (e ? ' ' + e : '')
|
|
1650
1650
|
}
|
|
1651
|
-
function formatNumber(n,
|
|
1652
|
-
const
|
|
1653
|
-
r =
|
|
1654
|
-
i =
|
|
1655
|
-
o =
|
|
1651
|
+
function formatNumber(n, t) {
|
|
1652
|
+
const e = t?.longForm ?? !1,
|
|
1653
|
+
r = t?.unit ? ` ${t.unit}` : '',
|
|
1654
|
+
i = e ? longNumberUnits : shortNumberUnits,
|
|
1655
|
+
o = t?.precision ?? 1
|
|
1656
1656
|
if (n < thresholds[0]) return `${n}${r}`
|
|
1657
|
-
for (let c = 0; c < thresholds.length - 1; c++) if (n < thresholds[c + 1]) return `${(n / thresholds[c]).toFixed(o)}${
|
|
1658
|
-
return `${(n / thresholds[thresholds.length - 1]).toFixed(o)}${
|
|
1657
|
+
for (let c = 0; c < thresholds.length - 1; c++) if (n < thresholds[c + 1]) return `${(n / thresholds[c]).toFixed(o)}${e ? ' ' : ''}${i[c]}${r}`
|
|
1658
|
+
return `${(n / thresholds[thresholds.length - 1]).toFixed(o)}${e ? ' ' : ''}${i[thresholds.length - 1]}${r}`
|
|
1659
1659
|
}
|
|
1660
1660
|
function makeNumber(n) {
|
|
1661
|
-
const
|
|
1662
|
-
if (isNaN(
|
|
1663
|
-
const
|
|
1664
|
-
r = shortNumberUnits.findIndex(i => i.toLowerCase() ===
|
|
1665
|
-
return r === -1 ?
|
|
1661
|
+
const t = parseFloat(n)
|
|
1662
|
+
if (isNaN(t)) throw Error('makeNumber got NaN for input')
|
|
1663
|
+
const e = n.replace(/^-?[0-9.]+/, '').trim(),
|
|
1664
|
+
r = shortNumberUnits.findIndex(i => i.toLowerCase() === e.toLowerCase())
|
|
1665
|
+
return r === -1 ? t : t * thresholds[r]
|
|
1666
1666
|
}
|
|
1667
|
-
function clamp(n,
|
|
1668
|
-
return n <
|
|
1667
|
+
function clamp(n, t, e) {
|
|
1668
|
+
return n < t ? t : n > e ? e : n
|
|
1669
1669
|
}
|
|
1670
|
-
function increment(n,
|
|
1671
|
-
const r = n +
|
|
1672
|
-
return r >
|
|
1670
|
+
function increment(n, t, e) {
|
|
1671
|
+
const r = n + t
|
|
1672
|
+
return r > e ? e : r
|
|
1673
1673
|
}
|
|
1674
|
-
function decrement(n,
|
|
1675
|
-
const r = n -
|
|
1676
|
-
return r <
|
|
1674
|
+
function decrement(n, t, e) {
|
|
1675
|
+
const r = n - t
|
|
1676
|
+
return r < e ? e : r
|
|
1677
1677
|
}
|
|
1678
|
-
function runOn(n,
|
|
1679
|
-
return
|
|
1678
|
+
function runOn(n, t) {
|
|
1679
|
+
return t(n), n
|
|
1680
1680
|
}
|
|
1681
|
-
function ifPresent(n,
|
|
1682
|
-
n &&
|
|
1681
|
+
function ifPresent(n, t) {
|
|
1682
|
+
n && t(n)
|
|
1683
1683
|
}
|
|
1684
|
-
function mergeArrays(n,
|
|
1685
|
-
const
|
|
1686
|
-
for (const r of
|
|
1684
|
+
function mergeArrays(n, t) {
|
|
1685
|
+
const e = Object.keys(t)
|
|
1686
|
+
for (const r of e) Array.isArray(t[r]) && Array.isArray(n[r]) && pushAll(n[r], t[r])
|
|
1687
1687
|
}
|
|
1688
1688
|
function empty(n) {
|
|
1689
1689
|
return n.splice(0, n.length), n
|
|
1690
1690
|
}
|
|
1691
1691
|
function removeEmptyArrays(n) {
|
|
1692
|
-
for (const
|
|
1692
|
+
for (const t of Object.keys(n)) (isEmptyArray(n[t]) || (Array.isArray(n[t]) && n[t].every(e => e == null))) && delete n[t]
|
|
1693
1693
|
return n
|
|
1694
1694
|
}
|
|
1695
1695
|
function removeEmptyValues(n) {
|
|
1696
|
-
for (const
|
|
1696
|
+
for (const t of Object.entries(n)) (t[1] === null || t[1] === void 0 || (isString(t[1]) && isBlank(t[1]))) && delete n[t[0]]
|
|
1697
1697
|
return n
|
|
1698
1698
|
}
|
|
1699
|
-
function filterObjectKeys(n,
|
|
1700
|
-
const
|
|
1701
|
-
for (const [r, i] of Object.entries(n))
|
|
1702
|
-
return
|
|
1699
|
+
function filterObjectKeys(n, t) {
|
|
1700
|
+
const e = {}
|
|
1701
|
+
for (const [r, i] of Object.entries(n)) t(r) && (e[r] = i)
|
|
1702
|
+
return e
|
|
1703
1703
|
}
|
|
1704
|
-
function filterObjectValues(n,
|
|
1705
|
-
const
|
|
1706
|
-
for (const [r, i] of Object.entries(n))
|
|
1707
|
-
return
|
|
1704
|
+
function filterObjectValues(n, t) {
|
|
1705
|
+
const e = {}
|
|
1706
|
+
for (const [r, i] of Object.entries(n)) t(i) && (e[r] = i)
|
|
1707
|
+
return e
|
|
1708
1708
|
}
|
|
1709
|
-
function mapObject(n,
|
|
1710
|
-
const
|
|
1711
|
-
for (const r of Object.entries(n))
|
|
1712
|
-
return
|
|
1709
|
+
function mapObject(n, t) {
|
|
1710
|
+
const e = {}
|
|
1711
|
+
for (const r of Object.entries(n)) e[r[0]] = t(r[1])
|
|
1712
|
+
return e
|
|
1713
1713
|
}
|
|
1714
|
-
function mapIterable(n,
|
|
1715
|
-
const
|
|
1714
|
+
function mapIterable(n, t) {
|
|
1715
|
+
const e = []
|
|
1716
1716
|
let r = 0
|
|
1717
|
-
for (const i of n)
|
|
1718
|
-
return
|
|
1717
|
+
for (const i of n) e.push(t(i, r++))
|
|
1718
|
+
return e
|
|
1719
1719
|
}
|
|
1720
|
-
async function rethrow(n,
|
|
1720
|
+
async function rethrow(n, t) {
|
|
1721
1721
|
try {
|
|
1722
1722
|
return await n()
|
|
1723
1723
|
} catch {
|
|
1724
|
-
throw
|
|
1724
|
+
throw t
|
|
1725
1725
|
}
|
|
1726
1726
|
}
|
|
1727
|
-
function setSomeOnObject(n,
|
|
1728
|
-
|
|
1727
|
+
function setSomeOnObject(n, t, e) {
|
|
1728
|
+
e != null && (n[t] = e)
|
|
1729
1729
|
}
|
|
1730
|
-
function setSomeDeep(n,
|
|
1731
|
-
const i = getDeep(
|
|
1732
|
-
i != null && setDeep(n,
|
|
1730
|
+
function setSomeDeep(n, t, e, r) {
|
|
1731
|
+
const i = getDeep(e, r)
|
|
1732
|
+
i != null && setDeep(n, t, i)
|
|
1733
1733
|
}
|
|
1734
1734
|
function flip(n) {
|
|
1735
|
-
const
|
|
1736
|
-
for (const [
|
|
1737
|
-
return
|
|
1735
|
+
const t = {}
|
|
1736
|
+
for (const [e, r] of Object.entries(n)) t[r] = e
|
|
1737
|
+
return t
|
|
1738
1738
|
}
|
|
1739
1739
|
function getAllPermutations(n) {
|
|
1740
|
-
const
|
|
1741
|
-
|
|
1742
|
-
r =
|
|
1740
|
+
const t = Object.keys(n),
|
|
1741
|
+
e = t.map(s => n[s].length),
|
|
1742
|
+
r = e.reduce((s, u) => (s *= u))
|
|
1743
1743
|
let i = 1
|
|
1744
1744
|
const o = [1]
|
|
1745
|
-
for (let
|
|
1745
|
+
for (let s = 0; s < e.length - 1; s++) (i *= e[s]), o.push(i)
|
|
1746
1746
|
const c = []
|
|
1747
|
-
for (let
|
|
1748
|
-
const
|
|
1749
|
-
for (let f = 0; f <
|
|
1750
|
-
const l = n[
|
|
1751
|
-
a = Math.floor(
|
|
1752
|
-
|
|
1747
|
+
for (let s = 0; s < r; s++) {
|
|
1748
|
+
const u = {}
|
|
1749
|
+
for (let f = 0; f < t.length; f++) {
|
|
1750
|
+
const l = n[t[f]],
|
|
1751
|
+
a = Math.floor(s / o[f]) % l.length
|
|
1752
|
+
u[t[f]] = l[a]
|
|
1753
1753
|
}
|
|
1754
|
-
c.push(
|
|
1754
|
+
c.push(u)
|
|
1755
1755
|
}
|
|
1756
1756
|
return c
|
|
1757
1757
|
}
|
|
1758
1758
|
function countTruthyValues(n) {
|
|
1759
|
-
return Object.values(n).filter(
|
|
1759
|
+
return Object.values(n).filter(t => t).length
|
|
1760
1760
|
}
|
|
1761
|
-
function getFlatNotation(n,
|
|
1762
|
-
return n + (
|
|
1761
|
+
function getFlatNotation(n, t, e) {
|
|
1762
|
+
return n + (e ? '[' + t + ']' : (n.length ? '.' : '') + t)
|
|
1763
1763
|
}
|
|
1764
|
-
function flattenInner(n,
|
|
1765
|
-
if (!isObject(
|
|
1766
|
-
for (const [o, c] of Object.entries(
|
|
1767
|
-
const
|
|
1768
|
-
Array.isArray(c) ? (i ? flattenInner(n, c,
|
|
1764
|
+
function flattenInner(n, t, e, r, i) {
|
|
1765
|
+
if (!isObject(t)) return t
|
|
1766
|
+
for (const [o, c] of Object.entries(t)) {
|
|
1767
|
+
const s = getFlatNotation(e, o, r)
|
|
1768
|
+
Array.isArray(c) ? (i ? flattenInner(n, c, s, !0, i) : (n[s] = c.map(u => flattenInner(Array.isArray(u) ? [] : {}, u, '', !1, i)))) : isObject(c) ? flattenInner(n, c, s, !1, i) : (n[s] = c)
|
|
1769
1769
|
}
|
|
1770
1770
|
return n
|
|
1771
1771
|
}
|
|
1772
|
-
function flatten(n,
|
|
1773
|
-
return flattenInner({}, n,
|
|
1772
|
+
function flatten(n, t = !1, e) {
|
|
1773
|
+
return flattenInner({}, n, e || '', !1, t)
|
|
1774
1774
|
}
|
|
1775
1775
|
function unflatten(n) {
|
|
1776
1776
|
if (!isObject(n)) return n
|
|
1777
|
-
const
|
|
1778
|
-
for (const [
|
|
1777
|
+
const t = Array.isArray(n) ? [] : {}
|
|
1778
|
+
for (const [e, r] of Object.entries(n))
|
|
1779
1779
|
Array.isArray(r)
|
|
1780
1780
|
? setDeep(
|
|
1781
|
-
e,
|
|
1782
1781
|
t,
|
|
1782
|
+
e,
|
|
1783
1783
|
r.map(i => unflatten(i))
|
|
1784
1784
|
)
|
|
1785
|
-
: setDeep(
|
|
1786
|
-
return
|
|
1785
|
+
: setDeep(t, e, r)
|
|
1786
|
+
return t
|
|
1787
1787
|
}
|
|
1788
|
-
function match(n,
|
|
1789
|
-
return
|
|
1788
|
+
function match(n, t, e) {
|
|
1789
|
+
return t[n] ? t[n] : e
|
|
1790
1790
|
}
|
|
1791
|
-
function indexArray(n,
|
|
1792
|
-
const
|
|
1791
|
+
function indexArray(n, t) {
|
|
1792
|
+
const e = {}
|
|
1793
1793
|
for (const r of n) {
|
|
1794
|
-
const i =
|
|
1795
|
-
|
|
1794
|
+
const i = t(r)
|
|
1795
|
+
e[i] = r
|
|
1796
1796
|
}
|
|
1797
|
-
return
|
|
1797
|
+
return e
|
|
1798
1798
|
}
|
|
1799
|
-
function indexArrayToCollection(n,
|
|
1800
|
-
const
|
|
1799
|
+
function indexArrayToCollection(n, t) {
|
|
1800
|
+
const e = {}
|
|
1801
1801
|
for (const r of n) {
|
|
1802
|
-
const i =
|
|
1803
|
-
|
|
1802
|
+
const i = t(r)
|
|
1803
|
+
e[i] || (e[i] = []), e[i].push(r)
|
|
1804
1804
|
}
|
|
1805
|
-
return
|
|
1805
|
+
return e
|
|
1806
1806
|
}
|
|
1807
|
-
function splitBySize(n,
|
|
1808
|
-
const
|
|
1809
|
-
for (let r = 0; r < n.length; r +=
|
|
1810
|
-
return
|
|
1807
|
+
function splitBySize(n, t) {
|
|
1808
|
+
const e = []
|
|
1809
|
+
for (let r = 0; r < n.length; r += t) e.push(n.slice(r, r + t))
|
|
1810
|
+
return e
|
|
1811
1811
|
}
|
|
1812
|
-
function splitByCount(n,
|
|
1813
|
-
const
|
|
1812
|
+
function splitByCount(n, t) {
|
|
1813
|
+
const e = Math.ceil(n.length / t),
|
|
1814
1814
|
r = []
|
|
1815
|
-
for (let i = 0; i < n.length; i +=
|
|
1815
|
+
for (let i = 0; i < n.length; i += e) r.push(n.slice(i, i + e))
|
|
1816
1816
|
return r
|
|
1817
1817
|
}
|
|
1818
|
-
function tokenizeByLength(n,
|
|
1819
|
-
const
|
|
1820
|
-
r = Math.ceil(n.length /
|
|
1821
|
-
for (let i = 0; i < r; i++)
|
|
1822
|
-
return
|
|
1818
|
+
function tokenizeByLength(n, t) {
|
|
1819
|
+
const e = [],
|
|
1820
|
+
r = Math.ceil(n.length / t)
|
|
1821
|
+
for (let i = 0; i < r; i++) e.push(n.slice(i * t, i * t + t))
|
|
1822
|
+
return e
|
|
1823
1823
|
}
|
|
1824
|
-
function tokenizeByCount(n,
|
|
1825
|
-
const
|
|
1826
|
-
return tokenizeByLength(n,
|
|
1824
|
+
function tokenizeByCount(n, t) {
|
|
1825
|
+
const e = Math.ceil(n.length / t)
|
|
1826
|
+
return tokenizeByLength(n, e)
|
|
1827
1827
|
}
|
|
1828
|
-
function makeUnique(n,
|
|
1829
|
-
return Object.values(indexArray(n,
|
|
1828
|
+
function makeUnique(n, t) {
|
|
1829
|
+
return Object.values(indexArray(n, t))
|
|
1830
1830
|
}
|
|
1831
|
-
function countUnique(n,
|
|
1832
|
-
const o =
|
|
1831
|
+
function countUnique(n, t, e, r, i) {
|
|
1832
|
+
const o = t ? n.map(t) : n,
|
|
1833
1833
|
c = {}
|
|
1834
|
-
for (const
|
|
1835
|
-
const
|
|
1836
|
-
return
|
|
1834
|
+
for (const u of o) c[u] = (c[u] || 0) + 1
|
|
1835
|
+
const s = r ? sortObjectValues(c, i ? (u, f) => u[1] - f[1] : (u, f) => f[1] - u[1]) : c
|
|
1836
|
+
return e ? Object.keys(s) : s
|
|
1837
1837
|
}
|
|
1838
|
-
function sortObjectValues(n,
|
|
1839
|
-
return Object.fromEntries(Object.entries(n).sort(
|
|
1838
|
+
function sortObjectValues(n, t) {
|
|
1839
|
+
return Object.fromEntries(Object.entries(n).sort(t))
|
|
1840
1840
|
}
|
|
1841
1841
|
function transformToArray(n) {
|
|
1842
|
-
const
|
|
1843
|
-
|
|
1844
|
-
r = n[
|
|
1842
|
+
const t = [],
|
|
1843
|
+
e = Object.keys(n),
|
|
1844
|
+
r = n[e[0]].length
|
|
1845
1845
|
for (let i = 0; i < r; i++) {
|
|
1846
1846
|
const o = {}
|
|
1847
|
-
for (const c of
|
|
1848
|
-
|
|
1847
|
+
for (const c of e) o[c] = n[c][i]
|
|
1848
|
+
t.push(o)
|
|
1849
1849
|
}
|
|
1850
|
-
return
|
|
1850
|
+
return t
|
|
1851
1851
|
}
|
|
1852
|
-
function incrementMulti(n,
|
|
1853
|
-
for (const r of n) r[
|
|
1852
|
+
function incrementMulti(n, t, e = 1) {
|
|
1853
|
+
for (const r of n) r[t] += e
|
|
1854
1854
|
}
|
|
1855
|
-
function setMulti(n,
|
|
1856
|
-
for (const r of n) r[
|
|
1855
|
+
function setMulti(n, t, e) {
|
|
1856
|
+
for (const r of n) r[t] = e
|
|
1857
1857
|
}
|
|
1858
|
-
function group(n,
|
|
1859
|
-
const
|
|
1858
|
+
function group(n, t) {
|
|
1859
|
+
const e = []
|
|
1860
1860
|
let r = []
|
|
1861
1861
|
return (
|
|
1862
1862
|
n.forEach((i, o) => {
|
|
1863
|
-
;(o === 0 || !
|
|
1863
|
+
;(o === 0 || !t(i, n[o - 1])) && ((r = []), e.push(r)), r.push(i)
|
|
1864
1864
|
}),
|
|
1865
|
-
|
|
1865
|
+
e
|
|
1866
1866
|
)
|
|
1867
1867
|
}
|
|
1868
1868
|
function createBidirectionalMap() {
|
|
@@ -1871,66 +1871,66 @@ function createBidirectionalMap() {
|
|
|
1871
1871
|
function createTemporalBidirectionalMap() {
|
|
1872
1872
|
return { map: new Map(), keys: [] }
|
|
1873
1873
|
}
|
|
1874
|
-
function pushToBidirectionalMap(n,
|
|
1875
|
-
if (n.map.has(
|
|
1876
|
-
const i = n.keys.indexOf(
|
|
1874
|
+
function pushToBidirectionalMap(n, t, e, r = 100) {
|
|
1875
|
+
if (n.map.has(t)) {
|
|
1876
|
+
const i = n.keys.indexOf(t)
|
|
1877
1877
|
n.keys.splice(i, 1)
|
|
1878
1878
|
}
|
|
1879
|
-
if ((n.map.set(
|
|
1879
|
+
if ((n.map.set(t, e), n.keys.push(t), n.keys.length > r)) {
|
|
1880
1880
|
const i = n.keys.shift()
|
|
1881
1881
|
i && n.map.delete(i)
|
|
1882
1882
|
}
|
|
1883
1883
|
}
|
|
1884
|
-
function unshiftToBidirectionalMap(n,
|
|
1885
|
-
if (n.map.has(
|
|
1886
|
-
const i = n.keys.indexOf(
|
|
1884
|
+
function unshiftToBidirectionalMap(n, t, e, r = 100) {
|
|
1885
|
+
if (n.map.has(t)) {
|
|
1886
|
+
const i = n.keys.indexOf(t)
|
|
1887
1887
|
n.keys.splice(i, 1)
|
|
1888
1888
|
}
|
|
1889
|
-
if ((n.map.set(
|
|
1889
|
+
if ((n.map.set(t, e), n.keys.unshift(t), n.keys.length > r)) {
|
|
1890
1890
|
const i = n.keys.shift()
|
|
1891
1891
|
i && n.map.delete(i)
|
|
1892
1892
|
}
|
|
1893
1893
|
}
|
|
1894
|
-
function addToTemporalBidirectionalMap(n,
|
|
1895
|
-
pushToBidirectionalMap(n,
|
|
1894
|
+
function addToTemporalBidirectionalMap(n, t, e, r, i = 100) {
|
|
1895
|
+
pushToBidirectionalMap(n, t, { validUntil: Date.now() + r, data: e }, i)
|
|
1896
1896
|
}
|
|
1897
|
-
function getFromTemporalBidirectionalMap(n,
|
|
1898
|
-
const
|
|
1899
|
-
return
|
|
1897
|
+
function getFromTemporalBidirectionalMap(n, t) {
|
|
1898
|
+
const e = n.map.get(t)
|
|
1899
|
+
return e && e.validUntil > Date.now() ? e.data : null
|
|
1900
1900
|
}
|
|
1901
1901
|
class Optional {
|
|
1902
|
-
constructor(
|
|
1903
|
-
this.value =
|
|
1902
|
+
constructor(t) {
|
|
1903
|
+
this.value = t
|
|
1904
1904
|
}
|
|
1905
|
-
static of(
|
|
1906
|
-
return new Optional(
|
|
1905
|
+
static of(t) {
|
|
1906
|
+
return new Optional(t)
|
|
1907
1907
|
}
|
|
1908
1908
|
static empty() {
|
|
1909
1909
|
return new Optional(null)
|
|
1910
1910
|
}
|
|
1911
|
-
map(
|
|
1912
|
-
return new Optional(this.value !== null && this.value !== void 0 ?
|
|
1911
|
+
map(t) {
|
|
1912
|
+
return new Optional(this.value !== null && this.value !== void 0 ? t(this.value) : null)
|
|
1913
1913
|
}
|
|
1914
|
-
mapAsync(
|
|
1915
|
-
return this.value !== null && this.value !== void 0 ?
|
|
1914
|
+
mapAsync(t) {
|
|
1915
|
+
return this.value !== null && this.value !== void 0 ? t(this.value).then(e => Optional.of(e)) : Promise.resolve(Optional.empty())
|
|
1916
1916
|
}
|
|
1917
|
-
ifPresent(
|
|
1918
|
-
return this.value !== null && this.value !== void 0 &&
|
|
1917
|
+
ifPresent(t) {
|
|
1918
|
+
return this.value !== null && this.value !== void 0 && t(this.value), this
|
|
1919
1919
|
}
|
|
1920
|
-
ifPresentAsync(
|
|
1921
|
-
return this.value !== null && this.value !== void 0 ?
|
|
1920
|
+
ifPresentAsync(t) {
|
|
1921
|
+
return this.value !== null && this.value !== void 0 ? t(this.value).then(() => this) : Promise.resolve(this)
|
|
1922
1922
|
}
|
|
1923
|
-
ifAbsent(
|
|
1924
|
-
return (this.value === null || this.value === void 0) &&
|
|
1923
|
+
ifAbsent(t) {
|
|
1924
|
+
return (this.value === null || this.value === void 0) && t(), this
|
|
1925
1925
|
}
|
|
1926
|
-
ifAbsentAsync(
|
|
1927
|
-
return this.value === null || this.value === void 0 ?
|
|
1926
|
+
ifAbsentAsync(t) {
|
|
1927
|
+
return this.value === null || this.value === void 0 ? t().then(() => this) : Promise.resolve(this)
|
|
1928
1928
|
}
|
|
1929
|
-
getOrFallback(
|
|
1930
|
-
return this.value ??
|
|
1929
|
+
getOrFallback(t) {
|
|
1930
|
+
return this.value ?? t()
|
|
1931
1931
|
}
|
|
1932
|
-
getOrFallbackAsync(
|
|
1933
|
-
return this.value !== null && this.value !== void 0 ? Promise.resolve(this.value) :
|
|
1932
|
+
getOrFallbackAsync(t) {
|
|
1933
|
+
return this.value !== null && this.value !== void 0 ? Promise.resolve(this.value) : t()
|
|
1934
1934
|
}
|
|
1935
1935
|
getOrThrow() {
|
|
1936
1936
|
if (this.value === null || this.value === void 0) throw Error('Optional.value is empty')
|
|
@@ -1939,8 +1939,8 @@ class Optional {
|
|
|
1939
1939
|
}
|
|
1940
1940
|
exports.Optional = Optional
|
|
1941
1941
|
class Lazy {
|
|
1942
|
-
constructor(
|
|
1943
|
-
;(this.supplier =
|
|
1942
|
+
constructor(t) {
|
|
1943
|
+
;(this.supplier = t), (this.value = null)
|
|
1944
1944
|
}
|
|
1945
1945
|
get() {
|
|
1946
1946
|
return this.value || (this.value = this.supplier()), this.value
|
|
@@ -1948,8 +1948,8 @@ class Lazy {
|
|
|
1948
1948
|
}
|
|
1949
1949
|
exports.Lazy = Lazy
|
|
1950
1950
|
class AsyncLazy {
|
|
1951
|
-
constructor(
|
|
1952
|
-
;(this.supplier =
|
|
1951
|
+
constructor(t) {
|
|
1952
|
+
;(this.supplier = t), (this.value = null)
|
|
1953
1953
|
}
|
|
1954
1954
|
async get() {
|
|
1955
1955
|
return this.value || (this.value = await this.supplier()), this.value
|
|
@@ -1957,92 +1957,92 @@ class AsyncLazy {
|
|
|
1957
1957
|
}
|
|
1958
1958
|
exports.AsyncLazy = AsyncLazy
|
|
1959
1959
|
function multicall(n) {
|
|
1960
|
-
return () => n.forEach(
|
|
1960
|
+
return () => n.forEach(t => t())
|
|
1961
1961
|
}
|
|
1962
|
-
function maxBy(n,
|
|
1963
|
-
return n.reduce((
|
|
1962
|
+
function maxBy(n, t) {
|
|
1963
|
+
return n.reduce((e, r) => (t(e) > t(r) ? e : r))
|
|
1964
1964
|
}
|
|
1965
|
-
function minBy(n,
|
|
1966
|
-
return n.reduce((
|
|
1965
|
+
function minBy(n, t) {
|
|
1966
|
+
return n.reduce((e, r) => (t(e) < t(r) ? e : r))
|
|
1967
1967
|
}
|
|
1968
|
-
function allArrayIndexOf(n,
|
|
1969
|
-
const
|
|
1968
|
+
function allArrayIndexOf(n, t) {
|
|
1969
|
+
const e = []
|
|
1970
1970
|
return (
|
|
1971
1971
|
n.forEach((r, i) => {
|
|
1972
|
-
|
|
1972
|
+
t(r) && e.push(i)
|
|
1973
1973
|
}),
|
|
1974
|
-
|
|
1974
|
+
e
|
|
1975
1975
|
)
|
|
1976
1976
|
}
|
|
1977
|
-
function findInstance(n,
|
|
1978
|
-
const
|
|
1979
|
-
return Optional.of(
|
|
1977
|
+
function findInstance(n, t) {
|
|
1978
|
+
const e = n.find(r => r instanceof t)
|
|
1979
|
+
return Optional.of(e)
|
|
1980
1980
|
}
|
|
1981
|
-
function filterInstances(n,
|
|
1982
|
-
return n.filter(
|
|
1981
|
+
function filterInstances(n, t) {
|
|
1982
|
+
return n.filter(e => e instanceof t)
|
|
1983
1983
|
}
|
|
1984
|
-
function interleave(n,
|
|
1985
|
-
const
|
|
1986
|
-
r = Math.max(n.length,
|
|
1987
|
-
for (let i = 0; i < r; i++) n[i] &&
|
|
1988
|
-
return
|
|
1984
|
+
function interleave(n, t) {
|
|
1985
|
+
const e = [],
|
|
1986
|
+
r = Math.max(n.length, t.length)
|
|
1987
|
+
for (let i = 0; i < r; i++) n[i] && e.push(n[i]), t[i] && e.push(t[i])
|
|
1988
|
+
return e
|
|
1989
1989
|
}
|
|
1990
|
-
function toggle(n,
|
|
1991
|
-
return n.includes(
|
|
1990
|
+
function toggle(n, t) {
|
|
1991
|
+
return n.includes(t) ? n.filter(e => e !== t) : [...n, t]
|
|
1992
1992
|
}
|
|
1993
1993
|
class Node {
|
|
1994
|
-
constructor(
|
|
1995
|
-
;(this.value =
|
|
1994
|
+
constructor(t) {
|
|
1995
|
+
;(this.value = t), (this.children = [])
|
|
1996
1996
|
}
|
|
1997
1997
|
}
|
|
1998
|
-
function createHierarchy(n,
|
|
1998
|
+
function createHierarchy(n, t, e, r, i = !1) {
|
|
1999
1999
|
const o = new Map(),
|
|
2000
2000
|
c = []
|
|
2001
|
-
n.forEach(
|
|
2002
|
-
const f = new Node(
|
|
2003
|
-
o.set(
|
|
2001
|
+
n.forEach(u => {
|
|
2002
|
+
const f = new Node(u)
|
|
2003
|
+
o.set(u[t], f)
|
|
2004
2004
|
}),
|
|
2005
|
-
n.forEach(
|
|
2006
|
-
const f = o.get(
|
|
2005
|
+
n.forEach(u => {
|
|
2006
|
+
const f = o.get(u[t])
|
|
2007
2007
|
if (!f) return
|
|
2008
|
-
const l =
|
|
2008
|
+
const l = u[e]
|
|
2009
2009
|
if (l) {
|
|
2010
2010
|
const a = o.get(l)
|
|
2011
2011
|
a && a.children.push(f)
|
|
2012
2012
|
} else c.push(f)
|
|
2013
2013
|
})
|
|
2014
|
-
const
|
|
2015
|
-
|
|
2014
|
+
const s = u => {
|
|
2015
|
+
u.children.sort((f, l) => {
|
|
2016
2016
|
const a = f.value[r],
|
|
2017
2017
|
h = l.value[r]
|
|
2018
2018
|
return i ? h - a : a - h
|
|
2019
2019
|
}),
|
|
2020
|
-
|
|
2020
|
+
u.children.forEach(s)
|
|
2021
2021
|
}
|
|
2022
|
-
return c.forEach(
|
|
2022
|
+
return c.forEach(s), c
|
|
2023
2023
|
}
|
|
2024
|
-
function log2Reduce(n,
|
|
2024
|
+
function log2Reduce(n, t) {
|
|
2025
2025
|
if (Math.log2(n.length) % 1 !== 0) throw new Error('Array length must be a power of 2')
|
|
2026
|
-
let
|
|
2027
|
-
for (;
|
|
2026
|
+
let e = [...n]
|
|
2027
|
+
for (; e.length > 1; ) {
|
|
2028
2028
|
const r = []
|
|
2029
|
-
for (let i = 0; i <
|
|
2030
|
-
const o =
|
|
2031
|
-
r.push(e
|
|
2029
|
+
for (let i = 0; i < e.length; i += 2) {
|
|
2030
|
+
const o = e[i + 1]
|
|
2031
|
+
r.push(t(e[i], o))
|
|
2032
2032
|
}
|
|
2033
|
-
|
|
2033
|
+
e = r
|
|
2034
2034
|
}
|
|
2035
|
-
return
|
|
2035
|
+
return e[0]
|
|
2036
2036
|
}
|
|
2037
2037
|
function concatBytes(...n) {
|
|
2038
|
-
const
|
|
2039
|
-
|
|
2038
|
+
const t = n.reduce((i, o) => i + o.length, 0),
|
|
2039
|
+
e = new Uint8Array(t)
|
|
2040
2040
|
let r = 0
|
|
2041
2041
|
return (
|
|
2042
2042
|
n.forEach(i => {
|
|
2043
|
-
|
|
2043
|
+
e.set(i, r), (r += i.length)
|
|
2044
2044
|
}),
|
|
2045
|
-
|
|
2045
|
+
e
|
|
2046
2046
|
)
|
|
2047
2047
|
}
|
|
2048
2048
|
function isPng(n) {
|
|
@@ -2063,169 +2063,169 @@ function numberToUint8(n) {
|
|
|
2063
2063
|
function uint8ToNumber(n) {
|
|
2064
2064
|
return n[0]
|
|
2065
2065
|
}
|
|
2066
|
-
function numberToUint16(n,
|
|
2067
|
-
const
|
|
2068
|
-
return new DataView(
|
|
2066
|
+
function numberToUint16(n, t) {
|
|
2067
|
+
const e = new ArrayBuffer(2)
|
|
2068
|
+
return new DataView(e).setUint16(0, n, t === 'LE'), new Uint8Array(e)
|
|
2069
2069
|
}
|
|
2070
|
-
function uint16ToNumber(n,
|
|
2071
|
-
return new DataView(n.buffer).getUint16(n.byteOffset,
|
|
2070
|
+
function uint16ToNumber(n, t) {
|
|
2071
|
+
return new DataView(n.buffer).getUint16(n.byteOffset, t === 'LE')
|
|
2072
2072
|
}
|
|
2073
|
-
function numberToUint32(n,
|
|
2074
|
-
const
|
|
2075
|
-
return new DataView(
|
|
2073
|
+
function numberToUint32(n, t) {
|
|
2074
|
+
const e = new ArrayBuffer(4)
|
|
2075
|
+
return new DataView(e).setUint32(0, n, t === 'LE'), new Uint8Array(e)
|
|
2076
2076
|
}
|
|
2077
|
-
function uint32ToNumber(n,
|
|
2078
|
-
return new DataView(n.buffer).getUint32(n.byteOffset,
|
|
2077
|
+
function uint32ToNumber(n, t) {
|
|
2078
|
+
return new DataView(n.buffer).getUint32(n.byteOffset, t === 'LE')
|
|
2079
2079
|
}
|
|
2080
|
-
function numberToUint64(n,
|
|
2081
|
-
const
|
|
2082
|
-
return new DataView(
|
|
2080
|
+
function numberToUint64(n, t) {
|
|
2081
|
+
const e = new ArrayBuffer(8)
|
|
2082
|
+
return new DataView(e).setBigUint64(0, BigInt(n), t === 'LE'), new Uint8Array(e)
|
|
2083
2083
|
}
|
|
2084
|
-
function uint64ToNumber(n,
|
|
2085
|
-
return new DataView(n.buffer).getBigUint64(n.byteOffset,
|
|
2084
|
+
function uint64ToNumber(n, t) {
|
|
2085
|
+
return new DataView(n.buffer).getBigUint64(n.byteOffset, t === 'LE')
|
|
2086
2086
|
}
|
|
2087
|
-
function numberToUint256(n,
|
|
2087
|
+
function numberToUint256(n, t) {
|
|
2088
2088
|
const r = new Uint8Array(32)
|
|
2089
2089
|
let i = n
|
|
2090
|
-
if (
|
|
2090
|
+
if (t === 'LE') {
|
|
2091
2091
|
for (let o = 0; o < 32; o++) (r[o] = Number(i & 0xffn)), (i >>= 8n)
|
|
2092
2092
|
return r
|
|
2093
2093
|
}
|
|
2094
2094
|
for (let o = 31; o >= 0; o--) (r[o] = Number(i & 0xffn)), (i >>= 8n)
|
|
2095
2095
|
return r
|
|
2096
2096
|
}
|
|
2097
|
-
function uint256ToNumber(n,
|
|
2097
|
+
function uint256ToNumber(n, t) {
|
|
2098
2098
|
let r = 0n
|
|
2099
|
-
if (
|
|
2099
|
+
if (t === 'LE') {
|
|
2100
2100
|
for (let i = 31; i >= 0; i--) r = (r << 8n) | BigInt(n[i])
|
|
2101
2101
|
return r
|
|
2102
2102
|
}
|
|
2103
2103
|
for (let i = 0; i < 32; i++) r = (r << 8n) | BigInt(n[i])
|
|
2104
2104
|
return r
|
|
2105
2105
|
}
|
|
2106
|
-
function sliceBytes(n,
|
|
2107
|
-
const
|
|
2106
|
+
function sliceBytes(n, t) {
|
|
2107
|
+
const e = []
|
|
2108
2108
|
let r = 0
|
|
2109
|
-
for (const i of
|
|
2110
|
-
return
|
|
2109
|
+
for (const i of t) e.push(n.subarray(r, r + i)), (r += i)
|
|
2110
|
+
return e
|
|
2111
2111
|
}
|
|
2112
|
-
function partition(n,
|
|
2113
|
-
const
|
|
2114
|
-
for (let r = 0; r < n.length; r +=
|
|
2115
|
-
return
|
|
2112
|
+
function partition(n, t) {
|
|
2113
|
+
const e = []
|
|
2114
|
+
for (let r = 0; r < n.length; r += t) e.push(n.subarray(r, r + t))
|
|
2115
|
+
return e
|
|
2116
2116
|
}
|
|
2117
2117
|
const IOTA_CONSTANTS = [0, 1, 0, 32898, 2147483648, 32906, 2147483648, 2147516416, 0, 32907, 0, 2147483649, 2147483648, 2147516545, 2147483648, 32777, 0, 138, 0, 136, 0, 2147516425, 0, 2147483658, 0, 2147516555, 2147483648, 139, 2147483648, 32905, 2147483648, 32771, 2147483648, 32770, 2147483648, 128, 0, 32778, 2147483648, 2147483658, 2147483648, 2147516545, 2147483648, 32896, 0, 2147483649, 2147483648, 2147516424]
|
|
2118
2118
|
function keccakPermutate(n) {
|
|
2119
|
-
for (let
|
|
2120
|
-
const
|
|
2119
|
+
for (let t = 0; t < 24; t++) {
|
|
2120
|
+
const e = n[0] ^ n[10] ^ n[20] ^ n[30] ^ n[40],
|
|
2121
2121
|
r = n[1] ^ n[11] ^ n[21] ^ n[31] ^ n[41],
|
|
2122
2122
|
i = n[2] ^ n[12] ^ n[22] ^ n[32] ^ n[42],
|
|
2123
2123
|
o = n[3] ^ n[13] ^ n[23] ^ n[33] ^ n[43],
|
|
2124
2124
|
c = n[4] ^ n[14] ^ n[24] ^ n[34] ^ n[44],
|
|
2125
|
-
|
|
2126
|
-
|
|
2125
|
+
s = n[5] ^ n[15] ^ n[25] ^ n[35] ^ n[45],
|
|
2126
|
+
u = n[6] ^ n[16] ^ n[26] ^ n[36] ^ n[46],
|
|
2127
2127
|
f = n[7] ^ n[17] ^ n[27] ^ n[37] ^ n[47],
|
|
2128
2128
|
l = n[8] ^ n[18] ^ n[28] ^ n[38] ^ n[48],
|
|
2129
2129
|
a = n[9] ^ n[19] ^ n[29] ^ n[39] ^ n[49],
|
|
2130
2130
|
h = (i << 1) | (o >>> 31),
|
|
2131
|
-
|
|
2131
|
+
p = (o << 1) | (i >>> 31),
|
|
2132
2132
|
d = l ^ h,
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
En = (
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2133
|
+
m = a ^ p,
|
|
2134
|
+
w = (c << 1) | (s >>> 31),
|
|
2135
|
+
x = (s << 1) | (c >>> 31),
|
|
2136
|
+
g = e ^ w,
|
|
2137
|
+
y = r ^ x,
|
|
2138
|
+
En = (u << 1) | (f >>> 31),
|
|
2139
|
+
Mn = (f << 1) | (u >>> 31),
|
|
2140
|
+
b = i ^ En,
|
|
2141
|
+
A = o ^ Mn,
|
|
2142
|
+
Sn = (l << 1) | (a >>> 31),
|
|
2143
2143
|
kn = (a << 1) | (l >>> 31),
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
On = (
|
|
2147
|
-
Tn = (r << 1) | (
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
;(n[0] ^= d), (n[1] ^=
|
|
2151
|
-
const
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
;(n[0] =
|
|
2144
|
+
$ = c ^ Sn,
|
|
2145
|
+
E = s ^ kn,
|
|
2146
|
+
On = (e << 1) | (r >>> 31),
|
|
2147
|
+
Tn = (r << 1) | (e >>> 31),
|
|
2148
|
+
M = u ^ On,
|
|
2149
|
+
S = f ^ Tn
|
|
2150
|
+
;(n[0] ^= d), (n[1] ^= m), (n[2] ^= g), (n[3] ^= y), (n[4] ^= b), (n[5] ^= A), (n[6] ^= $), (n[7] ^= E), (n[8] ^= M), (n[9] ^= S), (n[10] ^= d), (n[11] ^= m), (n[12] ^= g), (n[13] ^= y), (n[14] ^= b), (n[15] ^= A), (n[16] ^= $), (n[17] ^= E), (n[18] ^= M), (n[19] ^= S), (n[20] ^= d), (n[21] ^= m), (n[22] ^= g), (n[23] ^= y), (n[24] ^= b), (n[25] ^= A), (n[26] ^= $), (n[27] ^= E), (n[28] ^= M), (n[29] ^= S), (n[30] ^= d), (n[31] ^= m), (n[32] ^= g), (n[33] ^= y), (n[34] ^= b), (n[35] ^= A), (n[36] ^= $), (n[37] ^= E), (n[38] ^= M), (n[39] ^= S), (n[40] ^= d), (n[41] ^= m), (n[42] ^= g), (n[43] ^= y), (n[44] ^= b), (n[45] ^= A), (n[46] ^= $), (n[47] ^= E), (n[48] ^= M), (n[49] ^= S)
|
|
2151
|
+
const k = n[0],
|
|
2152
|
+
O = n[1],
|
|
2153
|
+
T = (n[2] << 1) | (n[3] >>> 31),
|
|
2154
|
+
C = (n[3] << 1) | (n[2] >>> 31),
|
|
2155
|
+
R = (n[5] << 30) | (n[4] >>> 2),
|
|
2156
|
+
I = (n[4] << 30) | (n[5] >>> 2),
|
|
2157
|
+
D = (n[6] << 28) | (n[7] >>> 4),
|
|
2158
|
+
B = (n[7] << 28) | (n[6] >>> 4),
|
|
2159
|
+
P = (n[8] << 27) | (n[9] >>> 5),
|
|
2160
|
+
U = (n[9] << 27) | (n[8] >>> 5),
|
|
2161
|
+
v = (n[11] << 4) | (n[10] >>> 28),
|
|
2162
|
+
j = (n[10] << 4) | (n[11] >>> 28),
|
|
2163
|
+
L = (n[13] << 12) | (n[12] >>> 20),
|
|
2164
|
+
N = (n[12] << 12) | (n[13] >>> 20),
|
|
2165
|
+
F = (n[14] << 6) | (n[15] >>> 26),
|
|
2166
|
+
z = (n[15] << 6) | (n[14] >>> 26),
|
|
2167
|
+
W = (n[17] << 23) | (n[16] >>> 9),
|
|
2168
|
+
H = (n[16] << 23) | (n[17] >>> 9),
|
|
2169
|
+
q = (n[18] << 20) | (n[19] >>> 12),
|
|
2170
|
+
V = (n[19] << 20) | (n[18] >>> 12),
|
|
2171
|
+
J = (n[20] << 3) | (n[21] >>> 29),
|
|
2172
|
+
K = (n[21] << 3) | (n[20] >>> 29),
|
|
2173
|
+
Z = (n[22] << 10) | (n[23] >>> 22),
|
|
2174
|
+
Q = (n[23] << 10) | (n[22] >>> 22),
|
|
2175
|
+
G = (n[25] << 11) | (n[24] >>> 21),
|
|
2176
|
+
_ = (n[24] << 11) | (n[25] >>> 21),
|
|
2177
|
+
Y = (n[26] << 25) | (n[27] >>> 7),
|
|
2178
|
+
X = (n[27] << 25) | (n[26] >>> 7),
|
|
2179
|
+
nn = (n[29] << 7) | (n[28] >>> 25),
|
|
2180
|
+
tn = (n[28] << 7) | (n[29] >>> 25),
|
|
2181
|
+
en = (n[31] << 9) | (n[30] >>> 23),
|
|
2182
|
+
rn = (n[30] << 9) | (n[31] >>> 23),
|
|
2183
|
+
on = (n[33] << 13) | (n[32] >>> 19),
|
|
2184
|
+
cn = (n[32] << 13) | (n[33] >>> 19),
|
|
2185
|
+
sn = (n[34] << 15) | (n[35] >>> 17),
|
|
2186
|
+
un = (n[35] << 15) | (n[34] >>> 17),
|
|
2187
|
+
fn = (n[36] << 21) | (n[37] >>> 11),
|
|
2188
|
+
ln = (n[37] << 21) | (n[36] >>> 11),
|
|
2189
|
+
an = (n[38] << 8) | (n[39] >>> 24),
|
|
2190
|
+
hn = (n[39] << 8) | (n[38] >>> 24),
|
|
2191
|
+
dn = (n[40] << 18) | (n[41] >>> 14),
|
|
2192
|
+
pn = (n[41] << 18) | (n[40] >>> 14),
|
|
2193
|
+
mn = (n[42] << 2) | (n[43] >>> 30),
|
|
2194
|
+
gn = (n[43] << 2) | (n[42] >>> 30),
|
|
2195
|
+
wn = (n[45] << 29) | (n[44] >>> 3),
|
|
2196
|
+
xn = (n[44] << 29) | (n[45] >>> 3),
|
|
2197
|
+
yn = (n[47] << 24) | (n[46] >>> 8),
|
|
2198
|
+
bn = (n[46] << 24) | (n[47] >>> 8),
|
|
2199
|
+
An = (n[48] << 14) | (n[49] >>> 18),
|
|
2200
|
+
$n = (n[49] << 14) | (n[48] >>> 18)
|
|
2201
|
+
;(n[0] = k ^ (~L & G)), (n[1] = O ^ (~N & _)), (n[2] = L ^ (~G & fn)), (n[3] = N ^ (~_ & ln)), (n[4] = G ^ (~fn & An)), (n[5] = _ ^ (~ln & $n)), (n[6] = fn ^ (~An & k)), (n[7] = ln ^ (~$n & O)), (n[8] = An ^ (~k & L)), (n[9] = $n ^ (~O & N)), (n[10] = D ^ (~q & J)), (n[11] = B ^ (~V & K)), (n[12] = q ^ (~J & on)), (n[13] = V ^ (~K & cn)), (n[14] = J ^ (~on & wn)), (n[15] = K ^ (~cn & xn)), (n[16] = on ^ (~wn & D)), (n[17] = cn ^ (~xn & B)), (n[18] = wn ^ (~D & q)), (n[19] = xn ^ (~B & V)), (n[20] = T ^ (~F & Y)), (n[21] = C ^ (~z & X)), (n[22] = F ^ (~Y & an)), (n[23] = z ^ (~X & hn)), (n[24] = Y ^ (~an & dn)), (n[25] = X ^ (~hn & pn)), (n[26] = an ^ (~dn & T)), (n[27] = hn ^ (~pn & C)), (n[28] = dn ^ (~T & F)), (n[29] = pn ^ (~C & z)), (n[30] = P ^ (~v & Z)), (n[31] = U ^ (~j & Q)), (n[32] = v ^ (~Z & sn)), (n[33] = j ^ (~Q & un)), (n[34] = Z ^ (~sn & yn)), (n[35] = Q ^ (~un & bn)), (n[36] = sn ^ (~yn & P)), (n[37] = un ^ (~bn & U)), (n[38] = yn ^ (~P & v)), (n[39] = bn ^ (~U & j)), (n[40] = R ^ (~W & nn)), (n[41] = I ^ (~H & tn)), (n[42] = W ^ (~nn & en)), (n[43] = H ^ (~tn & rn)), (n[44] = nn ^ (~en & mn)), (n[45] = tn ^ (~rn & gn)), (n[46] = en ^ (~mn & R)), (n[47] = rn ^ (~gn & I)), (n[48] = mn ^ (~R & W)), (n[49] = gn ^ (~I & H)), (n[0] ^= IOTA_CONSTANTS[t * 2]), (n[1] ^= IOTA_CONSTANTS[t * 2 + 1])
|
|
2202
2202
|
}
|
|
2203
2203
|
}
|
|
2204
2204
|
function bytesToNumbers(n) {
|
|
2205
|
-
const
|
|
2206
|
-
for (let
|
|
2207
|
-
return
|
|
2205
|
+
const t = []
|
|
2206
|
+
for (let e = 0; e < n.length; e += 4) t.push(n[e] | (n[e + 1] << 8) | (n[e + 2] << 16) | (n[e + 3] << 24))
|
|
2207
|
+
return t
|
|
2208
2208
|
}
|
|
2209
|
-
function divideToBlocks(n,
|
|
2209
|
+
function divideToBlocks(n, t) {
|
|
2210
2210
|
if (!n.length) {
|
|
2211
2211
|
const i = new Uint8Array(136)
|
|
2212
|
-
return (i[0] =
|
|
2212
|
+
return (i[0] = t), (i[135] = 128), [bytesToNumbers(i)]
|
|
2213
2213
|
}
|
|
2214
|
-
const
|
|
2215
|
-
r =
|
|
2214
|
+
const e = partition(n, 136),
|
|
2215
|
+
r = e[e.length - 1]
|
|
2216
2216
|
if (r.length < 136) {
|
|
2217
2217
|
const i = new Uint8Array(136)
|
|
2218
|
-
i.set(r), (i[r.length] =
|
|
2218
|
+
i.set(r), (i[r.length] = t), (i[135] |= 128), (e[e.length - 1] = i)
|
|
2219
2219
|
}
|
|
2220
2220
|
if (r.length === 136) {
|
|
2221
2221
|
const i = new Uint8Array(136)
|
|
2222
|
-
;(i[0] =
|
|
2222
|
+
;(i[0] = t), (i[135] = 128), e.push(i)
|
|
2223
2223
|
}
|
|
2224
|
-
return
|
|
2224
|
+
return e.map(bytesToNumbers)
|
|
2225
2225
|
}
|
|
2226
|
-
function absorb(n,
|
|
2227
|
-
for (const
|
|
2228
|
-
for (let r = 0; r < 34; r += 2) (n[r] ^=
|
|
2226
|
+
function absorb(n, t) {
|
|
2227
|
+
for (const e of t) {
|
|
2228
|
+
for (let r = 0; r < 34; r += 2) (n[r] ^= e[r + 1]), (n[r + 1] ^= e[r])
|
|
2229
2229
|
keccakPermutate(n)
|
|
2230
2230
|
}
|
|
2231
2231
|
return n
|
|
@@ -2233,248 +2233,364 @@ function absorb(n, e) {
|
|
|
2233
2233
|
function squeeze(n) {
|
|
2234
2234
|
return new Uint8Array([n[1], n[1] >> -24, n[1] >> -16, n[1] >> -8, n[0], n[0] >> 8, n[0] >> 16, n[0] >> 24, n[3], n[3] >> -24, n[3] >> -16, n[3] >> -8, n[2], n[2] >> 8, n[2] >> 16, n[2] >> 24, n[5], n[5] >> -24, n[5] >> -16, n[5] >> -8, n[4], n[4] >> 8, n[4] >> 16, n[4] >> 24, n[7], n[7] >> -24, n[7] >> -16, n[7] >> -8, n[6], n[6] >> 8, n[6] >> 16, n[6] >> 24])
|
|
2235
2235
|
}
|
|
2236
|
-
function squeezeInto(n,
|
|
2237
|
-
;(e
|
|
2236
|
+
function squeezeInto(n, t, e) {
|
|
2237
|
+
;(t[e] = n[1]), (t[e + 1] = n[1] >> 8), (t[e + 2] = n[1] >> 16), (t[e + 3] = n[1] >> 24), (t[e + 4] = n[0]), (t[e + 5] = n[0] >> 8), (t[e + 6] = n[0] >> 16), (t[e + 7] = n[0] >> 24), (t[e + 8] = n[3]), (t[e + 9] = n[3] >> 8), (t[e + 10] = n[3] >> 16), (t[e + 11] = n[3] >> 24), (t[e + 12] = n[2]), (t[e + 13] = n[2] >> 8), (t[e + 14] = n[2] >> 16), (t[e + 15] = n[2] >> 24), (t[e + 16] = n[5]), (t[e + 17] = n[5] >> 8), (t[e + 18] = n[5] >> 16), (t[e + 19] = n[5] >> 24), (t[e + 20] = n[4]), (t[e + 21] = n[4] >> 8), (t[e + 22] = n[4] >> 16), (t[e + 23] = n[4] >> 24), (t[e + 24] = n[7]), (t[e + 25] = n[7] >> 8), (t[e + 26] = n[7] >> 16), (t[e + 27] = n[7] >> 24), (t[e + 28] = n[6]), (t[e + 29] = n[6] >> 8), (t[e + 30] = n[6] >> 16), (t[e + 31] = n[6] >> 24)
|
|
2238
2238
|
}
|
|
2239
2239
|
function keccak256(n) {
|
|
2240
2240
|
return squeeze(absorb(new Array(50).fill(0), divideToBlocks(n, 1)))
|
|
2241
2241
|
}
|
|
2242
2242
|
function bmtRoot(n) {
|
|
2243
|
-
const
|
|
2244
|
-
|
|
2243
|
+
const t = new Uint8Array(n),
|
|
2244
|
+
e = new Array(50).fill(0),
|
|
2245
2245
|
r = new Uint8Array(136)
|
|
2246
2246
|
;(r[64] = 1), (r[135] = 128)
|
|
2247
|
-
let i =
|
|
2247
|
+
let i = t.length >>> 5
|
|
2248
2248
|
for (; i > 1; ) {
|
|
2249
2249
|
const o = i >>> 1
|
|
2250
2250
|
for (let c = 0; c < o; c++) {
|
|
2251
|
-
r.set(
|
|
2252
|
-
for (let
|
|
2253
|
-
keccakPermutate(
|
|
2251
|
+
r.set(t.subarray(c << 6, (c + 1) << 6)), e.fill(0)
|
|
2252
|
+
for (let s = 0, u = 0; s < 34; s += 2, u += 8) (e[s] ^= r[u + 4] | (r[u + 5] << 8) | (r[u + 6] << 16) | (r[u + 7] << 24)), (e[s + 1] ^= r[u] | (r[u + 1] << 8) | (r[u + 2] << 16) | (r[u + 3] << 24))
|
|
2253
|
+
keccakPermutate(e), squeezeInto(e, t, c << 5)
|
|
2254
2254
|
}
|
|
2255
2255
|
i = o
|
|
2256
2256
|
}
|
|
2257
|
-
return
|
|
2257
|
+
return t.subarray(0, 32)
|
|
2258
2258
|
}
|
|
2259
2259
|
const SPAN_ENCRYPT_INIT_CTR = 128
|
|
2260
|
-
function encryptSegments(n,
|
|
2261
|
-
const r = new Uint8Array(
|
|
2260
|
+
function encryptSegments(n, t, e) {
|
|
2261
|
+
const r = new Uint8Array(e.length),
|
|
2262
2262
|
i = new Uint8Array(36)
|
|
2263
2263
|
i.set(n)
|
|
2264
|
-
for (let o = 0, c = 0; c <
|
|
2265
|
-
const
|
|
2266
|
-
;(i[32] =
|
|
2267
|
-
const
|
|
2268
|
-
f = Math.min(c + 32,
|
|
2269
|
-
for (let l = c; l < f; l++) r[l] =
|
|
2264
|
+
for (let o = 0, c = 0; c < e.length; o++, c += 32) {
|
|
2265
|
+
const s = (t + o) >>> 0
|
|
2266
|
+
;(i[32] = s & 255), (i[33] = (s >>> 8) & 255), (i[34] = (s >>> 16) & 255), (i[35] = (s >>> 24) & 255)
|
|
2267
|
+
const u = keccak256(keccak256(i)),
|
|
2268
|
+
f = Math.min(c + 32, e.length)
|
|
2269
|
+
for (let l = c; l < f; l++) r[l] = e[l] ^ u[l - c]
|
|
2270
2270
|
}
|
|
2271
2271
|
return r
|
|
2272
2272
|
}
|
|
2273
|
-
function encryptSpan(n,
|
|
2274
|
-
return encryptSegments(n, SPAN_ENCRYPT_INIT_CTR,
|
|
2273
|
+
function encryptSpan(n, t) {
|
|
2274
|
+
return encryptSegments(n, SPAN_ENCRYPT_INIT_CTR, t)
|
|
2275
2275
|
}
|
|
2276
|
-
function encryptData(n,
|
|
2277
|
-
return encryptSegments(n, 0,
|
|
2276
|
+
function encryptData(n, t) {
|
|
2277
|
+
return encryptSegments(n, 0, t)
|
|
2278
2278
|
}
|
|
2279
|
-
function decryptChunk(n,
|
|
2280
|
-
return { span: uint64ToNumber(encryptSpan(
|
|
2279
|
+
function decryptChunk(n, t) {
|
|
2280
|
+
return { span: uint64ToNumber(encryptSpan(t, n.subarray(0, 8)), 'LE'), data: encryptData(t, n.subarray(8, 4104)) }
|
|
2281
2281
|
}
|
|
2282
2282
|
function sha3_256(n) {
|
|
2283
2283
|
return squeeze(absorb(new Array(50).fill(0), divideToBlocks(n, 6)))
|
|
2284
2284
|
}
|
|
2285
|
-
function proximity(n,
|
|
2286
|
-
const
|
|
2287
|
-
for (let r = 0; r <
|
|
2288
|
-
const i = n[r] ^
|
|
2285
|
+
function proximity(n, t) {
|
|
2286
|
+
const e = Math.min(n.length, t.length)
|
|
2287
|
+
for (let r = 0; r < e; r++) {
|
|
2288
|
+
const i = n[r] ^ t[r]
|
|
2289
2289
|
for (let o = 0; o < 8; o++) if ((i >> (7 - o)) & 1) return r * 8 + o
|
|
2290
2290
|
}
|
|
2291
|
-
return Math.min(n.length,
|
|
2291
|
+
return Math.min(n.length, t.length) * 8
|
|
2292
2292
|
}
|
|
2293
|
-
function commonPrefix(n,
|
|
2294
|
-
const
|
|
2295
|
-
for (let r = 0; r <
|
|
2296
|
-
return n.subarray(0,
|
|
2293
|
+
function commonPrefix(n, t) {
|
|
2294
|
+
const e = Math.min(n.length, t.length)
|
|
2295
|
+
for (let r = 0; r < e; r++) if (n[r] !== t[r]) return n.subarray(0, r)
|
|
2296
|
+
return n.subarray(0, e)
|
|
2297
2297
|
}
|
|
2298
|
-
function setBit(n,
|
|
2299
|
-
const i = Math.floor(
|
|
2300
|
-
o =
|
|
2301
|
-
|
|
2298
|
+
function setBit(n, t, e, r) {
|
|
2299
|
+
const i = Math.floor(t / 8),
|
|
2300
|
+
o = t % 8
|
|
2301
|
+
e === 1 ? (n[i] |= 1 << (r === 'BE' ? 7 - o : o)) : (n[i] &= ~(1 << (r === 'BE' ? 7 - o : o)))
|
|
2302
2302
|
}
|
|
2303
|
-
function getBit(n,
|
|
2304
|
-
const r = Math.floor(
|
|
2305
|
-
i =
|
|
2306
|
-
return (n[r] >> (
|
|
2303
|
+
function getBit(n, t, e) {
|
|
2304
|
+
const r = Math.floor(t / 8),
|
|
2305
|
+
i = t % 8
|
|
2306
|
+
return (n[r] >> (e === 'BE' ? 7 - i : i)) & 1
|
|
2307
2307
|
}
|
|
2308
|
-
function binaryIndexOf(n,
|
|
2309
|
-
for (let r =
|
|
2308
|
+
function binaryIndexOf(n, t, e = 0) {
|
|
2309
|
+
for (let r = e; r < n.length; r++) for (let i = 0; i < t.length && n[r + i] === t[i]; i++) if (i === t.length - 1) return r
|
|
2310
2310
|
return -1
|
|
2311
2311
|
}
|
|
2312
|
-
function binaryPadStart(n,
|
|
2313
|
-
if (n.length >=
|
|
2314
|
-
const r = new Uint8Array(
|
|
2315
|
-
return r.fill(
|
|
2312
|
+
function binaryPadStart(n, t, e = 0) {
|
|
2313
|
+
if (n.length >= t) return n
|
|
2314
|
+
const r = new Uint8Array(t)
|
|
2315
|
+
return r.fill(e), r.set(n, t - n.length), r
|
|
2316
2316
|
}
|
|
2317
|
-
function binaryPadStartToMultiple(n,
|
|
2318
|
-
const r = n.length %
|
|
2319
|
-
return r === 0 ? n : binaryPadStart(n, n.length +
|
|
2317
|
+
function binaryPadStartToMultiple(n, t, e = 0) {
|
|
2318
|
+
const r = n.length % t
|
|
2319
|
+
return r === 0 ? n : binaryPadStart(n, n.length + t - r, e)
|
|
2320
2320
|
}
|
|
2321
|
-
function binaryPadEnd(n,
|
|
2322
|
-
if (n.length >=
|
|
2323
|
-
const r = new Uint8Array(
|
|
2324
|
-
return r.fill(
|
|
2321
|
+
function binaryPadEnd(n, t, e = 0) {
|
|
2322
|
+
if (n.length >= t) return n
|
|
2323
|
+
const r = new Uint8Array(t)
|
|
2324
|
+
return r.fill(e), r.set(n, 0), r
|
|
2325
2325
|
}
|
|
2326
|
-
function binaryPadEndToMultiple(n,
|
|
2327
|
-
const r = n.length %
|
|
2328
|
-
return r === 0 ? n : binaryPadEnd(n, n.length +
|
|
2326
|
+
function binaryPadEndToMultiple(n, t, e = 0) {
|
|
2327
|
+
const r = n.length % t
|
|
2328
|
+
return r === 0 ? n : binaryPadEnd(n, n.length + t - r, e)
|
|
2329
2329
|
}
|
|
2330
|
-
function xorCypher(n,
|
|
2331
|
-
const
|
|
2332
|
-
for (let r = 0; r < n.length; r++)
|
|
2333
|
-
return
|
|
2330
|
+
function xorCypher(n, t) {
|
|
2331
|
+
const e = new Uint8Array(n.length)
|
|
2332
|
+
for (let r = 0; r < n.length; r++) e[r] = n[r] ^ t[r % t.length]
|
|
2333
|
+
return e
|
|
2334
2334
|
}
|
|
2335
2335
|
function isUtf8(n) {
|
|
2336
|
-
for (let
|
|
2337
|
-
const
|
|
2338
|
-
if (!(
|
|
2339
|
-
if ((
|
|
2340
|
-
if (
|
|
2341
|
-
|
|
2342
|
-
} else if ((
|
|
2343
|
-
if (
|
|
2344
|
-
|
|
2345
|
-
} else if ((
|
|
2346
|
-
if (
|
|
2347
|
-
|
|
2336
|
+
for (let t = 0; t < n.length; t++) {
|
|
2337
|
+
const e = n[t]
|
|
2338
|
+
if (!(e < 128))
|
|
2339
|
+
if ((e & 224) === 192) {
|
|
2340
|
+
if (t + 1 >= n.length || (n[t + 1] & 192) !== 128) return !1
|
|
2341
|
+
t += 1
|
|
2342
|
+
} else if ((e & 240) === 224) {
|
|
2343
|
+
if (t + 2 >= n.length || (n[t + 1] & 192) !== 128 || (n[t + 2] & 192) !== 128) return !1
|
|
2344
|
+
t += 2
|
|
2345
|
+
} else if ((e & 248) === 240) {
|
|
2346
|
+
if (t + 3 >= n.length || (n[t + 1] & 192) !== 128 || (n[t + 2] & 192) !== 128 || (n[t + 3] & 192) !== 128) return !1
|
|
2347
|
+
t += 3
|
|
2348
2348
|
} else return !1
|
|
2349
2349
|
}
|
|
2350
2350
|
return !0
|
|
2351
2351
|
}
|
|
2352
|
-
function binaryEquals(n,
|
|
2353
|
-
if (n.length !==
|
|
2354
|
-
for (let
|
|
2352
|
+
function binaryEquals(n, t) {
|
|
2353
|
+
if (n.length !== t.length) return !1
|
|
2354
|
+
for (let e = 0; e < n.length; e++) if (n[e] !== t[e]) return !1
|
|
2355
2355
|
return !0
|
|
2356
2356
|
}
|
|
2357
|
-
function mod(n,
|
|
2358
|
-
return ((n %
|
|
2357
|
+
function mod(n, t) {
|
|
2358
|
+
return ((n % t) + t) % t
|
|
2359
2359
|
}
|
|
2360
|
-
function modInverse(n,
|
|
2361
|
-
n = mod(n,
|
|
2362
|
-
let [
|
|
2363
|
-
[i, o] = [
|
|
2360
|
+
function modInverse(n, t) {
|
|
2361
|
+
n = mod(n, t)
|
|
2362
|
+
let [e, r] = [0n, 1n],
|
|
2363
|
+
[i, o] = [t, n]
|
|
2364
2364
|
for (; o !== 0n; ) {
|
|
2365
2365
|
const c = i / o
|
|
2366
|
-
;([
|
|
2366
|
+
;([e, r] = [r, e - c * r]), ([i, o] = [o, i - c * o])
|
|
2367
2367
|
}
|
|
2368
2368
|
if (i > 1n) throw new Error('a is not invertible')
|
|
2369
|
-
return
|
|
2369
|
+
return e < 0n && (e += t), e
|
|
2370
2370
|
}
|
|
2371
|
-
function modPow(n,
|
|
2371
|
+
function modPow(n, t, e) {
|
|
2372
2372
|
let r = 1n
|
|
2373
|
-
for (n = mod(n,
|
|
2373
|
+
for (n = mod(n, e); t > 0; ) t % 2n === 1n && (r = mod(r * n, e)), (n = mod(n * n, e)), (t = t / 2n)
|
|
2374
2374
|
return r
|
|
2375
2375
|
}
|
|
2376
|
-
function modSqrt(n,
|
|
2377
|
-
return mod(n,
|
|
2376
|
+
function modSqrt(n, t) {
|
|
2377
|
+
return mod(n, t) === 0n ? 0n : t % 4n === 3n ? modPow(n, (t + 1n) / 4n, t) : null
|
|
2378
2378
|
}
|
|
2379
2379
|
const SECP256K1_P = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,
|
|
2380
2380
|
SECP256K1_N = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,
|
|
2381
2381
|
SECP256K1_X = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,
|
|
2382
|
-
SECP256K1_Y = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2382
|
+
SECP256K1_Y = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n,
|
|
2383
|
+
SECP256K1_BETA = 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501een
|
|
2384
|
+
function ellipticDouble(n, t, e) {
|
|
2385
|
+
if (t === 0n) return [0n, 0n]
|
|
2386
|
+
const r = mod(3n * n * n * modInverse(2n * t, e), e),
|
|
2387
|
+
i = mod(r * r - 2n * n, e),
|
|
2388
|
+
o = mod(r * (n - i) - t, e)
|
|
2388
2389
|
return [i, o]
|
|
2389
2390
|
}
|
|
2390
|
-
function ellipticAdd(n,
|
|
2391
|
-
if (n === 0n &&
|
|
2392
|
-
if (
|
|
2393
|
-
if (n ===
|
|
2394
|
-
if (n ===
|
|
2395
|
-
const o = mod((r -
|
|
2396
|
-
c = mod(o * o - n -
|
|
2397
|
-
|
|
2398
|
-
return [c,
|
|
2391
|
+
function ellipticAdd(n, t, e, r, i) {
|
|
2392
|
+
if (n === 0n && t === 0n) return [e, r]
|
|
2393
|
+
if (e === 0n && r === 0n) return [n, t]
|
|
2394
|
+
if (n === e && t === mod(-r, i)) return [0n, 0n]
|
|
2395
|
+
if (n === e && t === r) return ellipticDouble(n, t, i)
|
|
2396
|
+
const o = mod((r - t) * modInverse(e - n, i), i),
|
|
2397
|
+
c = mod(o * o - n - e, i),
|
|
2398
|
+
s = mod(o * (n - c) - t, i)
|
|
2399
|
+
return [c, s]
|
|
2399
2400
|
}
|
|
2400
2401
|
function privateKeyToPublicKey(n) {
|
|
2401
2402
|
if (n <= 0n || n >= SECP256K1_N) throw new Error('Invalid private key')
|
|
2402
|
-
return doubleAndAdd(SECP256K1_X, SECP256K1_Y, n
|
|
2403
|
+
return doubleAndAdd(SECP256K1_X, SECP256K1_Y, n)
|
|
2403
2404
|
}
|
|
2404
2405
|
function compressPublicKey(n) {
|
|
2405
|
-
const
|
|
2406
|
-
return new Uint8Array([
|
|
2406
|
+
const e = n[1] % 2n === 0n ? 2 : 3
|
|
2407
|
+
return new Uint8Array([e, ...numberToUint256(n[0], 'BE')])
|
|
2407
2408
|
}
|
|
2408
2409
|
function publicKeyFromCompressed(n) {
|
|
2409
2410
|
if (n.length !== 33 || (n[0] !== 2 && n[0] !== 3)) throw new Error('Invalid compressed public key')
|
|
2410
|
-
const
|
|
2411
|
-
|
|
2412
|
-
if (!
|
|
2413
|
-
const r = mod(-
|
|
2414
|
-
i =
|
|
2415
|
-
return [
|
|
2411
|
+
const t = uint256ToNumber(n.slice(1), 'BE'),
|
|
2412
|
+
e = modSqrt(mod(t ** 3n + 7n, SECP256K1_P), SECP256K1_P)
|
|
2413
|
+
if (!e) throw Error('Invalid x: does not correspond to a valid curve point')
|
|
2414
|
+
const r = mod(-e, SECP256K1_P),
|
|
2415
|
+
i = e % 2n === 0n
|
|
2416
|
+
return [t, n[0] === 2 ? (i ? e : r) : i ? r : e]
|
|
2416
2417
|
}
|
|
2417
2418
|
function publicKeyToAddress(n) {
|
|
2418
|
-
const
|
|
2419
|
-
|
|
2420
|
-
return
|
|
2419
|
+
const t = new Uint8Array(20),
|
|
2420
|
+
e = keccak256(concatBytes(numberToUint256(n[0], 'BE'), numberToUint256(n[1], 'BE')))
|
|
2421
|
+
return t.set(e.subarray(12)), t
|
|
2421
2422
|
}
|
|
2422
2423
|
function checksumEncode(n) {
|
|
2423
|
-
const
|
|
2424
|
-
|
|
2424
|
+
const t = exports.Binary.uint8ArrayToHex(n),
|
|
2425
|
+
e = exports.Binary.uint8ArrayToHex(exports.Binary.keccak256(new Uint8Array([...t].map(i => i.charCodeAt(0)))))
|
|
2425
2426
|
let r = '0x'
|
|
2426
|
-
for (let i = 0; i <
|
|
2427
|
+
for (let i = 0; i < t.length; i++) parseInt(e[i], 16) > 7 ? (r += t[i].toUpperCase()) : (r += t[i])
|
|
2427
2428
|
return r
|
|
2428
2429
|
}
|
|
2429
|
-
function
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2430
|
+
function jacobianDouble(n, t, e) {
|
|
2431
|
+
if (e === 0n) return [0n, 0n, 0n]
|
|
2432
|
+
const r = SECP256K1_P,
|
|
2433
|
+
i = (t * t) % r,
|
|
2434
|
+
o = (4n * n * i) % r,
|
|
2435
|
+
c = (3n * n * n) % r,
|
|
2436
|
+
s = mod(c * c - 2n * o, r),
|
|
2437
|
+
u = mod(c * (o - s) - 8n * i * i, r),
|
|
2438
|
+
f = (2n * t * e) % r
|
|
2439
|
+
return [s, u, f]
|
|
2440
|
+
}
|
|
2441
|
+
function jacobianAddMixed(n, t, e, r, i) {
|
|
2442
|
+
if (e === 0n) return [r, i, 1n]
|
|
2443
|
+
const o = SECP256K1_P,
|
|
2444
|
+
c = (e * e) % o,
|
|
2445
|
+
s = (r * c) % o,
|
|
2446
|
+
u = (((i * c) % o) * e) % o,
|
|
2447
|
+
f = mod(s - n, o),
|
|
2448
|
+
l = mod(u - t, o)
|
|
2449
|
+
if (f === 0n) return l === 0n ? jacobianDouble(n, t, e) : [0n, 0n, 0n]
|
|
2450
|
+
const a = (f * f) % o,
|
|
2451
|
+
h = (f * a) % o,
|
|
2452
|
+
p = (n * a) % o,
|
|
2453
|
+
d = mod(l * l - h - 2n * p, o),
|
|
2454
|
+
m = mod(l * (p - d) - t * h, o),
|
|
2455
|
+
w = (f * e) % o
|
|
2456
|
+
return [d, m, w]
|
|
2457
|
+
}
|
|
2458
|
+
function jacobianToAffine(n, t, e) {
|
|
2459
|
+
if (e === 0n) return [0n, 0n]
|
|
2460
|
+
const r = SECP256K1_P,
|
|
2461
|
+
i = modInverse(e, r),
|
|
2462
|
+
o = (i * i) % r
|
|
2463
|
+
return [(n * o) % r, (((t * o) % r) * i) % r]
|
|
2464
|
+
}
|
|
2465
|
+
function glvDecompose(n) {
|
|
2466
|
+
const t = SECP256K1_N,
|
|
2467
|
+
e = 0x3086d221a7d46bcde86c90e49284eb15n,
|
|
2468
|
+
r = 0xe4437ed6010e88286f547fa90abfe4c3n,
|
|
2469
|
+
i = 0x114ca50f7a8e2f3f657c1108d9d44cfd8n,
|
|
2470
|
+
o = 0x3086d221a7d46bcde86c90e49284eb15n,
|
|
2471
|
+
c = (o * n + t / 2n) / t,
|
|
2472
|
+
s = (r * n + t / 2n) / t
|
|
2473
|
+
let u = mod(n - c * e - s * i, t),
|
|
2474
|
+
f = mod(c * r - s * o, t)
|
|
2475
|
+
const l = u > 1n << 128n,
|
|
2476
|
+
a = f > 1n << 128n
|
|
2477
|
+
return l && (u = t - u), a && (f = t - f), [u, l, f, a]
|
|
2478
|
+
}
|
|
2479
|
+
const WNAF_W = 4
|
|
2480
|
+
function toWNAF(n) {
|
|
2481
|
+
const t = 1 << WNAF_W,
|
|
2482
|
+
e = t >> 1,
|
|
2483
|
+
r = BigInt(t - 1),
|
|
2484
|
+
i = []
|
|
2485
|
+
for (; n > 0n; ) {
|
|
2486
|
+
if (n & 1n) {
|
|
2487
|
+
let o = Number(n & r)
|
|
2488
|
+
o >= e && (o -= t), i.push(o), (n -= BigInt(o))
|
|
2489
|
+
} else i.push(0)
|
|
2490
|
+
n >>= 1n
|
|
2491
|
+
}
|
|
2434
2492
|
return i
|
|
2435
2493
|
}
|
|
2436
|
-
function
|
|
2437
|
-
|
|
2494
|
+
function batchToAffine(n) {
|
|
2495
|
+
const t = SECP256K1_P,
|
|
2496
|
+
e = n.length
|
|
2497
|
+
if (e === 1) return [jacobianToAffine(n[0][0], n[0][1], n[0][2])]
|
|
2498
|
+
const r = new Array(e)
|
|
2499
|
+
r[0] = n[0][2]
|
|
2500
|
+
for (let s = 1; s < e; s++) r[s] = (r[s - 1] * n[s][2]) % t
|
|
2501
|
+
let i = modInverse(r[e - 1], t)
|
|
2502
|
+
const o = new Array(e)
|
|
2503
|
+
for (let s = e - 1; s > 0; s--) {
|
|
2504
|
+
const u = (i * r[s - 1]) % t,
|
|
2505
|
+
f = (u * u) % t
|
|
2506
|
+
;(o[s] = [(n[s][0] * f) % t, (((n[s][1] * f) % t) * u) % t]), (i = (i * n[s][2]) % t)
|
|
2507
|
+
}
|
|
2508
|
+
const c = (i * i) % t
|
|
2509
|
+
return (o[0] = [(n[0][0] * c) % t, (((n[0][1] * c) % t) * i) % t]), o
|
|
2510
|
+
}
|
|
2511
|
+
function precomputeOddMultiples(n, t) {
|
|
2512
|
+
const e = 1 << (WNAF_W - 2),
|
|
2513
|
+
[r, i] = jacobianToAffine(...jacobianDouble(n, t, 1n)),
|
|
2514
|
+
o = [[n, t, 1n]]
|
|
2515
|
+
for (let c = 1; c < e; c++) {
|
|
2516
|
+
const [s, u, f] = o[c - 1]
|
|
2517
|
+
o.push(jacobianAddMixed(s, u, f, r, i))
|
|
2518
|
+
}
|
|
2519
|
+
return batchToAffine(o)
|
|
2520
|
+
}
|
|
2521
|
+
function shamirWNAF(n, t, e, r, i, o) {
|
|
2522
|
+
const c = toWNAF(n),
|
|
2523
|
+
s = toWNAF(r),
|
|
2524
|
+
u = precomputeOddMultiples(t, e),
|
|
2525
|
+
f = precomputeOddMultiples(i, o),
|
|
2526
|
+
l = Math.max(c.length, s.length)
|
|
2527
|
+
let a = 0n,
|
|
2528
|
+
h = 0n,
|
|
2529
|
+
p = 0n
|
|
2530
|
+
for (let d = l - 1; d >= 0; d--) {
|
|
2531
|
+
;[a, h, p] = jacobianDouble(a, h, p)
|
|
2532
|
+
const m = d < c.length ? c[d] : 0
|
|
2533
|
+
if (m !== 0) {
|
|
2534
|
+
const [x, g] = u[(Math.abs(m) - 1) >> 1]
|
|
2535
|
+
;[a, h, p] = jacobianAddMixed(a, h, p, x, m > 0 ? g : SECP256K1_P - g)
|
|
2536
|
+
}
|
|
2537
|
+
const w = d < s.length ? s[d] : 0
|
|
2538
|
+
if (w !== 0) {
|
|
2539
|
+
const [x, g] = f[(Math.abs(w) - 1) >> 1]
|
|
2540
|
+
;[a, h, p] = jacobianAddMixed(a, h, p, x, w > 0 ? g : SECP256K1_P - g)
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
return jacobianToAffine(a, h, p)
|
|
2544
|
+
}
|
|
2545
|
+
function doubleAndAdd(n, t, e) {
|
|
2546
|
+
const [r, i, o, c] = glvDecompose(e),
|
|
2547
|
+
s = i ? SECP256K1_P - t : t,
|
|
2548
|
+
u = (SECP256K1_BETA * n) % SECP256K1_P,
|
|
2549
|
+
f = c ? SECP256K1_P - t : t
|
|
2550
|
+
return shamirWNAF(r, n, s, o, u, f)
|
|
2438
2551
|
}
|
|
2439
|
-
function
|
|
2440
|
-
|
|
2441
|
-
|
|
2552
|
+
function signMessage(n, t, e) {
|
|
2553
|
+
return signHash(uint256ToNumber(keccak256(n), 'BE'), t, e)
|
|
2554
|
+
}
|
|
2555
|
+
function signHash(n, t, e) {
|
|
2556
|
+
if (t <= 0n || t >= SECP256K1_N) throw new Error('Invalid private key')
|
|
2557
|
+
if ((e || (e = mod(uint256ToNumber(keccak256(concatBytes(keccak256(numberToUint256(t, 'BE')), numberToUint256(n, 'BE'))), 'BE'), SECP256K1_N)), e <= 0n || e >= SECP256K1_N)) throw new Error('Invalid nonce')
|
|
2442
2558
|
const r = mod(n, SECP256K1_N),
|
|
2443
|
-
i = doubleAndAdd(SECP256K1_X, SECP256K1_Y,
|
|
2559
|
+
i = doubleAndAdd(SECP256K1_X, SECP256K1_Y, e),
|
|
2444
2560
|
o = mod(i[0], SECP256K1_N)
|
|
2445
|
-
let c = mod((r + mod(o, SECP256K1_N) *
|
|
2561
|
+
let c = mod((r + mod(o, SECP256K1_N) * t) * modInverse(e, SECP256K1_N), SECP256K1_N)
|
|
2446
2562
|
if (o === 0n || c === 0n) throw new Error('Invalid r or s value')
|
|
2447
|
-
let
|
|
2448
|
-
return c > SECP256K1_N / 2n && ((c = SECP256K1_N - c), (
|
|
2563
|
+
let s = i[1] % 2n === 0n ? 27n : 28n
|
|
2564
|
+
return c > SECP256K1_N / 2n && ((c = SECP256K1_N - c), (s = s === 27n ? 28n : 27n)), [o, c, s]
|
|
2449
2565
|
}
|
|
2450
|
-
function recoverPublicKey(n,
|
|
2451
|
-
const i = modSqrt(mod(
|
|
2566
|
+
function recoverPublicKey(n, t, e, r) {
|
|
2567
|
+
const i = modSqrt(mod(t ** 3n + 7n, SECP256K1_P), SECP256K1_P)
|
|
2452
2568
|
if (!i) throw new Error('Invalid r: does not correspond to a valid curve point')
|
|
2453
2569
|
const o = r === 27n ? 0n : 1n,
|
|
2454
2570
|
c = i % 2n === o ? i : SECP256K1_P - i,
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
f = doubleAndAdd(SECP256K1_X, SECP256K1_Y,
|
|
2458
|
-
l = ellipticAdd(
|
|
2459
|
-
return doubleAndAdd(l[0], l[1], modInverse(
|
|
2571
|
+
s = mod(uint256ToNumber(keccak256(n), 'BE'), SECP256K1_N),
|
|
2572
|
+
u = doubleAndAdd(t, c, e),
|
|
2573
|
+
f = doubleAndAdd(SECP256K1_X, SECP256K1_Y, s),
|
|
2574
|
+
l = ellipticAdd(u[0], u[1], f[0], mod(-f[1], SECP256K1_P), SECP256K1_P)
|
|
2575
|
+
return doubleAndAdd(l[0], l[1], modInverse(t, SECP256K1_N))
|
|
2460
2576
|
}
|
|
2461
|
-
function verifySignature(n,
|
|
2577
|
+
function verifySignature(n, t, e, r) {
|
|
2462
2578
|
const i = mod(uint256ToNumber(keccak256(n), 'BE'), SECP256K1_N),
|
|
2463
2579
|
o = modInverse(r, SECP256K1_N),
|
|
2464
2580
|
c = mod(i * o, SECP256K1_N),
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
f = doubleAndAdd(
|
|
2468
|
-
l = ellipticAdd(
|
|
2469
|
-
return
|
|
2581
|
+
s = mod(e * o, SECP256K1_N),
|
|
2582
|
+
u = doubleAndAdd(SECP256K1_X, SECP256K1_Y, c),
|
|
2583
|
+
f = doubleAndAdd(t[0], t[1], s),
|
|
2584
|
+
l = ellipticAdd(u[0], u[1], f[0], f[1], SECP256K1_P)
|
|
2585
|
+
return e === mod(l[0], SECP256K1_N)
|
|
2470
2586
|
}
|
|
2471
2587
|
class Uint8ArrayReader {
|
|
2472
|
-
constructor(
|
|
2473
|
-
;(this.cursor = 0), (this.buffer =
|
|
2588
|
+
constructor(t) {
|
|
2589
|
+
;(this.cursor = 0), (this.buffer = t)
|
|
2474
2590
|
}
|
|
2475
|
-
read(
|
|
2476
|
-
const
|
|
2477
|
-
return (this.cursor +=
|
|
2591
|
+
read(t) {
|
|
2592
|
+
const e = this.buffer.subarray(this.cursor, this.cursor + t)
|
|
2593
|
+
return (this.cursor += t), e
|
|
2478
2594
|
}
|
|
2479
2595
|
max() {
|
|
2480
2596
|
return this.buffer.length - this.cursor
|
|
@@ -2482,12 +2598,12 @@ class Uint8ArrayReader {
|
|
|
2482
2598
|
}
|
|
2483
2599
|
exports.Uint8ArrayReader = Uint8ArrayReader
|
|
2484
2600
|
class Uint8ArrayWriter {
|
|
2485
|
-
constructor(
|
|
2486
|
-
;(this.buffer =
|
|
2601
|
+
constructor(t) {
|
|
2602
|
+
;(this.buffer = t), (this.cursor = 0)
|
|
2487
2603
|
}
|
|
2488
|
-
write(
|
|
2489
|
-
const
|
|
2490
|
-
return this.buffer.set(
|
|
2604
|
+
write(t) {
|
|
2605
|
+
const e = Math.min(this.max(), t.max())
|
|
2606
|
+
return this.buffer.set(t.read(e), this.cursor), (this.cursor += e), e
|
|
2491
2607
|
}
|
|
2492
2608
|
max() {
|
|
2493
2609
|
return this.buffer.length - this.cursor
|
|
@@ -2495,91 +2611,104 @@ class Uint8ArrayWriter {
|
|
|
2495
2611
|
}
|
|
2496
2612
|
exports.Uint8ArrayWriter = Uint8ArrayWriter
|
|
2497
2613
|
class Chunk {
|
|
2498
|
-
constructor(
|
|
2499
|
-
;(this.span =
|
|
2614
|
+
constructor(t = 0n) {
|
|
2615
|
+
;(this.span = t), (this.writer = new Uint8ArrayWriter(new Uint8Array(4096)))
|
|
2500
2616
|
}
|
|
2501
2617
|
build() {
|
|
2502
2618
|
return concatBytes(numberToUint64(this.span, 'LE'), this.writer.buffer)
|
|
2503
2619
|
}
|
|
2504
2620
|
hash() {
|
|
2505
|
-
const
|
|
2506
|
-
return
|
|
2621
|
+
const t = new Uint8Array(40)
|
|
2622
|
+
return t.set(numberToUint64(this.span, 'LE')), t.set(bmtRoot(this.writer.buffer), 8), Chunk.hashFunction(t)
|
|
2507
2623
|
}
|
|
2508
|
-
encryptedHash(
|
|
2509
|
-
|
|
2510
|
-
const
|
|
2624
|
+
encryptedHash(t) {
|
|
2625
|
+
t || ((t = new Uint8Array(32)), crypto.getRandomValues(t))
|
|
2626
|
+
const e = encryptSpan(t, numberToUint64(this.span, 'LE')),
|
|
2511
2627
|
r = new Uint8Array(40)
|
|
2512
|
-
return r.set(
|
|
2628
|
+
return r.set(e), r.set(bmtRoot(encryptData(t, this.writer.buffer)), 8), { address: Chunk.hashFunction(r), key: t }
|
|
2513
2629
|
}
|
|
2514
|
-
static encryptSpan(
|
|
2515
|
-
return encryptSpan(
|
|
2630
|
+
static encryptSpan(t, e) {
|
|
2631
|
+
return encryptSpan(t, e)
|
|
2516
2632
|
}
|
|
2517
|
-
static encryptData(
|
|
2518
|
-
return encryptData(
|
|
2633
|
+
static encryptData(t, e) {
|
|
2634
|
+
return encryptData(t, e)
|
|
2519
2635
|
}
|
|
2520
|
-
static decrypt(
|
|
2521
|
-
return decryptChunk(
|
|
2636
|
+
static decrypt(t, e) {
|
|
2637
|
+
return decryptChunk(t, e)
|
|
2522
2638
|
}
|
|
2523
2639
|
}
|
|
2524
2640
|
;(exports.Chunk = Chunk), (Chunk.hashFunction = keccak256)
|
|
2525
2641
|
class ChunkSplitter {
|
|
2526
|
-
constructor(e,
|
|
2527
|
-
;(this.counters = [1]), (this.encrypted =
|
|
2642
|
+
constructor(t, e, r = !1) {
|
|
2643
|
+
;(this.counters = [1]), (this.pending = [[]]), (this.encrypted = r), (this.refSize = r ? 64 : 32), (this.maxShards = e ?? 4096 / this.refSize), (this.chunks = [new Chunk()]), (this.onBatch = t)
|
|
2528
2644
|
}
|
|
2529
|
-
static async root(
|
|
2530
|
-
const
|
|
2531
|
-
return await
|
|
2645
|
+
static async root(t) {
|
|
2646
|
+
const e = new _a(_a.NOOP)
|
|
2647
|
+
return await e.append(t), e.finalize()
|
|
2532
2648
|
}
|
|
2533
|
-
static async encryptedRoot(
|
|
2534
|
-
const
|
|
2535
|
-
return await
|
|
2649
|
+
static async encryptedRoot(t) {
|
|
2650
|
+
const e = new _a(_a.NOOP, void 0, !0)
|
|
2651
|
+
return await e.append(t), (await e.finalize()).encryptedHash()
|
|
2536
2652
|
}
|
|
2537
|
-
async append(
|
|
2538
|
-
const i = new Uint8ArrayReader(
|
|
2653
|
+
async append(t, e = 0, r = 0n) {
|
|
2654
|
+
const i = new Uint8ArrayReader(t)
|
|
2539
2655
|
for (; i.max() > 0; ) {
|
|
2540
|
-
this.chunks[
|
|
2541
|
-
const o = this.chunks[
|
|
2542
|
-
r ? (this.chunks[
|
|
2656
|
+
this.chunks[e].writer.max() === 0 && (await this.elevate(e))
|
|
2657
|
+
const o = this.chunks[e].writer.write(i)
|
|
2658
|
+
r ? (this.chunks[e].span += r) : (this.chunks[0].span += BigInt(o))
|
|
2543
2659
|
}
|
|
2544
2660
|
}
|
|
2545
|
-
async elevate(
|
|
2546
|
-
if (((this.counters[
|
|
2547
|
-
const { address:
|
|
2661
|
+
async elevate(t) {
|
|
2662
|
+
if (((this.counters[t] = ++this.counters[t] % (4096 / this.refSize)), this.pending[t] || (this.pending[t] = []), this.encrypted)) {
|
|
2663
|
+
const { address: e, key: r } = this.chunks[t].encryptedHash(),
|
|
2548
2664
|
i = new Uint8Array(64)
|
|
2549
|
-
i.set(
|
|
2550
|
-
} else
|
|
2551
|
-
this.chunks[
|
|
2665
|
+
i.set(e), i.set(r, 32), this.pending[t].push({ entry: { chunk: this.chunks[t], key: r }, ref: i, span: this.chunks[t].span })
|
|
2666
|
+
} else this.pending[t].push({ entry: { chunk: this.chunks[t] }, ref: this.chunks[t].hash(), span: this.chunks[t].span })
|
|
2667
|
+
;(this.chunks[t] = new Chunk()), this.pending[t].length >= this.maxShards && (await this.flushBatch(t))
|
|
2668
|
+
}
|
|
2669
|
+
async flushBatch(t) {
|
|
2670
|
+
this.chunks[t + 1] || (this.chunks.push(new Chunk()), this.counters.push(1), this.pending.push([]))
|
|
2671
|
+
const e = this.pending[t]
|
|
2672
|
+
this.pending[t] = []
|
|
2673
|
+
const r = await this.onBatch(e.map(i => i.entry))
|
|
2674
|
+
for (const { ref: i, span: o } of e) await this.append(i, t + 1, o)
|
|
2675
|
+
for (const { chunk: i, key: o } of r)
|
|
2676
|
+
if (this.encrypted) {
|
|
2677
|
+
const { address: c, key: s } = i.encryptedHash(o),
|
|
2678
|
+
u = new Uint8Array(64)
|
|
2679
|
+
u.set(c), u.set(s, 32), await this.append(u, t + 1, i.span)
|
|
2680
|
+
} else await this.append(i.hash(), t + 1, i.span)
|
|
2552
2681
|
}
|
|
2553
|
-
async finalize(
|
|
2554
|
-
if (!this.chunks[
|
|
2682
|
+
async finalize(t = 0) {
|
|
2683
|
+
if ((this.pending[t]?.length && (await this.flushBatch(t)), !this.chunks[t + 1])) {
|
|
2555
2684
|
if (this.encrypted) {
|
|
2556
|
-
const { key:
|
|
2557
|
-
await this.
|
|
2558
|
-
} else await this.
|
|
2559
|
-
return this.chunks[
|
|
2685
|
+
const { key: e } = this.chunks[t].encryptedHash()
|
|
2686
|
+
await this.onBatch([{ chunk: this.chunks[t], key: e }])
|
|
2687
|
+
} else await this.onBatch([{ chunk: this.chunks[t] }])
|
|
2688
|
+
return this.chunks[t]
|
|
2560
2689
|
}
|
|
2561
|
-
return this.counters[
|
|
2690
|
+
return this.counters[t] === 1 ? (await this.elevate(t + 1), await this.flushBatch(t + 1), (this.chunks[t + 1] = this.chunks[t]), this.finalize(t + 1)) : (await this.elevate(t), await this.flushBatch(t), this.finalize(t + 1))
|
|
2562
2691
|
}
|
|
2563
2692
|
}
|
|
2564
|
-
;(exports.ChunkSplitter = ChunkSplitter), (_a = ChunkSplitter), (ChunkSplitter.NOOP = async
|
|
2693
|
+
;(exports.ChunkSplitter = ChunkSplitter), (_a = ChunkSplitter), (ChunkSplitter.NOOP = async n => [])
|
|
2565
2694
|
function isAllZero(n) {
|
|
2566
|
-
for (let
|
|
2695
|
+
for (let t = 0; t < n.length; t++) if (n[t] !== 0) return !1
|
|
2567
2696
|
return !0
|
|
2568
2697
|
}
|
|
2569
2698
|
class ChunkJoiner {
|
|
2570
|
-
constructor(
|
|
2571
|
-
;(this.fetch =
|
|
2699
|
+
constructor(t, e, r = !1) {
|
|
2700
|
+
;(this.fetch = t), (this.onData = e), (this.encrypted = r), (this.refSize = r ? 64 : 32)
|
|
2572
2701
|
}
|
|
2573
|
-
static async collect(
|
|
2702
|
+
static async collect(t, e) {
|
|
2574
2703
|
const r = []
|
|
2575
2704
|
return (
|
|
2576
|
-
await new ChunkJoiner(
|
|
2705
|
+
await new ChunkJoiner(e, async i => {
|
|
2577
2706
|
r.push(i)
|
|
2578
|
-
}).join(
|
|
2707
|
+
}).join(t),
|
|
2579
2708
|
concatBytes(...r)
|
|
2580
2709
|
)
|
|
2581
2710
|
}
|
|
2582
|
-
static async collectEncrypted(
|
|
2711
|
+
static async collectEncrypted(t, e, r) {
|
|
2583
2712
|
const i = []
|
|
2584
2713
|
return (
|
|
2585
2714
|
await new ChunkJoiner(
|
|
@@ -2588,77 +2717,77 @@ class ChunkJoiner {
|
|
|
2588
2717
|
i.push(o)
|
|
2589
2718
|
},
|
|
2590
2719
|
!0
|
|
2591
|
-
).join(
|
|
2720
|
+
).join(t, e),
|
|
2592
2721
|
concatBytes(...i)
|
|
2593
2722
|
)
|
|
2594
2723
|
}
|
|
2595
|
-
async join(
|
|
2596
|
-
const r = await this.fetch(
|
|
2724
|
+
async join(t, e) {
|
|
2725
|
+
const r = await this.fetch(t)
|
|
2597
2726
|
let i, o
|
|
2598
|
-
if ((this.encrypted &&
|
|
2727
|
+
if ((this.encrypted && e ? ({ span: i, data: o } = decryptChunk(r, e)) : ((i = uint64ToNumber(r.subarray(0, 8), 'LE')), (o = r.subarray(8, 4104))), i <= 4096n)) await this.onData(o.subarray(0, Number(i)))
|
|
2599
2728
|
else
|
|
2600
2729
|
for (let c = 0; c < 4096 / this.refSize; c++) {
|
|
2601
|
-
const
|
|
2602
|
-
|
|
2603
|
-
if (isAllZero(
|
|
2604
|
-
await this.join(
|
|
2730
|
+
const s = o.subarray(c * this.refSize, (c + 1) * this.refSize),
|
|
2731
|
+
u = s.subarray(0, 32)
|
|
2732
|
+
if (isAllZero(u)) break
|
|
2733
|
+
await this.join(u, this.encrypted ? s.subarray(32, 64) : void 0)
|
|
2605
2734
|
}
|
|
2606
2735
|
}
|
|
2607
2736
|
}
|
|
2608
2737
|
exports.ChunkJoiner = ChunkJoiner
|
|
2609
2738
|
class FixedPointNumber {
|
|
2610
|
-
constructor(
|
|
2611
|
-
if (
|
|
2612
|
-
;(this.value = BigInt(
|
|
2613
|
-
}
|
|
2614
|
-
static cast(
|
|
2615
|
-
if (!
|
|
2616
|
-
if (
|
|
2617
|
-
const
|
|
2618
|
-
r = asNumber(
|
|
2619
|
-
return new FixedPointNumber(
|
|
2620
|
-
}
|
|
2621
|
-
static fromDecimalString(
|
|
2622
|
-
;/e\-\d+$/i.test(
|
|
2623
|
-
let [r, i] =
|
|
2624
|
-
return (i = (i || '').padEnd(
|
|
2625
|
-
}
|
|
2626
|
-
static fromFloat(
|
|
2627
|
-
return FixedPointNumber.fromDecimalString(
|
|
2628
|
-
}
|
|
2629
|
-
add(
|
|
2630
|
-
return this.assertSameScale(
|
|
2631
|
-
}
|
|
2632
|
-
subtract(
|
|
2633
|
-
return this.assertSameScale(
|
|
2634
|
-
}
|
|
2635
|
-
multiply(
|
|
2636
|
-
return new FixedPointNumber(this.value *
|
|
2637
|
-
}
|
|
2638
|
-
divmod(
|
|
2639
|
-
if (
|
|
2640
|
-
const
|
|
2641
|
-
r = this.value %
|
|
2642
|
-
return [new FixedPointNumber(
|
|
2643
|
-
}
|
|
2644
|
-
exchange(
|
|
2645
|
-
if (
|
|
2646
|
-
const o = (this.value *
|
|
2739
|
+
constructor(t, e) {
|
|
2740
|
+
if (e < 0) throw Error('Scale must be non-negative')
|
|
2741
|
+
;(this.value = BigInt(t)), (this.scale = e)
|
|
2742
|
+
}
|
|
2743
|
+
static cast(t) {
|
|
2744
|
+
if (!t) throw Error('Cannot cast falsy value to FixedPointNumber')
|
|
2745
|
+
if (t instanceof FixedPointNumber) return t
|
|
2746
|
+
const e = asBigint(t.value),
|
|
2747
|
+
r = asNumber(t.scale)
|
|
2748
|
+
return new FixedPointNumber(e, r)
|
|
2749
|
+
}
|
|
2750
|
+
static fromDecimalString(t, e) {
|
|
2751
|
+
;/e\-\d+$/i.test(t) && (t = parseFloat(t).toFixed(e))
|
|
2752
|
+
let [r, i] = t.split('.')
|
|
2753
|
+
return (i = (i || '').padEnd(e, '0').slice(0, e)), new FixedPointNumber(BigInt(r + i), e)
|
|
2754
|
+
}
|
|
2755
|
+
static fromFloat(t, e) {
|
|
2756
|
+
return FixedPointNumber.fromDecimalString(t.toString(), e)
|
|
2757
|
+
}
|
|
2758
|
+
add(t) {
|
|
2759
|
+
return this.assertSameScale(t), new FixedPointNumber(this.value + t.value, this.scale)
|
|
2760
|
+
}
|
|
2761
|
+
subtract(t) {
|
|
2762
|
+
return this.assertSameScale(t), new FixedPointNumber(this.value - t.value, this.scale)
|
|
2763
|
+
}
|
|
2764
|
+
multiply(t) {
|
|
2765
|
+
return new FixedPointNumber(this.value * t, this.scale)
|
|
2766
|
+
}
|
|
2767
|
+
divmod(t) {
|
|
2768
|
+
if (t === 0n) throw new Error('Division by zero is not allowed')
|
|
2769
|
+
const e = this.value / t,
|
|
2770
|
+
r = this.value % t
|
|
2771
|
+
return [new FixedPointNumber(e, this.scale), new FixedPointNumber(r, this.scale)]
|
|
2772
|
+
}
|
|
2773
|
+
exchange(t, e, r) {
|
|
2774
|
+
if (t === '*') {
|
|
2775
|
+
const o = (this.value * e.value) / 10n ** BigInt(this.scale)
|
|
2647
2776
|
return new FixedPointNumber(o, r)
|
|
2648
2777
|
}
|
|
2649
|
-
this.assertSameScale(
|
|
2650
|
-
const i = (this.value * 10n ** BigInt(r)) /
|
|
2778
|
+
this.assertSameScale(e)
|
|
2779
|
+
const i = (this.value * 10n ** BigInt(r)) / e.value
|
|
2651
2780
|
return new FixedPointNumber(i, r)
|
|
2652
2781
|
}
|
|
2653
|
-
compare(
|
|
2654
|
-
return this.assertSameScale(
|
|
2782
|
+
compare(t) {
|
|
2783
|
+
return this.assertSameScale(t), this.value > t.value ? 1 : this.value < t.value ? -1 : 0
|
|
2655
2784
|
}
|
|
2656
2785
|
toDecimalString() {
|
|
2657
2786
|
if (this.scale === 0) return this.value.toString()
|
|
2658
|
-
const
|
|
2659
|
-
|
|
2660
|
-
r =
|
|
2661
|
-
return `${
|
|
2787
|
+
const t = this.value.toString(),
|
|
2788
|
+
e = t.slice(0, -this.scale) || '0',
|
|
2789
|
+
r = t.slice(-this.scale).padStart(this.scale, '0')
|
|
2790
|
+
return `${e}.${r}`
|
|
2662
2791
|
}
|
|
2663
2792
|
toString() {
|
|
2664
2793
|
return this.value.toString()
|
|
@@ -2669,173 +2798,173 @@ class FixedPointNumber {
|
|
|
2669
2798
|
toFloat() {
|
|
2670
2799
|
return parseFloat(this.toDecimalString())
|
|
2671
2800
|
}
|
|
2672
|
-
assertSameScale(
|
|
2673
|
-
if (this.scale !==
|
|
2801
|
+
assertSameScale(t) {
|
|
2802
|
+
if (this.scale !== t.scale) throw new Error(`Scale mismatch: expected ${this.scale}, but got ${t.scale}`)
|
|
2674
2803
|
}
|
|
2675
2804
|
}
|
|
2676
2805
|
exports.FixedPointNumber = FixedPointNumber
|
|
2677
2806
|
function tickPlaybook(n) {
|
|
2678
2807
|
if (n.length === 0) return null
|
|
2679
|
-
const
|
|
2680
|
-
return
|
|
2808
|
+
const t = n[0]
|
|
2809
|
+
return t.ttlMax ? --t.ttl <= 0 && n.shift() : (t.ttlMax = t.ttl), { progress: (t.ttlMax - t.ttl) / t.ttlMax, data: t.data }
|
|
2681
2810
|
}
|
|
2682
|
-
function getArgument(n,
|
|
2683
|
-
const i = n.findIndex(
|
|
2811
|
+
function getArgument(n, t, e, r) {
|
|
2812
|
+
const i = n.findIndex(s => s === `--${t}` || s.startsWith(`--${t}=`)),
|
|
2684
2813
|
o = n[i]
|
|
2685
|
-
if (!o) return (
|
|
2814
|
+
if (!o) return (e || {})[r || t || ''] || null
|
|
2686
2815
|
if (o.includes('=')) return o.split('=')[1]
|
|
2687
2816
|
const c = n[i + 1]
|
|
2688
|
-
return c && !c.startsWith('-') ? c : (
|
|
2817
|
+
return c && !c.startsWith('-') ? c : (e || {})[r || t || ''] || null
|
|
2689
2818
|
}
|
|
2690
|
-
function getNumberArgument(n,
|
|
2691
|
-
const i = getArgument(n,
|
|
2819
|
+
function getNumberArgument(n, t, e, r) {
|
|
2820
|
+
const i = getArgument(n, t, e, r)
|
|
2692
2821
|
if (!i) return null
|
|
2693
2822
|
try {
|
|
2694
2823
|
return makeNumber(i)
|
|
2695
2824
|
} catch {
|
|
2696
|
-
throw new Error(`Invalid number argument ${
|
|
2825
|
+
throw new Error(`Invalid number argument ${t}: ${i}`)
|
|
2697
2826
|
}
|
|
2698
2827
|
}
|
|
2699
|
-
function getBooleanArgument(n,
|
|
2700
|
-
const i = n.some(
|
|
2701
|
-
o = getArgument(n,
|
|
2828
|
+
function getBooleanArgument(n, t, e, r) {
|
|
2829
|
+
const i = n.some(u => u.endsWith('-' + t)),
|
|
2830
|
+
o = getArgument(n, t, e, r)
|
|
2702
2831
|
if (!o && i) return !0
|
|
2703
2832
|
if (!o && !i) return null
|
|
2704
2833
|
const c = ['true', '1', 'yes', 'y', 'on'],
|
|
2705
|
-
|
|
2834
|
+
s = ['false', '0', 'no', 'n', 'off']
|
|
2706
2835
|
if (c.includes(o.toLowerCase())) return !0
|
|
2707
|
-
if (
|
|
2708
|
-
throw Error(`Invalid boolean argument ${
|
|
2836
|
+
if (s.includes(o.toLowerCase())) return !1
|
|
2837
|
+
throw Error(`Invalid boolean argument ${t}: ${o}`)
|
|
2709
2838
|
}
|
|
2710
|
-
function requireStringArgument(n,
|
|
2711
|
-
const i = getArgument(n,
|
|
2712
|
-
if (!i) throw new Error(`Missing argument ${
|
|
2839
|
+
function requireStringArgument(n, t, e, r) {
|
|
2840
|
+
const i = getArgument(n, t, e, r)
|
|
2841
|
+
if (!i) throw new Error(`Missing argument ${t}`)
|
|
2713
2842
|
return i
|
|
2714
2843
|
}
|
|
2715
|
-
function requireNumberArgument(n,
|
|
2716
|
-
const i = requireStringArgument(n,
|
|
2844
|
+
function requireNumberArgument(n, t, e, r) {
|
|
2845
|
+
const i = requireStringArgument(n, t, e, r)
|
|
2717
2846
|
try {
|
|
2718
2847
|
return makeNumber(i)
|
|
2719
2848
|
} catch {
|
|
2720
|
-
throw new Error(`Invalid argument ${
|
|
2849
|
+
throw new Error(`Invalid argument ${t}: ${i}`)
|
|
2721
2850
|
}
|
|
2722
2851
|
}
|
|
2723
|
-
function bringToFrontInPlace(n,
|
|
2724
|
-
const
|
|
2725
|
-
n.splice(
|
|
2852
|
+
function bringToFrontInPlace(n, t) {
|
|
2853
|
+
const e = n[t]
|
|
2854
|
+
n.splice(t, 1), n.unshift(e)
|
|
2726
2855
|
}
|
|
2727
|
-
function bringToFront(n,
|
|
2728
|
-
const
|
|
2729
|
-
return bringToFrontInPlace(
|
|
2856
|
+
function bringToFront(n, t) {
|
|
2857
|
+
const e = [...n]
|
|
2858
|
+
return bringToFrontInPlace(e, t), e
|
|
2730
2859
|
}
|
|
2731
|
-
function addPoint(n,
|
|
2732
|
-
return { x: n.x +
|
|
2860
|
+
function addPoint(n, t) {
|
|
2861
|
+
return { x: n.x + t.x, y: n.y + t.y }
|
|
2733
2862
|
}
|
|
2734
|
-
function subtractPoint(n,
|
|
2735
|
-
return { x: n.x -
|
|
2863
|
+
function subtractPoint(n, t) {
|
|
2864
|
+
return { x: n.x - t.x, y: n.y - t.y }
|
|
2736
2865
|
}
|
|
2737
|
-
function multiplyPoint(n,
|
|
2738
|
-
return { x: n.x *
|
|
2866
|
+
function multiplyPoint(n, t) {
|
|
2867
|
+
return { x: n.x * t, y: n.y * t }
|
|
2739
2868
|
}
|
|
2740
2869
|
function normalizePoint(n) {
|
|
2741
|
-
const
|
|
2742
|
-
return { x: n.x /
|
|
2870
|
+
const t = Math.sqrt(n.x * n.x + n.y * n.y)
|
|
2871
|
+
return { x: n.x / t, y: n.y / t }
|
|
2743
2872
|
}
|
|
2744
|
-
function pushPoint(n,
|
|
2745
|
-
return { x: n.x + Math.cos(
|
|
2873
|
+
function pushPoint(n, t, e) {
|
|
2874
|
+
return { x: n.x + Math.cos(t) * e, y: n.y + Math.sin(t) * e }
|
|
2746
2875
|
}
|
|
2747
|
-
function getDistanceBetweenPoints(n,
|
|
2748
|
-
return Math.sqrt((n.x -
|
|
2876
|
+
function getDistanceBetweenPoints(n, t) {
|
|
2877
|
+
return Math.sqrt((n.x - t.x) ** 2 + (n.y - t.y) ** 2)
|
|
2749
2878
|
}
|
|
2750
|
-
function filterCoordinates(n,
|
|
2879
|
+
function filterCoordinates(n, t, e = 'row-first') {
|
|
2751
2880
|
const r = []
|
|
2752
|
-
if (
|
|
2753
|
-
else for (let i = 0; i < n[0].length; i++) for (let o = 0; o < n.length; o++)
|
|
2881
|
+
if (e === 'column-first') for (let i = 0; i < n.length; i++) for (let o = 0; o < n[0].length; o++) t(i, o) && r.push({ x: i, y: o })
|
|
2882
|
+
else for (let i = 0; i < n[0].length; i++) for (let o = 0; o < n.length; o++) t(o, i) && r.push({ x: o, y: i })
|
|
2754
2883
|
return r
|
|
2755
2884
|
}
|
|
2756
|
-
function isHorizontalLine(n,
|
|
2757
|
-
return n[
|
|
2885
|
+
function isHorizontalLine(n, t, e) {
|
|
2886
|
+
return n[t + 1]?.[e] && n[t - 1]?.[e] && !n[t][e - 1] && !n[t][e + 1]
|
|
2758
2887
|
}
|
|
2759
|
-
function isVerticalLine(n,
|
|
2760
|
-
return n[
|
|
2888
|
+
function isVerticalLine(n, t, e) {
|
|
2889
|
+
return n[t][e + 1] && n[t][e - 1] && !n[t - 1]?.[e] && !n[t + 1]?.[e]
|
|
2761
2890
|
}
|
|
2762
|
-
function isLeftmost(n,
|
|
2763
|
-
return !n[
|
|
2891
|
+
function isLeftmost(n, t, e) {
|
|
2892
|
+
return !n[t - 1]?.[e]
|
|
2764
2893
|
}
|
|
2765
|
-
function isRightmost(n,
|
|
2766
|
-
return !n[
|
|
2894
|
+
function isRightmost(n, t, e) {
|
|
2895
|
+
return !n[t + 1]?.[e]
|
|
2767
2896
|
}
|
|
2768
|
-
function isTopmost(n,
|
|
2769
|
-
return !n[
|
|
2897
|
+
function isTopmost(n, t, e) {
|
|
2898
|
+
return !n[t][e - 1]
|
|
2770
2899
|
}
|
|
2771
|
-
function isBottommost(n,
|
|
2772
|
-
return !n[
|
|
2900
|
+
function isBottommost(n, t, e) {
|
|
2901
|
+
return !n[t][e + 1]
|
|
2773
2902
|
}
|
|
2774
|
-
function getCorners(n,
|
|
2903
|
+
function getCorners(n, t, e) {
|
|
2775
2904
|
const r = []
|
|
2776
|
-
return n[
|
|
2905
|
+
return n[t][e] ? (isHorizontalLine(n, t, e) || isVerticalLine(n, t, e) ? [] : (!n[t - 1]?.[e - 1] && isLeftmost(n, t, e) && isTopmost(n, t, e) && r.push({ x: t, y: e }), !n[t + 1]?.[e - 1] && isRightmost(n, t, e) && isTopmost(n, t, e) && r.push({ x: t + 1, y: e }), !n[t - 1]?.[e + 1] && isLeftmost(n, t, e) && isBottommost(n, t, e) && r.push({ x: t, y: e + 1 }), !n[t + 1]?.[e + 1] && isRightmost(n, t, e) && isBottommost(n, t, e) && r.push({ x: t + 1, y: e + 1 }), r)) : (n[t - 1]?.[e] && n[t][e - 1] && r.push({ x: t, y: e }), n[t + 1]?.[e] && n[t][e - 1] && r.push({ x: t + 1, y: e }), n[t - 1]?.[e] && n[t][e + 1] && r.push({ x: t, y: e + 1 }), n[t + 1]?.[e] && n[t][e + 1] && r.push({ x: t + 1, y: e + 1 }), r)
|
|
2777
2906
|
}
|
|
2778
|
-
function findCorners(n,
|
|
2907
|
+
function findCorners(n, t, e, r) {
|
|
2779
2908
|
const i = [
|
|
2780
2909
|
{ x: 0, y: 0 },
|
|
2781
|
-
{ x:
|
|
2910
|
+
{ x: e, y: 0 },
|
|
2782
2911
|
{ x: 0, y: r },
|
|
2783
|
-
{ x:
|
|
2912
|
+
{ x: e, y: r }
|
|
2784
2913
|
]
|
|
2785
2914
|
for (let o = 0; o < n.length; o++)
|
|
2786
2915
|
for (let c = 0; c < n[0].length; c++) {
|
|
2787
|
-
const
|
|
2788
|
-
for (const
|
|
2916
|
+
const s = getCorners(n, o, c)
|
|
2917
|
+
for (const u of s) i.some(f => f.x === u.x && f.y === u.y) || i.push(u)
|
|
2789
2918
|
}
|
|
2790
|
-
return i.map(o => ({ x: o.x *
|
|
2791
|
-
}
|
|
2792
|
-
function findLines(n,
|
|
2793
|
-
const
|
|
2794
|
-
r = filterCoordinates(n, (
|
|
2795
|
-
i = filterCoordinates(n, (
|
|
2796
|
-
o = filterCoordinates(n, (
|
|
2797
|
-
|
|
2798
|
-
const c = group([...i, ...o], (
|
|
2799
|
-
|
|
2800
|
-
return [...c, ...
|
|
2801
|
-
}
|
|
2802
|
-
function getAngleInRadians(n,
|
|
2803
|
-
return Math.atan2(
|
|
2804
|
-
}
|
|
2805
|
-
function getSortedRayAngles(n,
|
|
2806
|
-
return
|
|
2807
|
-
}
|
|
2808
|
-
function getLineIntersectionPoint(n,
|
|
2809
|
-
const i = (r.y -
|
|
2919
|
+
return i.map(o => ({ x: o.x * t, y: o.y * t }))
|
|
2920
|
+
}
|
|
2921
|
+
function findLines(n, t) {
|
|
2922
|
+
const e = filterCoordinates(n, (u, f) => n[u][f] === 0 && n[u][f + 1] !== 0, 'row-first').map(u => ({ ...u, dx: 1, dy: 0 })),
|
|
2923
|
+
r = filterCoordinates(n, (u, f) => n[u][f] === 0 && n[u][f - 1] !== 0, 'row-first').map(u => ({ ...u, dx: 1, dy: 0 })),
|
|
2924
|
+
i = filterCoordinates(n, (u, f) => n[u][f] === 0 && n[u - 1]?.[f] !== 0, 'column-first').map(u => ({ ...u, dx: 0, dy: 1 })),
|
|
2925
|
+
o = filterCoordinates(n, (u, f) => n[u][f] === 0 && n[u + 1]?.[f] !== 0, 'column-first').map(u => ({ ...u, dx: 0, dy: 1 }))
|
|
2926
|
+
e.forEach(u => u.y++), o.forEach(u => u.x++)
|
|
2927
|
+
const c = group([...i, ...o], (u, f) => u.x === f.x && u.y - 1 === f.y),
|
|
2928
|
+
s = group([...r, ...e], (u, f) => u.y === f.y && u.x - 1 === f.x)
|
|
2929
|
+
return [...c, ...s].map(u => ({ start: u[0], end: last(u) })).map(u => ({ start: multiplyPoint(u.start, t), end: multiplyPoint(addPoint(u.end, { x: u.start.dx, y: u.start.dy }), t) }))
|
|
2930
|
+
}
|
|
2931
|
+
function getAngleInRadians(n, t) {
|
|
2932
|
+
return Math.atan2(t.y - n.y, t.x - n.x)
|
|
2933
|
+
}
|
|
2934
|
+
function getSortedRayAngles(n, t) {
|
|
2935
|
+
return t.map(e => getAngleInRadians(n, e)).sort((e, r) => e - r)
|
|
2936
|
+
}
|
|
2937
|
+
function getLineIntersectionPoint(n, t, e, r) {
|
|
2938
|
+
const i = (r.y - e.y) * (t.x - n.x) - (r.x - e.x) * (t.y - n.y)
|
|
2810
2939
|
if (i === 0) return null
|
|
2811
|
-
let o = n.y -
|
|
2812
|
-
c = n.x -
|
|
2813
|
-
const
|
|
2814
|
-
|
|
2815
|
-
return (o =
|
|
2940
|
+
let o = n.y - e.y,
|
|
2941
|
+
c = n.x - e.x
|
|
2942
|
+
const s = (r.x - e.x) * o - (r.y - e.y) * c,
|
|
2943
|
+
u = (t.x - n.x) * o - (t.y - n.y) * c
|
|
2944
|
+
return (o = s / i), (c = u / i), o > 0 && o < 1 && c > 0 && c < 1 ? { x: n.x + o * (t.x - n.x), y: n.y + o * (t.y - n.y) } : null
|
|
2816
2945
|
}
|
|
2817
|
-
function raycast(n,
|
|
2946
|
+
function raycast(n, t, e) {
|
|
2818
2947
|
const r = [],
|
|
2819
|
-
i = pushPoint(n,
|
|
2820
|
-
for (const o of
|
|
2948
|
+
i = pushPoint(n, e, 1e4)
|
|
2949
|
+
for (const o of t) {
|
|
2821
2950
|
const c = getLineIntersectionPoint(n, i, o.start, o.end)
|
|
2822
2951
|
c && r.push(c)
|
|
2823
2952
|
}
|
|
2824
2953
|
return r.length
|
|
2825
2954
|
? r.reduce((o, c) => {
|
|
2826
|
-
const
|
|
2827
|
-
|
|
2828
|
-
return
|
|
2955
|
+
const s = getDistanceBetweenPoints(n, c),
|
|
2956
|
+
u = getDistanceBetweenPoints(n, o)
|
|
2957
|
+
return s < u ? c : o
|
|
2829
2958
|
})
|
|
2830
2959
|
: null
|
|
2831
2960
|
}
|
|
2832
|
-
function raycastCircle(n,
|
|
2833
|
-
const i = getSortedRayAngles(n,
|
|
2961
|
+
function raycastCircle(n, t, e) {
|
|
2962
|
+
const i = getSortedRayAngles(n, e),
|
|
2834
2963
|
o = []
|
|
2835
2964
|
for (const c of i) {
|
|
2836
|
-
const
|
|
2837
|
-
|
|
2838
|
-
|
|
2965
|
+
const s = raycast(n, t, c - 0.001),
|
|
2966
|
+
u = raycast(n, t, c + 0.001)
|
|
2967
|
+
s && o.push(s), u && o.push(u)
|
|
2839
2968
|
}
|
|
2840
2969
|
return o
|
|
2841
2970
|
}
|
|
@@ -2843,16 +2972,16 @@ class PubSubChannel {
|
|
|
2843
2972
|
constructor() {
|
|
2844
2973
|
this.subscribers = []
|
|
2845
2974
|
}
|
|
2846
|
-
subscribe(
|
|
2975
|
+
subscribe(t) {
|
|
2847
2976
|
return (
|
|
2848
|
-
this.subscribers.push(
|
|
2977
|
+
this.subscribers.push(t),
|
|
2849
2978
|
() => {
|
|
2850
|
-
this.subscribers = this.subscribers.filter(
|
|
2979
|
+
this.subscribers = this.subscribers.filter(e => e !== t)
|
|
2851
2980
|
}
|
|
2852
2981
|
)
|
|
2853
2982
|
}
|
|
2854
|
-
publish(
|
|
2855
|
-
this.subscribers.forEach(
|
|
2983
|
+
publish(t) {
|
|
2984
|
+
this.subscribers.forEach(e => e(t))
|
|
2856
2985
|
}
|
|
2857
2986
|
clear() {
|
|
2858
2987
|
this.subscribers = []
|
|
@@ -2863,33 +2992,33 @@ class PubSubChannel {
|
|
|
2863
2992
|
}
|
|
2864
2993
|
exports.PubSubChannel = PubSubChannel
|
|
2865
2994
|
class AsyncQueue {
|
|
2866
|
-
constructor(
|
|
2867
|
-
;(this.queue = []), (this.running = 0), (this.onProcessed = new PubSubChannel()), (this.onDrained = new PubSubChannel()), (this.concurrency =
|
|
2995
|
+
constructor(t, e) {
|
|
2996
|
+
;(this.queue = []), (this.running = 0), (this.onProcessed = new PubSubChannel()), (this.onDrained = new PubSubChannel()), (this.concurrency = t), (this.capacity = e)
|
|
2868
2997
|
}
|
|
2869
2998
|
process() {
|
|
2870
2999
|
if (this.running >= this.concurrency) return
|
|
2871
|
-
const
|
|
2872
|
-
|
|
3000
|
+
const t = this.queue.shift()
|
|
3001
|
+
t &&
|
|
2873
3002
|
(this.running++,
|
|
2874
|
-
|
|
3003
|
+
t().finally(() => {
|
|
2875
3004
|
this.running--, this.process(), this.onProcessed.publish(), this.running === 0 && this.onDrained.publish()
|
|
2876
3005
|
}))
|
|
2877
3006
|
}
|
|
2878
|
-
enqueue(
|
|
2879
|
-
if (this.queue.length < this.capacity) return this.queue.push(
|
|
3007
|
+
enqueue(t) {
|
|
3008
|
+
if (this.queue.length < this.capacity) return this.queue.push(t), this.process(), Promise.resolve()
|
|
2880
3009
|
if (this.onProcessed.getSubscriberCount()) throw Error('Queue capacity is full')
|
|
2881
|
-
return new Promise(
|
|
3010
|
+
return new Promise(e => {
|
|
2882
3011
|
this.onProcessed.subscribe(() => {
|
|
2883
|
-
this.queue.length < this.capacity && (this.queue.push(
|
|
3012
|
+
this.queue.length < this.capacity && (this.queue.push(t), this.process(), this.onProcessed.clear(), e())
|
|
2884
3013
|
})
|
|
2885
3014
|
})
|
|
2886
3015
|
}
|
|
2887
3016
|
drain() {
|
|
2888
3017
|
if (this.running === 0) return Promise.resolve()
|
|
2889
3018
|
if (this.onDrained.getSubscriberCount()) throw Error('Already draining')
|
|
2890
|
-
return new Promise(
|
|
3019
|
+
return new Promise(t => {
|
|
2891
3020
|
this.onDrained.subscribe(() => {
|
|
2892
|
-
this.onDrained.clear(),
|
|
3021
|
+
this.onDrained.clear(), t()
|
|
2893
3022
|
})
|
|
2894
3023
|
})
|
|
2895
3024
|
}
|
|
@@ -2899,35 +3028,35 @@ class TrieRouter {
|
|
|
2899
3028
|
constructor() {
|
|
2900
3029
|
this.forks = new Map()
|
|
2901
3030
|
}
|
|
2902
|
-
insert(
|
|
2903
|
-
if (
|
|
2904
|
-
this.handler =
|
|
3031
|
+
insert(t, e) {
|
|
3032
|
+
if (t.length === 0) {
|
|
3033
|
+
this.handler = e
|
|
2905
3034
|
return
|
|
2906
3035
|
}
|
|
2907
|
-
const r =
|
|
3036
|
+
const r = t[0]
|
|
2908
3037
|
let i = r,
|
|
2909
3038
|
o
|
|
2910
3039
|
if ((r.startsWith(':') && ((i = ':'), (o = r.slice(1))), !this.forks.has(i))) {
|
|
2911
3040
|
const c = new TrieRouter()
|
|
2912
3041
|
o && (c.variableName = o), this.forks.set(i, c)
|
|
2913
3042
|
}
|
|
2914
|
-
this.forks.get(i).insert(
|
|
3043
|
+
this.forks.get(i).insert(t.slice(1), e)
|
|
2915
3044
|
}
|
|
2916
|
-
async handle(
|
|
2917
|
-
if (
|
|
2918
|
-
const o =
|
|
3045
|
+
async handle(t, e, r, i) {
|
|
3046
|
+
if (t.length === 0) return this.handler ? (await this.handler(e, r, i), !0) : !1
|
|
3047
|
+
const o = t[0],
|
|
2919
3048
|
c = this.forks.get(o)
|
|
2920
|
-
if (c) return c.handle(
|
|
2921
|
-
const
|
|
2922
|
-
if (
|
|
2923
|
-
const
|
|
2924
|
-
return
|
|
3049
|
+
if (c) return c.handle(t.slice(1), e, r, i)
|
|
3050
|
+
const s = this.forks.get(':')
|
|
3051
|
+
if (s) return s.variableName && i.set(s.variableName, decodeURIComponent(o)), s.handle(t.slice(1), e, r, i)
|
|
3052
|
+
const u = this.forks.get('*')
|
|
3053
|
+
return u ? (i.set('wildcard', t.join('/')), u.handler ? (await u.handler(e, r, i), !0) : !1) : !1
|
|
2925
3054
|
}
|
|
2926
3055
|
}
|
|
2927
3056
|
exports.TrieRouter = TrieRouter
|
|
2928
3057
|
class RollingValueProvider {
|
|
2929
|
-
constructor(
|
|
2930
|
-
;(this.index = 0), (this.values =
|
|
3058
|
+
constructor(t) {
|
|
3059
|
+
;(this.index = 0), (this.values = t)
|
|
2931
3060
|
}
|
|
2932
3061
|
current() {
|
|
2933
3062
|
return this.values[this.index]
|
|
@@ -2945,51 +3074,51 @@ class Solver {
|
|
|
2945
3074
|
return this.status
|
|
2946
3075
|
}
|
|
2947
3076
|
createInitialState() {
|
|
2948
|
-
return Object.fromEntries(this.steps.map(
|
|
3077
|
+
return Object.fromEntries(this.steps.map(t => [t.name, 'pending']))
|
|
2949
3078
|
}
|
|
2950
|
-
setHooks(
|
|
2951
|
-
|
|
3079
|
+
setHooks(t) {
|
|
3080
|
+
t.onStatusChange && (this.onStatusChange = t.onStatusChange), t.onStepChange && (this.onStepChange = t.onStepChange), t.onFinish && (this.onFinish = t.onFinish), t.onError && (this.onError = t.onError)
|
|
2952
3081
|
}
|
|
2953
|
-
addStep(
|
|
2954
|
-
if (this.steps.find(
|
|
2955
|
-
this.steps.push(
|
|
3082
|
+
addStep(t) {
|
|
3083
|
+
if (this.steps.find(e => e.name === t.name)) throw Error(`Step with name ${t.name} already exists`)
|
|
3084
|
+
this.steps.push(t)
|
|
2956
3085
|
}
|
|
2957
3086
|
async execute() {
|
|
2958
3087
|
if (this.status !== 'pending') throw Error(`Cannot execute solver in status ${this.status}`)
|
|
2959
3088
|
;(this.status = 'in-progress'), await this.onStatusChange(this.status)
|
|
2960
|
-
let
|
|
2961
|
-
for (const
|
|
3089
|
+
let t = this.createInitialState()
|
|
3090
|
+
for (const e of this.steps)
|
|
2962
3091
|
try {
|
|
2963
|
-
if (
|
|
2964
|
-
const i = e
|
|
3092
|
+
if (e.transientSkipStepName) {
|
|
3093
|
+
const i = t[e.transientSkipStepName]
|
|
2965
3094
|
if (i === 'skipped' || i === 'failed') {
|
|
2966
|
-
;(
|
|
3095
|
+
;(t = { ...t, [e.name]: 'skipped' }), await this.onStepChange(t)
|
|
2967
3096
|
continue
|
|
2968
3097
|
}
|
|
2969
3098
|
}
|
|
2970
|
-
if (!(
|
|
2971
|
-
;(
|
|
3099
|
+
if (!(e.precondition ? await e.precondition(this.context) : !0)) {
|
|
3100
|
+
;(t = { ...t, [e.name]: 'skipped' }), await this.onStepChange(t)
|
|
2972
3101
|
continue
|
|
2973
3102
|
}
|
|
2974
|
-
;(
|
|
2975
|
-
for (let i = 0; (await
|
|
2976
|
-
;(
|
|
3103
|
+
;(t = { ...t, [e.name]: 'in-progress' }), await this.onStepChange(t)
|
|
3104
|
+
for (let i = 0; (await e.action(this.context, i)) === 'retry'; i++);
|
|
3105
|
+
;(t = { ...t, [e.name]: 'completed' }), await this.onStepChange(t)
|
|
2977
3106
|
} catch (r) {
|
|
2978
|
-
throw ((
|
|
3107
|
+
throw ((t = { ...t, [e.name]: 'failed' }), (this.status = 'failed'), await this.onStatusChange(this.status), await this.onStepChange(t), await this.onError(r), r)
|
|
2979
3108
|
}
|
|
2980
3109
|
return (this.status = 'completed'), await this.onStatusChange(this.status), await this.onFinish(), this.context
|
|
2981
3110
|
}
|
|
2982
3111
|
}
|
|
2983
3112
|
exports.Solver = Solver
|
|
2984
3113
|
class Lock {
|
|
2985
|
-
constructor(
|
|
2986
|
-
;(this.queryFunction =
|
|
3114
|
+
constructor(t) {
|
|
3115
|
+
;(this.queryFunction = t.queryFunction), (this.lockFunction = t.lockFunction), (this.unlockFunction = t.unlockFunction), (this.timeoutMillis = t.timeoutMillis)
|
|
2987
3116
|
}
|
|
2988
3117
|
async couldLock() {
|
|
2989
|
-
const
|
|
3118
|
+
const t = await this.queryFunction()
|
|
2990
3119
|
try {
|
|
2991
|
-
const
|
|
2992
|
-
if (Date.now() <
|
|
3120
|
+
const e = asInteger(t)
|
|
3121
|
+
if (Date.now() < e + this.timeoutMillis) return new Date(e + this.timeoutMillis)
|
|
2993
3122
|
} catch {}
|
|
2994
3123
|
return await this.lockFunction(Date.now().toString()), !0
|
|
2995
3124
|
}
|