cafe-utility 22.0.0 → 22.1.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 +12 -3
- package/index.js +190 -173
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -75,10 +75,19 @@ declare function randomUnicodeString(length: number, generator?: () => number):
|
|
|
75
75
|
declare function searchHex(string: string, length: number): string | null;
|
|
76
76
|
declare function searchSubstring(string: string, predicate: (string: string) => boolean, separators?: string[]): string | null;
|
|
77
77
|
declare function randomHexString(length: number, generator?: () => number): string;
|
|
78
|
-
declare function asString(string: any
|
|
78
|
+
declare function asString(string: any, limits?: {
|
|
79
|
+
min?: number;
|
|
80
|
+
max?: number;
|
|
81
|
+
}): string;
|
|
79
82
|
declare function asSafeString(string: any): string;
|
|
80
|
-
declare function asNumber(number: any
|
|
81
|
-
|
|
83
|
+
declare function asNumber(number: any, limits?: {
|
|
84
|
+
min?: number;
|
|
85
|
+
max?: number;
|
|
86
|
+
}): number;
|
|
87
|
+
declare function asInteger(number: any, limits?: {
|
|
88
|
+
min?: number;
|
|
89
|
+
max?: number;
|
|
90
|
+
}): number;
|
|
82
91
|
declare function asBoolean(bool: any): boolean;
|
|
83
92
|
declare function asDate(date: any): Date;
|
|
84
93
|
declare function asNullableString(string: any): string | null;
|
package/index.js
CHANGED
|
@@ -25,7 +25,7 @@ async function runInParallelBatches(n, t = 1) {
|
|
|
25
25
|
const e = splitByCount(n, t),
|
|
26
26
|
r = [],
|
|
27
27
|
o = e.map(async i => {
|
|
28
|
-
for (const
|
|
28
|
+
for (const u of i) r.push(await u())
|
|
29
29
|
})
|
|
30
30
|
return await Promise.all(o), r
|
|
31
31
|
}
|
|
@@ -132,16 +132,16 @@ function pickManyUnique(n, t, e, r = Math.random) {
|
|
|
132
132
|
const o = []
|
|
133
133
|
for (; o.length < t; ) {
|
|
134
134
|
const i = pick(n, r)
|
|
135
|
-
o.some(
|
|
135
|
+
o.some(u => e(u, i)) || o.push(i)
|
|
136
136
|
}
|
|
137
137
|
return o
|
|
138
138
|
}
|
|
139
139
|
function pickGuaranteed(n, t, e, r, o, i = Math.random) {
|
|
140
|
-
const
|
|
140
|
+
const u = n.filter(s => s !== t && s !== e),
|
|
141
141
|
c = []
|
|
142
|
-
for (t !== null && c.push(t);
|
|
143
|
-
const
|
|
144
|
-
o(s
|
|
142
|
+
for (t !== null && c.push(t); u.length && c.length < r; ) {
|
|
143
|
+
const s = exports.Random.intBetween(0, u.length - 1, i)
|
|
144
|
+
o(u[s], c) && c.push(u[s]), u.splice(s, 1)
|
|
145
145
|
}
|
|
146
146
|
return shuffle(c, i), { values: c, indexOfGuaranteed: t !== null ? c.indexOf(t) : -1 }
|
|
147
147
|
}
|
|
@@ -157,7 +157,7 @@ function makePipe(n, t) {
|
|
|
157
157
|
}
|
|
158
158
|
function pickWeighted(n, t, e) {
|
|
159
159
|
if ((isUndefined(e) && (e = Math.random()), n.length !== t.length)) throw new Error('Array length mismatch')
|
|
160
|
-
let r = t.reduce((i,
|
|
160
|
+
let r = t.reduce((i, u) => i + u, 0)
|
|
161
161
|
const o = e * r
|
|
162
162
|
for (let i = 0; i < n.length - 1; i++) if (((r -= t[i]), o >= r)) return n[i]
|
|
163
163
|
return last(n)
|
|
@@ -166,7 +166,7 @@ function sortWeighted(n, t, e = Math.random) {
|
|
|
166
166
|
const r = t.map(i => e() * i),
|
|
167
167
|
o = []
|
|
168
168
|
for (let i = 0; i < n.length; i++) o.push([n[i], r[i]])
|
|
169
|
-
return o.sort((i,
|
|
169
|
+
return o.sort((i, u) => u[1] - i[1]).map(i => i[0])
|
|
170
170
|
}
|
|
171
171
|
function getDeep(n, t) {
|
|
172
172
|
if (n == null) return null
|
|
@@ -182,9 +182,9 @@ function setDeep(n, t, e) {
|
|
|
182
182
|
const r = t.split(/\.|\[/)
|
|
183
183
|
let o = n
|
|
184
184
|
for (let i = 0; i < r.length; i++) {
|
|
185
|
-
const
|
|
185
|
+
const u = r[i],
|
|
186
186
|
c = i < r.length - 1 && r[i + 1].includes(']'),
|
|
187
|
-
f =
|
|
187
|
+
f = u.includes(']') ? u.replace(/\[|\]/g, '') : u
|
|
188
188
|
if (i === r.length - 1) return (o[f] = e), e
|
|
189
189
|
isObject(o[f]) || (c ? (o[f] = []) : (o[f] = {})), (o = o[f])
|
|
190
190
|
}
|
|
@@ -252,10 +252,10 @@ function rgbToHex(n) {
|
|
|
252
252
|
}
|
|
253
253
|
function haversineDistanceToMeters(n, t, e, r) {
|
|
254
254
|
const i = (n * Math.PI) / 180,
|
|
255
|
-
|
|
255
|
+
u = (e * Math.PI) / 180,
|
|
256
256
|
c = ((e - n) * Math.PI) / 180,
|
|
257
|
-
|
|
258
|
-
f = Math.sin(c / 2) * Math.sin(c / 2) + Math.cos(i) * Math.cos(
|
|
257
|
+
s = ((r - t) * Math.PI) / 180,
|
|
258
|
+
f = Math.sin(c / 2) * Math.sin(c / 2) + Math.cos(i) * Math.cos(u) * Math.sin(s / 2) * Math.sin(s / 2)
|
|
259
259
|
return 6371e3 * (2 * Math.atan2(Math.sqrt(f), Math.sqrt(1 - f)))
|
|
260
260
|
}
|
|
261
261
|
function roundToNearest(n, t) {
|
|
@@ -382,8 +382,15 @@ function randomHexString(n, t = Math.random) {
|
|
|
382
382
|
for (let r = 0; r < n; r++) e += hexAlphabet[Math.floor(t() * hexAlphabet.length)]
|
|
383
383
|
return e
|
|
384
384
|
}
|
|
385
|
-
function asString(n) {
|
|
385
|
+
function asString(n, t) {
|
|
386
|
+
var e, r
|
|
386
387
|
if (isBlank(n)) throw new TypeError('Expected string, got: ' + n)
|
|
388
|
+
if (t && ((t.min !== void 0 && n.length < t.min) || (t.max !== void 0 && n.length > t.max)))
|
|
389
|
+
throw RangeError(
|
|
390
|
+
`Expected string length in range: ${(e = t.min) !== null && e !== void 0 ? e : '-inf'}..${
|
|
391
|
+
(r = t.max) !== null && r !== void 0 ? r : 'inf'
|
|
392
|
+
}; got: ${n.length}`
|
|
393
|
+
)
|
|
387
394
|
return n
|
|
388
395
|
}
|
|
389
396
|
function asSafeString(n) {
|
|
@@ -395,13 +402,23 @@ function asSafeString(n) {
|
|
|
395
402
|
throw new TypeError('Expected safe string, got: ' + n)
|
|
396
403
|
return n
|
|
397
404
|
}
|
|
398
|
-
function
|
|
399
|
-
|
|
405
|
+
function checkLimits(n, t) {
|
|
406
|
+
var e, r
|
|
407
|
+
if ((t.min !== void 0 && n < t.min) || (t.max !== void 0 && n > t.max))
|
|
408
|
+
throw RangeError(
|
|
409
|
+
`Expected value in range: ${(e = t.min) !== null && e !== void 0 ? e : '-inf'}..${
|
|
410
|
+
(r = t.max) !== null && r !== void 0 ? r : 'inf'
|
|
411
|
+
}; got: ${n}`
|
|
412
|
+
)
|
|
413
|
+
}
|
|
414
|
+
function asNumber(n, t) {
|
|
415
|
+
if (isNumber(n)) return t && checkLimits(n, t), n
|
|
400
416
|
if (!isString(n) || !n.match(/^-?\d+(\.\d+)?$/)) throw new TypeError('Expected number, got: ' + n)
|
|
401
|
-
|
|
417
|
+
const e = parseFloat(n)
|
|
418
|
+
return t && checkLimits(e, t), e
|
|
402
419
|
}
|
|
403
|
-
function asInteger(n) {
|
|
404
|
-
return asNumber(n) | 0
|
|
420
|
+
function asInteger(n, t) {
|
|
421
|
+
return asNumber(n, t) | 0
|
|
405
422
|
}
|
|
406
423
|
function asBoolean(n) {
|
|
407
424
|
if (n === 'true') return !0
|
|
@@ -754,12 +771,12 @@ function expand(n) {
|
|
|
754
771
|
const r = e[1].split(','),
|
|
755
772
|
o = n.slice(0, e.index),
|
|
756
773
|
i = n.slice(e.index + e[0].length)
|
|
757
|
-
let
|
|
774
|
+
let u = []
|
|
758
775
|
for (const c of r) {
|
|
759
|
-
const
|
|
760
|
-
|
|
776
|
+
const s = expand(o + c + i)
|
|
777
|
+
u = u.concat(s)
|
|
761
778
|
}
|
|
762
|
-
return
|
|
779
|
+
return u
|
|
763
780
|
}
|
|
764
781
|
function shrinkTrim(n) {
|
|
765
782
|
return n.replace(/\s+/g, ' ').replace(/\s$|^\s/g, '')
|
|
@@ -983,13 +1000,13 @@ function base64ToUint8Array(n) {
|
|
|
983
1000
|
let o = 0,
|
|
984
1001
|
i = 0
|
|
985
1002
|
for (; o < n.length; ) {
|
|
986
|
-
const
|
|
1003
|
+
const u = BASE64_CHARS.indexOf(n.charAt(o++)),
|
|
987
1004
|
c = BASE64_CHARS.indexOf(n.charAt(o++)),
|
|
988
|
-
|
|
1005
|
+
s = BASE64_CHARS.indexOf(n.charAt(o++)),
|
|
989
1006
|
f = BASE64_CHARS.indexOf(n.charAt(o++)),
|
|
990
|
-
l = (
|
|
991
|
-
a = ((c & 15) << 4) | (
|
|
992
|
-
h = ((
|
|
1007
|
+
l = (u << 2) | (c >> 4),
|
|
1008
|
+
a = ((c & 15) << 4) | (s >> 2),
|
|
1009
|
+
h = ((s & 3) << 6) | f
|
|
993
1010
|
;(r[i++] = l), i < e && (r[i++] = a), i < e && (r[i++] = h)
|
|
994
1011
|
}
|
|
995
1012
|
return r
|
|
@@ -1000,12 +1017,12 @@ function uint8ArrayToBase64(n) {
|
|
|
1000
1017
|
for (let r = 0; r < n.length; r += 3) {
|
|
1001
1018
|
const o = n[r],
|
|
1002
1019
|
i = n[r + 1],
|
|
1003
|
-
|
|
1020
|
+
u = n[r + 2],
|
|
1004
1021
|
c = o >> 2,
|
|
1005
|
-
|
|
1006
|
-
f = ((i & 15) << 2) | (
|
|
1007
|
-
l =
|
|
1008
|
-
;(t += BASE64_CHARS[c] + BASE64_CHARS[
|
|
1022
|
+
s = ((o & 3) << 4) | (i >> 4),
|
|
1023
|
+
f = ((i & 15) << 2) | (u >> 6),
|
|
1024
|
+
l = u & 63
|
|
1025
|
+
;(t += BASE64_CHARS[c] + BASE64_CHARS[s]),
|
|
1009
1026
|
r + 1 < n.length ? (t += BASE64_CHARS[f]) : e++,
|
|
1010
1027
|
r + 2 < n.length ? (t += BASE64_CHARS[l]) : e++
|
|
1011
1028
|
}
|
|
@@ -1032,9 +1049,9 @@ function route(n, t) {
|
|
|
1032
1049
|
if (e.length !== r.length) return null
|
|
1033
1050
|
const o = {}
|
|
1034
1051
|
for (let i = 0; i < e.length; i++) {
|
|
1035
|
-
const
|
|
1036
|
-
if (
|
|
1037
|
-
else if (
|
|
1052
|
+
const u = e[i]
|
|
1053
|
+
if (u.startsWith(':')) o[u.slice(1)] = r[i]
|
|
1054
|
+
else if (u !== r[i]) return null
|
|
1038
1055
|
}
|
|
1039
1056
|
return o
|
|
1040
1057
|
}
|
|
@@ -1045,25 +1062,25 @@ function explodeReplace(n, t, e) {
|
|
|
1045
1062
|
}
|
|
1046
1063
|
function generateVariants(n, t, e, r = Math.random) {
|
|
1047
1064
|
const o = exports.Arrays.shuffle(
|
|
1048
|
-
t.map(
|
|
1065
|
+
t.map(u => ({
|
|
1049
1066
|
variants: exports.Arrays.shuffle(
|
|
1050
|
-
|
|
1067
|
+
u.variants.map(c => c),
|
|
1051
1068
|
r
|
|
1052
1069
|
),
|
|
1053
|
-
avoid:
|
|
1070
|
+
avoid: u.avoid
|
|
1054
1071
|
})),
|
|
1055
1072
|
r
|
|
1056
1073
|
),
|
|
1057
1074
|
i = []
|
|
1058
|
-
for (const
|
|
1059
|
-
const c =
|
|
1060
|
-
|
|
1061
|
-
if (
|
|
1075
|
+
for (const u of o) {
|
|
1076
|
+
const c = u.variants.filter(f => f !== u.avoid),
|
|
1077
|
+
s = c.find(f => n.includes(f))
|
|
1078
|
+
if (s && (pushAll(i, explodeReplace(n, s, c)), i.length >= e)) break
|
|
1062
1079
|
}
|
|
1063
1080
|
if (i.length < e)
|
|
1064
|
-
for (const
|
|
1065
|
-
const c =
|
|
1066
|
-
if (c && (pushAll(i, explodeReplace(n, c,
|
|
1081
|
+
for (const u of o) {
|
|
1082
|
+
const c = u.variants.find(s => n.includes(s))
|
|
1083
|
+
if (c && (pushAll(i, explodeReplace(n, c, u.variants)), i.length >= e)) break
|
|
1067
1084
|
}
|
|
1068
1085
|
return i.slice(0, e)
|
|
1069
1086
|
}
|
|
@@ -1097,10 +1114,10 @@ function toLines(n, t, e = {}) {
|
|
|
1097
1114
|
const r = []
|
|
1098
1115
|
let o = '',
|
|
1099
1116
|
i = 0
|
|
1100
|
-
for (let
|
|
1101
|
-
const c = n[
|
|
1102
|
-
|
|
1103
|
-
if (((o += c), (i +=
|
|
1117
|
+
for (let u = 0; u < n.length; u++) {
|
|
1118
|
+
const c = n[u],
|
|
1119
|
+
s = e[c] || 1
|
|
1120
|
+
if (((o += c), (i += s), i > t)) {
|
|
1104
1121
|
const { line: f, rest: l } = breakLine(o)
|
|
1105
1122
|
r.push(f),
|
|
1106
1123
|
(o = l),
|
|
@@ -1170,8 +1187,8 @@ function resolveRemainingVariablesWithDefaults(n, t = '$', e = ':') {
|
|
|
1170
1187
|
if (n[r + o.length + 1] === e)
|
|
1171
1188
|
if (n[r + o.length + 2] === e) n = n.replace(`${t}${o}${e}${e}`, '')
|
|
1172
1189
|
else {
|
|
1173
|
-
const
|
|
1174
|
-
n = n.replace(`${t}${o}${e}${
|
|
1190
|
+
const u = readNextWord(n, r + o.length + 2)
|
|
1191
|
+
n = n.replace(`${t}${o}${e}${u}`, u)
|
|
1175
1192
|
}
|
|
1176
1193
|
r = n.indexOf(t, r + 1)
|
|
1177
1194
|
}
|
|
@@ -1183,8 +1200,8 @@ function resolveMarkdownLinks(n, t) {
|
|
|
1183
1200
|
const r = lastIndexOfBefore(n, '[', e),
|
|
1184
1201
|
o = n.indexOf(')', e)
|
|
1185
1202
|
if (r !== -1 && o !== -1) {
|
|
1186
|
-
const [i,
|
|
1187
|
-
c = t(i,
|
|
1203
|
+
const [i, u] = n.slice(r + 1, o).split(']('),
|
|
1204
|
+
c = t(i, u)
|
|
1188
1205
|
n = n.slice(0, r) + c + n.slice(o + 1)
|
|
1189
1206
|
}
|
|
1190
1207
|
e = n.indexOf('](', e + 1)
|
|
@@ -1214,17 +1231,17 @@ function selectMax(n, t) {
|
|
|
1214
1231
|
let e = null,
|
|
1215
1232
|
r = -1 / 0
|
|
1216
1233
|
for (const [o, i] of Object.entries(n)) {
|
|
1217
|
-
const
|
|
1218
|
-
|
|
1234
|
+
const u = t(i)
|
|
1235
|
+
u > r && ((r = u), (e = o))
|
|
1219
1236
|
}
|
|
1220
1237
|
return e ? [e, n[e]] : null
|
|
1221
1238
|
}
|
|
1222
1239
|
function reposition(n, t, e, r) {
|
|
1223
|
-
const o = n.find(
|
|
1224
|
-
i = n.find(
|
|
1240
|
+
const o = n.find(u => u[t] === e),
|
|
1241
|
+
i = n.find(u => u[t] === e + r)
|
|
1225
1242
|
o && i ? ((o[t] = e + r), (i[t] = e)) : o && (o[t] = e + r),
|
|
1226
|
-
n.sort((
|
|
1227
|
-
n.forEach((
|
|
1243
|
+
n.sort((u, c) => asNumber(u[t]) - asNumber(c[t])),
|
|
1244
|
+
n.forEach((u, c) => (u[t] = c + 1))
|
|
1228
1245
|
}
|
|
1229
1246
|
function unwrapSingleKey(n) {
|
|
1230
1247
|
const t = Object.keys(n)
|
|
@@ -1238,8 +1255,8 @@ function parseCsv(n, t = ',', e = '"') {
|
|
|
1238
1255
|
const r = []
|
|
1239
1256
|
let o = '',
|
|
1240
1257
|
i = !1
|
|
1241
|
-
const
|
|
1242
|
-
for (const c of
|
|
1258
|
+
const u = n.split('')
|
|
1259
|
+
for (const c of u) c === t && !i ? (r.push(o), (o = '')) : c === e && ((!o && !i) || i) ? (i = !i) : (o += c)
|
|
1243
1260
|
return r.push(o), r
|
|
1244
1261
|
}
|
|
1245
1262
|
function humanizeProgress(n) {
|
|
@@ -1332,18 +1349,18 @@ function getTimeDelta(n, t) {
|
|
|
1332
1349
|
const r = (e = t?.now) !== null && e !== void 0 ? e : Date.now(),
|
|
1333
1350
|
o = t?.labels || DefaultTimeDeltaLabels,
|
|
1334
1351
|
i = exports.Types.isDate(n) ? n.getTime() : n
|
|
1335
|
-
let
|
|
1336
|
-
return
|
|
1352
|
+
let u = (r - i) / 1e3
|
|
1353
|
+
return u < 10
|
|
1337
1354
|
? o.now()
|
|
1338
|
-
:
|
|
1339
|
-
? o.seconds(Math.floor(
|
|
1340
|
-
: ((
|
|
1341
|
-
|
|
1342
|
-
? o.minutes(Math.floor(
|
|
1343
|
-
: ((
|
|
1344
|
-
|
|
1345
|
-
? o.hours(Math.floor(
|
|
1346
|
-
: ((
|
|
1355
|
+
: u < 120
|
|
1356
|
+
? o.seconds(Math.floor(u))
|
|
1357
|
+
: ((u /= 60),
|
|
1358
|
+
u < 120
|
|
1359
|
+
? o.minutes(Math.floor(u))
|
|
1360
|
+
: ((u /= 60),
|
|
1361
|
+
u < 48
|
|
1362
|
+
? o.hours(Math.floor(u))
|
|
1363
|
+
: ((u /= 24), u < 14 ? o.days(Math.floor(u)) : ((u /= 7), o.weeks(Math.floor(u))))))
|
|
1347
1364
|
}
|
|
1348
1365
|
function secondsToHumanTime(n, t = DefaultTimeDeltaLabels) {
|
|
1349
1366
|
return getTimeDelta(0, { now: n * 1e3, labels: t })
|
|
@@ -1351,11 +1368,11 @@ function secondsToHumanTime(n, t = DefaultTimeDeltaLabels) {
|
|
|
1351
1368
|
function countCycles(n, t, e) {
|
|
1352
1369
|
var r, o, i
|
|
1353
1370
|
const c = ((r = e?.now) !== null && r !== void 0 ? r : Date.now()) - n,
|
|
1354
|
-
|
|
1371
|
+
s = Math.floor(c / t),
|
|
1355
1372
|
f =
|
|
1356
1373
|
t / ((o = e?.precision) !== null && o !== void 0 ? o : 1) -
|
|
1357
1374
|
Math.ceil((c % t) / ((i = e?.precision) !== null && i !== void 0 ? i : 1))
|
|
1358
|
-
return { cycles:
|
|
1375
|
+
return { cycles: s, remaining: f }
|
|
1359
1376
|
}
|
|
1360
1377
|
const throttleTimers = {}
|
|
1361
1378
|
function throttle(n, t) {
|
|
@@ -1371,10 +1388,10 @@ function getProgress(n, t, e, r) {
|
|
|
1371
1388
|
r || (r = Date.now())
|
|
1372
1389
|
const o = t / e,
|
|
1373
1390
|
i = r - n,
|
|
1374
|
-
|
|
1375
|
-
c =
|
|
1376
|
-
|
|
1377
|
-
return { deltaMs: i, progress: o, baseTimeMs:
|
|
1391
|
+
u = i / t,
|
|
1392
|
+
c = u * e,
|
|
1393
|
+
s = c - i
|
|
1394
|
+
return { deltaMs: i, progress: o, baseTimeMs: u, totalTimeMs: c, remainingTimeMs: s }
|
|
1378
1395
|
}
|
|
1379
1396
|
const dayNumberIndex = {
|
|
1380
1397
|
0: 'sunday',
|
|
@@ -1443,8 +1460,8 @@ async function getCached(n, t, e) {
|
|
|
1443
1460
|
o = tinyCache[n]
|
|
1444
1461
|
if (o && o.validUntil > r) return o.value
|
|
1445
1462
|
const i = await e(),
|
|
1446
|
-
|
|
1447
|
-
return (tinyCache[n] = { value: i, validUntil:
|
|
1463
|
+
u = r + t
|
|
1464
|
+
return (tinyCache[n] = { value: i, validUntil: u }), i
|
|
1448
1465
|
}
|
|
1449
1466
|
function joinUrl(...n) {
|
|
1450
1467
|
return n
|
|
@@ -1454,9 +1471,9 @@ function joinUrl(...n) {
|
|
|
1454
1471
|
}
|
|
1455
1472
|
function replaceBetweenStrings(n, t, e, r, o = !0) {
|
|
1456
1473
|
const i = n.indexOf(t),
|
|
1457
|
-
|
|
1458
|
-
if (i === -1 ||
|
|
1459
|
-
return o ? n.substring(0, i + t.length) + r + n.substring(
|
|
1474
|
+
u = n.indexOf(e, i + t.length)
|
|
1475
|
+
if (i === -1 || u === -1) throw Error('Start or end not found')
|
|
1476
|
+
return o ? n.substring(0, i + t.length) + r + n.substring(u) : n.substring(0, i) + r + n.substring(u + e.length)
|
|
1460
1477
|
}
|
|
1461
1478
|
function describeMarkdown(n) {
|
|
1462
1479
|
let t = 'p'
|
|
@@ -1536,12 +1553,12 @@ function createStatefulToggle(n) {
|
|
|
1536
1553
|
}
|
|
1537
1554
|
function organiseWithLimits(n, t, e, r, o) {
|
|
1538
1555
|
const i = {}
|
|
1539
|
-
for (const
|
|
1556
|
+
for (const u of Object.keys(t)) i[u] = []
|
|
1540
1557
|
;(i[r] = []), o && (n = n.sort(o))
|
|
1541
|
-
for (const
|
|
1542
|
-
const c =
|
|
1543
|
-
|
|
1544
|
-
i[
|
|
1558
|
+
for (const u of n) {
|
|
1559
|
+
const c = u[e],
|
|
1560
|
+
s = t[c] ? c : r
|
|
1561
|
+
i[s].length >= t[s] ? i[r].push(u) : i[s].push(u)
|
|
1545
1562
|
}
|
|
1546
1563
|
return i
|
|
1547
1564
|
}
|
|
@@ -1609,12 +1626,12 @@ function formatNumber(n, t) {
|
|
|
1609
1626
|
var e, r
|
|
1610
1627
|
const o = (e = t?.longForm) !== null && e !== void 0 ? e : !1,
|
|
1611
1628
|
i = t?.unit ? ` ${t.unit}` : '',
|
|
1612
|
-
|
|
1629
|
+
u = o ? longNumberUnits : shortNumberUnits,
|
|
1613
1630
|
c = (r = t?.precision) !== null && r !== void 0 ? r : 1
|
|
1614
1631
|
if (n < thresholds[0]) return `${n}${i}`
|
|
1615
|
-
for (let
|
|
1616
|
-
if (n < thresholds[
|
|
1617
|
-
return `${(n / thresholds[thresholds.length - 1]).toFixed(c)}${o ? ' ' : ''}${
|
|
1632
|
+
for (let s = 0; s < thresholds.length - 1; s++)
|
|
1633
|
+
if (n < thresholds[s + 1]) return `${(n / thresholds[s]).toFixed(c)}${o ? ' ' : ''}${u[s]}${i}`
|
|
1634
|
+
return `${(n / thresholds[thresholds.length - 1]).toFixed(c)}${o ? ' ' : ''}${u[thresholds.length - 1]}${i}`
|
|
1618
1635
|
}
|
|
1619
1636
|
function makeNumber(n) {
|
|
1620
1637
|
const t = parseFloat(n)
|
|
@@ -1694,21 +1711,21 @@ function flip(n) {
|
|
|
1694
1711
|
function getAllPermutations(n) {
|
|
1695
1712
|
const t = Object.keys(n),
|
|
1696
1713
|
e = t.map(c => n[c].length),
|
|
1697
|
-
r = e.reduce((c,
|
|
1714
|
+
r = e.reduce((c, s) => (c *= s))
|
|
1698
1715
|
let o = 1
|
|
1699
1716
|
const i = [1]
|
|
1700
1717
|
for (let c = 0; c < e.length - 1; c++) (o *= e[c]), i.push(o)
|
|
1701
|
-
const
|
|
1718
|
+
const u = []
|
|
1702
1719
|
for (let c = 0; c < r; c++) {
|
|
1703
|
-
const
|
|
1720
|
+
const s = {}
|
|
1704
1721
|
for (let f = 0; f < t.length; f++) {
|
|
1705
1722
|
const l = n[t[f]],
|
|
1706
1723
|
a = Math.floor(c / i[f]) % l.length
|
|
1707
|
-
|
|
1724
|
+
s[t[f]] = l[a]
|
|
1708
1725
|
}
|
|
1709
|
-
|
|
1726
|
+
u.push(s)
|
|
1710
1727
|
}
|
|
1711
|
-
return
|
|
1728
|
+
return u
|
|
1712
1729
|
}
|
|
1713
1730
|
function countTruthyValues(n) {
|
|
1714
1731
|
return Object.values(n).filter(t => t).length
|
|
@@ -1718,15 +1735,15 @@ function getFlatNotation(n, t, e) {
|
|
|
1718
1735
|
}
|
|
1719
1736
|
function flattenInner(n, t, e, r, o) {
|
|
1720
1737
|
if (!isObject(t)) return t
|
|
1721
|
-
for (const [i,
|
|
1738
|
+
for (const [i, u] of Object.entries(t)) {
|
|
1722
1739
|
const c = getFlatNotation(e, i, r)
|
|
1723
|
-
Array.isArray(
|
|
1740
|
+
Array.isArray(u)
|
|
1724
1741
|
? o
|
|
1725
|
-
? flattenInner(n,
|
|
1726
|
-
: (n[c] =
|
|
1727
|
-
: isObject(
|
|
1728
|
-
? flattenInner(n,
|
|
1729
|
-
: (n[c] =
|
|
1742
|
+
? flattenInner(n, u, c, !0, o)
|
|
1743
|
+
: (n[c] = u.map(s => flattenInner(Array.isArray(s) ? [] : {}, s, '', !1, o)))
|
|
1744
|
+
: isObject(u)
|
|
1745
|
+
? flattenInner(n, u, c, !1, o)
|
|
1746
|
+
: (n[c] = u)
|
|
1730
1747
|
}
|
|
1731
1748
|
return n
|
|
1732
1749
|
}
|
|
@@ -1791,9 +1808,9 @@ function makeUnique(n, t) {
|
|
|
1791
1808
|
}
|
|
1792
1809
|
function countUnique(n, t, e, r, o) {
|
|
1793
1810
|
const i = t ? n.map(t) : n,
|
|
1794
|
-
|
|
1795
|
-
for (const
|
|
1796
|
-
const c = r ? sortObjectValues(
|
|
1811
|
+
u = {}
|
|
1812
|
+
for (const s of i) u[s] = (u[s] || 0) + 1
|
|
1813
|
+
const c = r ? sortObjectValues(u, o ? (s, f) => s[1] - f[1] : (s, f) => f[1] - s[1]) : u
|
|
1797
1814
|
return e ? Object.keys(c) : c
|
|
1798
1815
|
}
|
|
1799
1816
|
function sortObjectValues(n, t) {
|
|
@@ -1805,7 +1822,7 @@ function transformToArray(n) {
|
|
|
1805
1822
|
r = n[e[0]].length
|
|
1806
1823
|
for (let o = 0; o < r; o++) {
|
|
1807
1824
|
const i = {}
|
|
1808
|
-
for (const
|
|
1825
|
+
for (const u of e) i[u] = n[u][o]
|
|
1809
1826
|
t.push(i)
|
|
1810
1827
|
}
|
|
1811
1828
|
return t
|
|
@@ -1866,9 +1883,9 @@ function makeAsyncQueue(n = 1) {
|
|
|
1866
1883
|
async function o() {
|
|
1867
1884
|
if (t.length > 0 && r < n) {
|
|
1868
1885
|
r++
|
|
1869
|
-
const
|
|
1886
|
+
const u = t.shift()
|
|
1870
1887
|
try {
|
|
1871
|
-
|
|
1888
|
+
u && (await u())
|
|
1872
1889
|
} finally {
|
|
1873
1890
|
if (--r === 0) for (; e.length > 0; ) e.shift()()
|
|
1874
1891
|
o()
|
|
@@ -1877,14 +1894,14 @@ function makeAsyncQueue(n = 1) {
|
|
|
1877
1894
|
}
|
|
1878
1895
|
async function i() {
|
|
1879
1896
|
return r
|
|
1880
|
-
? new Promise(
|
|
1881
|
-
e.push(
|
|
1897
|
+
? new Promise(u => {
|
|
1898
|
+
e.push(u)
|
|
1882
1899
|
})
|
|
1883
1900
|
: Promise.resolve()
|
|
1884
1901
|
}
|
|
1885
1902
|
return {
|
|
1886
|
-
enqueue(
|
|
1887
|
-
t.push(
|
|
1903
|
+
enqueue(u) {
|
|
1904
|
+
t.push(u), o()
|
|
1888
1905
|
},
|
|
1889
1906
|
drain: i
|
|
1890
1907
|
}
|
|
@@ -1934,29 +1951,29 @@ class Node {
|
|
|
1934
1951
|
}
|
|
1935
1952
|
function createHierarchy(n, t, e, r, o = !1) {
|
|
1936
1953
|
const i = new Map(),
|
|
1937
|
-
|
|
1938
|
-
n.forEach(
|
|
1939
|
-
const f = new Node(
|
|
1940
|
-
i.set(
|
|
1954
|
+
u = []
|
|
1955
|
+
n.forEach(s => {
|
|
1956
|
+
const f = new Node(s)
|
|
1957
|
+
i.set(s[t], f)
|
|
1941
1958
|
}),
|
|
1942
|
-
n.forEach(
|
|
1943
|
-
const f = i.get(
|
|
1959
|
+
n.forEach(s => {
|
|
1960
|
+
const f = i.get(s[t])
|
|
1944
1961
|
if (!f) return
|
|
1945
|
-
const l =
|
|
1962
|
+
const l = s[e]
|
|
1946
1963
|
if (l) {
|
|
1947
1964
|
const a = i.get(l)
|
|
1948
1965
|
a && a.children.push(f)
|
|
1949
|
-
} else
|
|
1966
|
+
} else u.push(f)
|
|
1950
1967
|
})
|
|
1951
|
-
const c =
|
|
1952
|
-
|
|
1968
|
+
const c = s => {
|
|
1969
|
+
s.children.sort((f, l) => {
|
|
1953
1970
|
const a = f.value[r],
|
|
1954
1971
|
h = l.value[r]
|
|
1955
1972
|
return o ? h - a : a - h
|
|
1956
1973
|
}),
|
|
1957
|
-
|
|
1974
|
+
s.children.forEach(c)
|
|
1958
1975
|
}
|
|
1959
|
-
return
|
|
1976
|
+
return u.forEach(c), u
|
|
1960
1977
|
}
|
|
1961
1978
|
function log2Reduce(n, t) {
|
|
1962
1979
|
if (Math.log2(n.length) % 1 !== 0) throw new Error('Array length must be a power of 2')
|
|
@@ -2066,7 +2083,7 @@ async function merkleAppend(n, t, e, r = 0) {
|
|
|
2066
2083
|
const o = new Uint8ArrayReader(t)
|
|
2067
2084
|
for (; o.max() !== 0; ) {
|
|
2068
2085
|
const i = n[r].writer.write(o)
|
|
2069
|
-
if (r === 0) for (let
|
|
2086
|
+
if (r === 0) for (let u = 0; u < n.length; u++) n[u].span += i
|
|
2070
2087
|
n[r].writer.max() === 0 && o.max() > 0 && (await merkleElevate(n, e, r))
|
|
2071
2088
|
}
|
|
2072
2089
|
return n
|
|
@@ -2087,8 +2104,8 @@ function getArgument(n, t, e, r) {
|
|
|
2087
2104
|
i = n[o]
|
|
2088
2105
|
if (!i) return (e || {})[r || t || ''] || null
|
|
2089
2106
|
if (i.includes('=')) return i.split('=')[1]
|
|
2090
|
-
const
|
|
2091
|
-
return
|
|
2107
|
+
const u = n[o + 1]
|
|
2108
|
+
return u && !u.startsWith('-') ? u : (e || {})[r || t || ''] || null
|
|
2092
2109
|
}
|
|
2093
2110
|
function getNumberArgument(n, t, e, r) {
|
|
2094
2111
|
const o = getArgument(n, t, e, r)
|
|
@@ -2100,13 +2117,13 @@ function getNumberArgument(n, t, e, r) {
|
|
|
2100
2117
|
}
|
|
2101
2118
|
}
|
|
2102
2119
|
function getBooleanArgument(n, t, e, r) {
|
|
2103
|
-
const o = n.some(
|
|
2120
|
+
const o = n.some(s => s.endsWith('-' + t)),
|
|
2104
2121
|
i = getArgument(n, t, e, r)
|
|
2105
2122
|
if (!i && o) return !0
|
|
2106
2123
|
if (!i && !o) return null
|
|
2107
|
-
const
|
|
2124
|
+
const u = ['true', '1', 'yes', 'y', 'on'],
|
|
2108
2125
|
c = ['false', '0', 'no', 'n', 'off']
|
|
2109
|
-
if (
|
|
2126
|
+
if (u.includes(i.toLowerCase())) return !0
|
|
2110
2127
|
if (c.includes(i.toLowerCase())) return !1
|
|
2111
2128
|
throw Error(`Invalid boolean argument ${t}: ${i}`)
|
|
2112
2129
|
}
|
|
@@ -2190,7 +2207,7 @@ function isBottommost(n, t, e) {
|
|
|
2190
2207
|
return !n[t][e + 1]
|
|
2191
2208
|
}
|
|
2192
2209
|
function getCorners(n, t, e) {
|
|
2193
|
-
var r, o, i,
|
|
2210
|
+
var r, o, i, u, c, s, f, l
|
|
2194
2211
|
const a = []
|
|
2195
2212
|
return n[t][e]
|
|
2196
2213
|
? isHorizontalLine(n, t, e) || isVerticalLine(n, t, e)
|
|
@@ -2199,7 +2216,7 @@ function getCorners(n, t, e) {
|
|
|
2199
2216
|
isLeftmost(n, t, e) &&
|
|
2200
2217
|
isTopmost(n, t, e) &&
|
|
2201
2218
|
a.push({ x: t, y: e }),
|
|
2202
|
-
!(!((
|
|
2219
|
+
!(!((s = n[t + 1]) === null || s === void 0) && s[e - 1]) &&
|
|
2203
2220
|
isRightmost(n, t, e) &&
|
|
2204
2221
|
isTopmost(n, t, e) &&
|
|
2205
2222
|
a.push({ x: t + 1, y: e }),
|
|
@@ -2215,7 +2232,7 @@ function getCorners(n, t, e) {
|
|
|
2215
2232
|
: (!((r = n[t - 1]) === null || r === void 0) && r[e] && n[t][e - 1] && a.push({ x: t, y: e }),
|
|
2216
2233
|
!((o = n[t + 1]) === null || o === void 0) && o[e] && n[t][e - 1] && a.push({ x: t + 1, y: e }),
|
|
2217
2234
|
!((i = n[t - 1]) === null || i === void 0) && i[e] && n[t][e + 1] && a.push({ x: t, y: e + 1 }),
|
|
2218
|
-
!((
|
|
2235
|
+
!((u = n[t + 1]) === null || u === void 0) && u[e] && n[t][e + 1] && a.push({ x: t + 1, y: e + 1 }),
|
|
2219
2236
|
a)
|
|
2220
2237
|
}
|
|
2221
2238
|
function findCorners(n, t, e, r) {
|
|
@@ -2226,43 +2243,43 @@ function findCorners(n, t, e, r) {
|
|
|
2226
2243
|
{ x: e, y: r }
|
|
2227
2244
|
]
|
|
2228
2245
|
for (let i = 0; i < n.length; i++)
|
|
2229
|
-
for (let
|
|
2230
|
-
const c = getCorners(n, i,
|
|
2231
|
-
for (const
|
|
2246
|
+
for (let u = 0; u < n[0].length; u++) {
|
|
2247
|
+
const c = getCorners(n, i, u)
|
|
2248
|
+
for (const s of c) o.some(f => f.x === s.x && f.y === s.y) || o.push(s)
|
|
2232
2249
|
}
|
|
2233
2250
|
return o.map(i => ({ x: i.x * t, y: i.y * t }))
|
|
2234
2251
|
}
|
|
2235
2252
|
function findLines(n, t) {
|
|
2236
|
-
const e = filterCoordinates(n, (
|
|
2237
|
-
Object.assign(Object.assign({},
|
|
2253
|
+
const e = filterCoordinates(n, (s, f) => n[s][f] === 0 && n[s][f + 1] !== 0, 'row-first').map(s =>
|
|
2254
|
+
Object.assign(Object.assign({}, s), { dx: 1, dy: 0 })
|
|
2238
2255
|
),
|
|
2239
|
-
r = filterCoordinates(n, (
|
|
2240
|
-
Object.assign(Object.assign({},
|
|
2256
|
+
r = filterCoordinates(n, (s, f) => n[s][f] === 0 && n[s][f - 1] !== 0, 'row-first').map(s =>
|
|
2257
|
+
Object.assign(Object.assign({}, s), { dx: 1, dy: 0 })
|
|
2241
2258
|
),
|
|
2242
2259
|
o = filterCoordinates(
|
|
2243
2260
|
n,
|
|
2244
|
-
(
|
|
2261
|
+
(s, f) => {
|
|
2245
2262
|
var l
|
|
2246
|
-
return n[
|
|
2263
|
+
return n[s][f] === 0 && ((l = n[s - 1]) === null || l === void 0 ? void 0 : l[f]) !== 0
|
|
2247
2264
|
},
|
|
2248
2265
|
'column-first'
|
|
2249
|
-
).map(
|
|
2266
|
+
).map(s => Object.assign(Object.assign({}, s), { dx: 0, dy: 1 })),
|
|
2250
2267
|
i = filterCoordinates(
|
|
2251
2268
|
n,
|
|
2252
|
-
(
|
|
2269
|
+
(s, f) => {
|
|
2253
2270
|
var l
|
|
2254
|
-
return n[
|
|
2271
|
+
return n[s][f] === 0 && ((l = n[s + 1]) === null || l === void 0 ? void 0 : l[f]) !== 0
|
|
2255
2272
|
},
|
|
2256
2273
|
'column-first'
|
|
2257
|
-
).map(
|
|
2258
|
-
e.forEach(
|
|
2259
|
-
const
|
|
2260
|
-
c = group([...r, ...e], (
|
|
2261
|
-
return [...
|
|
2262
|
-
.map(
|
|
2263
|
-
.map(
|
|
2264
|
-
start: multiplyPoint(
|
|
2265
|
-
end: multiplyPoint(addPoint(
|
|
2274
|
+
).map(s => Object.assign(Object.assign({}, s), { dx: 0, dy: 1 }))
|
|
2275
|
+
e.forEach(s => s.y++), i.forEach(s => s.x++)
|
|
2276
|
+
const u = group([...o, ...i], (s, f) => s.x === f.x && s.y - 1 === f.y),
|
|
2277
|
+
c = group([...r, ...e], (s, f) => s.y === f.y && s.x - 1 === f.x)
|
|
2278
|
+
return [...u, ...c]
|
|
2279
|
+
.map(s => ({ start: s[0], end: last(s) }))
|
|
2280
|
+
.map(s => ({
|
|
2281
|
+
start: multiplyPoint(s.start, t),
|
|
2282
|
+
end: multiplyPoint(addPoint(s.end, { x: s.start.dx, y: s.start.dy }), t)
|
|
2266
2283
|
}))
|
|
2267
2284
|
}
|
|
2268
2285
|
function getAngleInRadians(n, t) {
|
|
@@ -2275,37 +2292,37 @@ function getLineIntersectionPoint(n, t, e, r) {
|
|
|
2275
2292
|
const o = (r.y - e.y) * (t.x - n.x) - (r.x - e.x) * (t.y - n.y)
|
|
2276
2293
|
if (o === 0) return null
|
|
2277
2294
|
let i = n.y - e.y,
|
|
2278
|
-
|
|
2279
|
-
const c = (r.x - e.x) * i - (r.y - e.y) *
|
|
2280
|
-
|
|
2295
|
+
u = n.x - e.x
|
|
2296
|
+
const c = (r.x - e.x) * i - (r.y - e.y) * u,
|
|
2297
|
+
s = (t.x - n.x) * i - (t.y - n.y) * u
|
|
2281
2298
|
return (
|
|
2282
2299
|
(i = c / o),
|
|
2283
|
-
(
|
|
2284
|
-
i > 0 && i < 1 &&
|
|
2300
|
+
(u = s / o),
|
|
2301
|
+
i > 0 && i < 1 && u > 0 && u < 1 ? { x: n.x + i * (t.x - n.x), y: n.y + i * (t.y - n.y) } : null
|
|
2285
2302
|
)
|
|
2286
2303
|
}
|
|
2287
2304
|
function raycast(n, t, e) {
|
|
2288
2305
|
const r = [],
|
|
2289
2306
|
o = pushPoint(n, e, 1e4)
|
|
2290
2307
|
for (const i of t) {
|
|
2291
|
-
const
|
|
2292
|
-
|
|
2308
|
+
const u = getLineIntersectionPoint(n, o, i.start, i.end)
|
|
2309
|
+
u && r.push(u)
|
|
2293
2310
|
}
|
|
2294
2311
|
return r.length
|
|
2295
|
-
? r.reduce((i,
|
|
2296
|
-
const c = getDistanceBetweenPoints(n,
|
|
2297
|
-
|
|
2298
|
-
return c <
|
|
2312
|
+
? r.reduce((i, u) => {
|
|
2313
|
+
const c = getDistanceBetweenPoints(n, u),
|
|
2314
|
+
s = getDistanceBetweenPoints(n, i)
|
|
2315
|
+
return c < s ? u : i
|
|
2299
2316
|
})
|
|
2300
2317
|
: null
|
|
2301
2318
|
}
|
|
2302
2319
|
function raycastCircle(n, t, e) {
|
|
2303
2320
|
const o = getSortedRayAngles(n, e),
|
|
2304
2321
|
i = []
|
|
2305
|
-
for (const
|
|
2306
|
-
const c = raycast(n, t,
|
|
2307
|
-
|
|
2308
|
-
c && i.push(c),
|
|
2322
|
+
for (const u of o) {
|
|
2323
|
+
const c = raycast(n, t, u - 0.001),
|
|
2324
|
+
s = raycast(n, t, u + 0.001)
|
|
2325
|
+
c && i.push(c), s && i.push(s)
|
|
2309
2326
|
}
|
|
2310
2327
|
return i
|
|
2311
2328
|
}
|