cafe-utility 16.1.0 → 16.1.1
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.js +134 -132
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -24,7 +24,7 @@ async function runInParallelBatches(n, e = 1) {
|
|
|
24
24
|
const t = splitByCount(n, e),
|
|
25
25
|
r = [],
|
|
26
26
|
o = t.map(async i => {
|
|
27
|
-
for (const
|
|
27
|
+
for (const u of i) r.push(await u())
|
|
28
28
|
})
|
|
29
29
|
return await Promise.all(o), r
|
|
30
30
|
}
|
|
@@ -120,16 +120,16 @@ function pickManyUnique(n, e, t, r = Math.random) {
|
|
|
120
120
|
const o = []
|
|
121
121
|
for (; o.length < e; ) {
|
|
122
122
|
const i = pick(n, r)
|
|
123
|
-
o.some(
|
|
123
|
+
o.some(u => t(u, i)) || o.push(i)
|
|
124
124
|
}
|
|
125
125
|
return o
|
|
126
126
|
}
|
|
127
127
|
function pickGuaranteed(n, e, t, r, o, i = Math.random) {
|
|
128
|
-
const
|
|
128
|
+
const u = n.filter(s => s !== e && s !== t),
|
|
129
129
|
c = []
|
|
130
|
-
for (e !== null && c.push(e);
|
|
131
|
-
const
|
|
132
|
-
o(s
|
|
130
|
+
for (e !== null && c.push(e); u.length && c.length < r; ) {
|
|
131
|
+
const s = exports.Random.intBetween(0, u.length - 1, i)
|
|
132
|
+
o(u[s], c) && c.push(u[s]), u.splice(s, 1)
|
|
133
133
|
}
|
|
134
134
|
return shuffle(c, i), { values: c, indexOfGuaranteed: e !== null ? c.indexOf(e) : -1 }
|
|
135
135
|
}
|
|
@@ -138,7 +138,7 @@ function last(n) {
|
|
|
138
138
|
}
|
|
139
139
|
function pickWeighted(n, e, t) {
|
|
140
140
|
if ((isUndefined(t) && (t = Math.random()), n.length !== e.length)) throw new Error('Array length mismatch')
|
|
141
|
-
let r = e.reduce((i,
|
|
141
|
+
let r = e.reduce((i, u) => i + u, 0)
|
|
142
142
|
const o = t * r
|
|
143
143
|
for (let i = 0; i < n.length - 1; i++) if (((r -= e[i]), o >= r)) return n[i]
|
|
144
144
|
return last(n)
|
|
@@ -147,7 +147,7 @@ function sortWeighted(n, e, t = Math.random) {
|
|
|
147
147
|
const r = e.map(i => t() * i),
|
|
148
148
|
o = []
|
|
149
149
|
for (let i = 0; i < n.length; i++) o.push([n[i], r[i]])
|
|
150
|
-
return o.sort((i,
|
|
150
|
+
return o.sort((i, u) => u[1] - i[1]).map(i => i[0])
|
|
151
151
|
}
|
|
152
152
|
function getDeep(n, e) {
|
|
153
153
|
const t = e.split('.')
|
|
@@ -165,9 +165,9 @@ function setDeep(n, e, t) {
|
|
|
165
165
|
const r = e.split(/\.|\[/)
|
|
166
166
|
let o = n
|
|
167
167
|
for (let i = 0; i < r.length; i++) {
|
|
168
|
-
const
|
|
168
|
+
const u = r[i],
|
|
169
169
|
c = i < r.length - 1 && r[i + 1].includes(']'),
|
|
170
|
-
l =
|
|
170
|
+
l = u.includes(']') ? u.replace(/\[|\]/g, '') : u
|
|
171
171
|
if (i === r.length - 1) return (o[l] = t), t
|
|
172
172
|
isObject(o[l]) || (c ? (o[l] = []) : (o[l] = {})), (o = o[l])
|
|
173
173
|
}
|
|
@@ -357,6 +357,8 @@ function asInteger(n) {
|
|
|
357
357
|
return asNumber(n) | 0
|
|
358
358
|
}
|
|
359
359
|
function asBoolean(n) {
|
|
360
|
+
if (n === 'true') return !0
|
|
361
|
+
if (n === 'false') return !1
|
|
360
362
|
if (!isBoolean(n)) throw new TypeError('Expected boolean, got: ' + n)
|
|
361
363
|
return n
|
|
362
364
|
}
|
|
@@ -682,12 +684,12 @@ function expand(n) {
|
|
|
682
684
|
const r = t[1].split(','),
|
|
683
685
|
o = n.slice(0, t.index),
|
|
684
686
|
i = n.slice(t.index + t[0].length)
|
|
685
|
-
let
|
|
687
|
+
let u = []
|
|
686
688
|
for (const c of r) {
|
|
687
|
-
const
|
|
688
|
-
|
|
689
|
+
const s = expand(o + c + i)
|
|
690
|
+
u = u.concat(s)
|
|
689
691
|
}
|
|
690
|
-
return
|
|
692
|
+
return u
|
|
691
693
|
}
|
|
692
694
|
function shrinkTrim(n) {
|
|
693
695
|
return n.replace(/\s+/g, ' ').replace(/\s$|^\s/g, '')
|
|
@@ -911,13 +913,13 @@ function base64ToUint8Array(n) {
|
|
|
911
913
|
let o = 0,
|
|
912
914
|
i = 0
|
|
913
915
|
for (; o < n.length; ) {
|
|
914
|
-
const
|
|
916
|
+
const u = BASE64_CHARS.indexOf(n.charAt(o++)),
|
|
915
917
|
c = BASE64_CHARS.indexOf(n.charAt(o++)),
|
|
916
|
-
|
|
918
|
+
s = BASE64_CHARS.indexOf(n.charAt(o++)),
|
|
917
919
|
l = BASE64_CHARS.indexOf(n.charAt(o++)),
|
|
918
|
-
f = (
|
|
919
|
-
a = ((c & 15) << 4) | (
|
|
920
|
-
h = ((
|
|
920
|
+
f = (u << 2) | (c >> 4),
|
|
921
|
+
a = ((c & 15) << 4) | (s >> 2),
|
|
922
|
+
h = ((s & 3) << 6) | l
|
|
921
923
|
;(r[i++] = f), i < t && (r[i++] = a), i < t && (r[i++] = h)
|
|
922
924
|
}
|
|
923
925
|
return r
|
|
@@ -928,12 +930,12 @@ function uint8ArrayToBase64(n) {
|
|
|
928
930
|
for (let r = 0; r < n.length; r += 3) {
|
|
929
931
|
const o = n[r],
|
|
930
932
|
i = n[r + 1],
|
|
931
|
-
|
|
933
|
+
u = n[r + 2],
|
|
932
934
|
c = o >> 2,
|
|
933
|
-
|
|
934
|
-
l = ((i & 15) << 2) | (
|
|
935
|
-
f =
|
|
936
|
-
;(e += BASE64_CHARS[c] + BASE64_CHARS[
|
|
935
|
+
s = ((o & 3) << 4) | (i >> 4),
|
|
936
|
+
l = ((i & 15) << 2) | (u >> 6),
|
|
937
|
+
f = u & 63
|
|
938
|
+
;(e += BASE64_CHARS[c] + BASE64_CHARS[s]),
|
|
937
939
|
r + 1 < n.length ? (e += BASE64_CHARS[l]) : t++,
|
|
938
940
|
r + 2 < n.length ? (e += BASE64_CHARS[f]) : t++
|
|
939
941
|
}
|
|
@@ -960,9 +962,9 @@ function route(n, e) {
|
|
|
960
962
|
if (t.length !== r.length) return null
|
|
961
963
|
const o = {}
|
|
962
964
|
for (let i = 0; i < t.length; i++) {
|
|
963
|
-
const
|
|
964
|
-
if (
|
|
965
|
-
else if (
|
|
965
|
+
const u = t[i]
|
|
966
|
+
if (u.startsWith(':')) o[u.slice(1)] = r[i]
|
|
967
|
+
else if (u !== r[i]) return null
|
|
966
968
|
}
|
|
967
969
|
return o
|
|
968
970
|
}
|
|
@@ -973,25 +975,25 @@ function explodeReplace(n, e, t) {
|
|
|
973
975
|
}
|
|
974
976
|
function generateVariants(n, e, t, r = Math.random) {
|
|
975
977
|
const o = exports.Arrays.shuffle(
|
|
976
|
-
e.map(
|
|
978
|
+
e.map(u => ({
|
|
977
979
|
variants: exports.Arrays.shuffle(
|
|
978
|
-
|
|
980
|
+
u.variants.map(c => c),
|
|
979
981
|
r
|
|
980
982
|
),
|
|
981
|
-
avoid:
|
|
983
|
+
avoid: u.avoid
|
|
982
984
|
})),
|
|
983
985
|
r
|
|
984
986
|
),
|
|
985
987
|
i = []
|
|
986
|
-
for (const
|
|
987
|
-
const c =
|
|
988
|
-
|
|
989
|
-
if (
|
|
988
|
+
for (const u of o) {
|
|
989
|
+
const c = u.variants.filter(l => l !== u.avoid),
|
|
990
|
+
s = c.find(l => n.includes(l))
|
|
991
|
+
if (s && (pushAll(i, explodeReplace(n, s, c)), i.length >= t)) break
|
|
990
992
|
}
|
|
991
993
|
if (i.length < t)
|
|
992
|
-
for (const
|
|
993
|
-
const c =
|
|
994
|
-
if (c && (pushAll(i, explodeReplace(n, c,
|
|
994
|
+
for (const u of o) {
|
|
995
|
+
const c = u.variants.find(s => n.includes(s))
|
|
996
|
+
if (c && (pushAll(i, explodeReplace(n, c, u.variants)), i.length >= t)) break
|
|
995
997
|
}
|
|
996
998
|
return i.slice(0, t)
|
|
997
999
|
}
|
|
@@ -1058,8 +1060,8 @@ function resolveRemainingVariablesWithDefaults(n, e = '$', t = ':') {
|
|
|
1058
1060
|
if (n[r + o.length + 1] === t)
|
|
1059
1061
|
if (n[r + o.length + 2] === t) n = n.replace(`${e}${o}${t}${t}`, '')
|
|
1060
1062
|
else {
|
|
1061
|
-
const
|
|
1062
|
-
n = n.replace(`${e}${o}${t}${
|
|
1063
|
+
const u = readNextWord(n, r + o.length + 2)
|
|
1064
|
+
n = n.replace(`${e}${o}${t}${u}`, u)
|
|
1063
1065
|
}
|
|
1064
1066
|
r = n.indexOf(e, r + 1)
|
|
1065
1067
|
}
|
|
@@ -1071,8 +1073,8 @@ function resolveMarkdownLinks(n, e) {
|
|
|
1071
1073
|
const r = lastIndexOfBefore(n, '[', t),
|
|
1072
1074
|
o = n.indexOf(')', t)
|
|
1073
1075
|
if (r !== -1 && o !== -1) {
|
|
1074
|
-
const [i,
|
|
1075
|
-
c = e(i,
|
|
1076
|
+
const [i, u] = n.slice(r + 1, o).split(']('),
|
|
1077
|
+
c = e(i, u)
|
|
1076
1078
|
n = n.slice(0, r) + c + n.slice(o + 1)
|
|
1077
1079
|
}
|
|
1078
1080
|
t = n.indexOf('](', t + 1)
|
|
@@ -1105,8 +1107,8 @@ function parseCsv(n, e = ',', t = '"') {
|
|
|
1105
1107
|
const r = []
|
|
1106
1108
|
let o = '',
|
|
1107
1109
|
i = !1
|
|
1108
|
-
const
|
|
1109
|
-
for (const c of
|
|
1110
|
+
const u = n.split('')
|
|
1111
|
+
for (const c of u) c === e && !i ? (r.push(o), (o = '')) : c === t && ((!o && !i) || i) ? (i = !i) : (o += c)
|
|
1110
1112
|
return r.push(o), r
|
|
1111
1113
|
}
|
|
1112
1114
|
function humanizeProgress(n) {
|
|
@@ -1188,11 +1190,11 @@ function getAgoStructured(n, e) {
|
|
|
1188
1190
|
function countCycles(n, e, t) {
|
|
1189
1191
|
var r, o, i
|
|
1190
1192
|
const c = ((r = t?.now) !== null && r !== void 0 ? r : Date.now()) - n,
|
|
1191
|
-
|
|
1193
|
+
s = Math.floor(c / e),
|
|
1192
1194
|
l =
|
|
1193
1195
|
e / ((o = t?.precision) !== null && o !== void 0 ? o : 1) -
|
|
1194
1196
|
Math.ceil((c % e) / ((i = t?.precision) !== null && i !== void 0 ? i : 1))
|
|
1195
|
-
return { cycles:
|
|
1197
|
+
return { cycles: s, remaining: l }
|
|
1196
1198
|
}
|
|
1197
1199
|
const throttleTimers = {}
|
|
1198
1200
|
function throttle(n, e) {
|
|
@@ -1208,10 +1210,10 @@ function getProgress(n, e, t, r) {
|
|
|
1208
1210
|
r || (r = Date.now())
|
|
1209
1211
|
const o = e / t,
|
|
1210
1212
|
i = r - n,
|
|
1211
|
-
|
|
1212
|
-
c =
|
|
1213
|
-
|
|
1214
|
-
return { deltaMs: i, progress: o, baseTimeMs:
|
|
1213
|
+
u = i / e,
|
|
1214
|
+
c = u * t,
|
|
1215
|
+
s = c - i
|
|
1216
|
+
return { deltaMs: i, progress: o, baseTimeMs: u, totalTimeMs: c, remainingTimeMs: s }
|
|
1215
1217
|
}
|
|
1216
1218
|
const dayNumberIndex = {
|
|
1217
1219
|
0: 'sunday',
|
|
@@ -1280,8 +1282,8 @@ async function getCached(n, e, t) {
|
|
|
1280
1282
|
o = tinyCache[n]
|
|
1281
1283
|
if (o && o.validUntil > r) return o.value
|
|
1282
1284
|
const i = await t(),
|
|
1283
|
-
|
|
1284
|
-
return (tinyCache[n] = { value: i, validUntil:
|
|
1285
|
+
u = r + e
|
|
1286
|
+
return (tinyCache[n] = { value: i, validUntil: u }), i
|
|
1285
1287
|
}
|
|
1286
1288
|
function joinUrl(...n) {
|
|
1287
1289
|
return n
|
|
@@ -1291,9 +1293,9 @@ function joinUrl(...n) {
|
|
|
1291
1293
|
}
|
|
1292
1294
|
function replaceBetweenStrings(n, e, t, r, o = !0) {
|
|
1293
1295
|
const i = n.indexOf(e),
|
|
1294
|
-
|
|
1295
|
-
if (i === -1 ||
|
|
1296
|
-
return o ? n.substring(0, i + e.length) + r + n.substring(
|
|
1296
|
+
u = n.indexOf(t, i + e.length)
|
|
1297
|
+
if (i === -1 || u === -1) throw Error('Start or end not found')
|
|
1298
|
+
return o ? n.substring(0, i + e.length) + r + n.substring(u) : n.substring(0, i) + r + n.substring(u + t.length)
|
|
1297
1299
|
}
|
|
1298
1300
|
function describeMarkdown(n) {
|
|
1299
1301
|
let e = 'p'
|
|
@@ -1373,20 +1375,20 @@ function createStatefulToggle(n) {
|
|
|
1373
1375
|
}
|
|
1374
1376
|
function organiseWithLimits(n, e, t, r, o) {
|
|
1375
1377
|
const i = {}
|
|
1376
|
-
for (const
|
|
1378
|
+
for (const u of Object.keys(e)) i[u] = []
|
|
1377
1379
|
;(i[r] = []), o && (n = n.sort(o))
|
|
1378
|
-
for (const
|
|
1379
|
-
const c =
|
|
1380
|
-
|
|
1381
|
-
i[
|
|
1380
|
+
for (const u of n) {
|
|
1381
|
+
const c = u[t],
|
|
1382
|
+
s = e[c] ? c : r
|
|
1383
|
+
i[s].length >= e[s] ? i[r].push(u) : i[s].push(u)
|
|
1382
1384
|
}
|
|
1383
1385
|
return i
|
|
1384
1386
|
}
|
|
1385
1387
|
function diffKeys(n, e) {
|
|
1386
1388
|
const t = Object.keys(n),
|
|
1387
1389
|
r = Object.keys(e),
|
|
1388
|
-
o = t.filter(
|
|
1389
|
-
i = r.filter(
|
|
1390
|
+
o = t.filter(u => !r.includes(u)),
|
|
1391
|
+
i = r.filter(u => !t.includes(u))
|
|
1390
1392
|
return { uniqueToA: o, uniqueToB: i }
|
|
1391
1393
|
}
|
|
1392
1394
|
function pickRandomKey(n) {
|
|
@@ -1441,12 +1443,12 @@ function formatNumber(n, e) {
|
|
|
1441
1443
|
var t, r
|
|
1442
1444
|
const o = (t = e?.longForm) !== null && t !== void 0 ? t : !1,
|
|
1443
1445
|
i = e?.unit ? ` ${e.unit}` : '',
|
|
1444
|
-
|
|
1446
|
+
u = o ? longNumberUnits : shortNumberUnits,
|
|
1445
1447
|
c = (r = e?.precision) !== null && r !== void 0 ? r : 1
|
|
1446
1448
|
if (n < thresholds[0]) return `${n}${i}`
|
|
1447
|
-
for (let
|
|
1448
|
-
if (n < thresholds[
|
|
1449
|
-
return `${(n / thresholds[thresholds.length - 1]).toFixed(c)}${o ? ' ' : ''}${
|
|
1449
|
+
for (let s = 0; s < thresholds.length - 1; s++)
|
|
1450
|
+
if (n < thresholds[s + 1]) return `${(n / thresholds[s]).toFixed(c)}${o ? ' ' : ''}${u[s]}${i}`
|
|
1451
|
+
return `${(n / thresholds[thresholds.length - 1]).toFixed(c)}${o ? ' ' : ''}${u[thresholds.length - 1]}${i}`
|
|
1450
1452
|
}
|
|
1451
1453
|
function makeNumber(n) {
|
|
1452
1454
|
const e = parseFloat(n)
|
|
@@ -1526,21 +1528,21 @@ function flip(n) {
|
|
|
1526
1528
|
function getAllPermutations(n) {
|
|
1527
1529
|
const e = Object.keys(n),
|
|
1528
1530
|
t = e.map(c => n[c].length),
|
|
1529
|
-
r = t.reduce((c,
|
|
1531
|
+
r = t.reduce((c, s) => (c *= s))
|
|
1530
1532
|
let o = 1
|
|
1531
1533
|
const i = [1]
|
|
1532
1534
|
for (let c = 0; c < t.length - 1; c++) (o *= t[c]), i.push(o)
|
|
1533
|
-
const
|
|
1535
|
+
const u = []
|
|
1534
1536
|
for (let c = 0; c < r; c++) {
|
|
1535
|
-
const
|
|
1537
|
+
const s = {}
|
|
1536
1538
|
for (let l = 0; l < e.length; l++) {
|
|
1537
1539
|
const f = n[e[l]],
|
|
1538
1540
|
a = Math.floor(c / i[l]) % f.length
|
|
1539
|
-
|
|
1541
|
+
s[e[l]] = f[a]
|
|
1540
1542
|
}
|
|
1541
|
-
|
|
1543
|
+
u.push(s)
|
|
1542
1544
|
}
|
|
1543
|
-
return
|
|
1545
|
+
return u
|
|
1544
1546
|
}
|
|
1545
1547
|
function countTruthyValues(n) {
|
|
1546
1548
|
return Object.values(n).filter(e => e).length
|
|
@@ -1550,15 +1552,15 @@ function getFlatNotation(n, e, t) {
|
|
|
1550
1552
|
}
|
|
1551
1553
|
function flattenInner(n, e, t, r, o) {
|
|
1552
1554
|
if (!isObject(e)) return e
|
|
1553
|
-
for (const [i,
|
|
1555
|
+
for (const [i, u] of Object.entries(e)) {
|
|
1554
1556
|
const c = getFlatNotation(t, i, r)
|
|
1555
|
-
Array.isArray(
|
|
1557
|
+
Array.isArray(u)
|
|
1556
1558
|
? o
|
|
1557
|
-
? flattenInner(n,
|
|
1558
|
-
: (n[c] =
|
|
1559
|
-
: isObject(
|
|
1560
|
-
? flattenInner(n,
|
|
1561
|
-
: (n[c] =
|
|
1559
|
+
? flattenInner(n, u, c, !0, o)
|
|
1560
|
+
: (n[c] = u.map(s => flattenInner(Array.isArray(s) ? [] : {}, s, '', !1, o)))
|
|
1561
|
+
: isObject(u)
|
|
1562
|
+
? flattenInner(n, u, c, !1, o)
|
|
1563
|
+
: (n[c] = u)
|
|
1562
1564
|
}
|
|
1563
1565
|
return n
|
|
1564
1566
|
}
|
|
@@ -1623,9 +1625,9 @@ function makeUnique(n, e) {
|
|
|
1623
1625
|
}
|
|
1624
1626
|
function countUnique(n, e, t, r, o) {
|
|
1625
1627
|
const i = e ? n.map(e) : n,
|
|
1626
|
-
|
|
1627
|
-
for (const
|
|
1628
|
-
const c = r ? sortObjectValues(
|
|
1628
|
+
u = {}
|
|
1629
|
+
for (const s of i) u[s] = (u[s] || 0) + 1
|
|
1630
|
+
const c = r ? sortObjectValues(u, o ? (s, l) => s[1] - l[1] : (s, l) => l[1] - s[1]) : u
|
|
1629
1631
|
return t ? Object.keys(c) : c
|
|
1630
1632
|
}
|
|
1631
1633
|
function sortObjectValues(n, e) {
|
|
@@ -1637,7 +1639,7 @@ function transformToArray(n) {
|
|
|
1637
1639
|
r = n[t[0]].length
|
|
1638
1640
|
for (let o = 0; o < r; o++) {
|
|
1639
1641
|
const i = {}
|
|
1640
|
-
for (const
|
|
1642
|
+
for (const u of t) i[u] = n[u][o]
|
|
1641
1643
|
e.push(i)
|
|
1642
1644
|
}
|
|
1643
1645
|
return e
|
|
@@ -1695,9 +1697,9 @@ function makeAsyncQueue(n = 1) {
|
|
|
1695
1697
|
async function o() {
|
|
1696
1698
|
if (e.length > 0 && r < n) {
|
|
1697
1699
|
r++
|
|
1698
|
-
const
|
|
1700
|
+
const u = e.shift()
|
|
1699
1701
|
try {
|
|
1700
|
-
|
|
1702
|
+
u && (await u())
|
|
1701
1703
|
} finally {
|
|
1702
1704
|
if (--r === 0) for (; t.length > 0; ) t.shift()()
|
|
1703
1705
|
o()
|
|
@@ -1706,14 +1708,14 @@ function makeAsyncQueue(n = 1) {
|
|
|
1706
1708
|
}
|
|
1707
1709
|
async function i() {
|
|
1708
1710
|
return r
|
|
1709
|
-
? new Promise(
|
|
1710
|
-
t.push(
|
|
1711
|
+
? new Promise(u => {
|
|
1712
|
+
t.push(u)
|
|
1711
1713
|
})
|
|
1712
1714
|
: Promise.resolve()
|
|
1713
1715
|
}
|
|
1714
1716
|
return {
|
|
1715
|
-
enqueue(
|
|
1716
|
-
e.push(
|
|
1717
|
+
enqueue(u) {
|
|
1718
|
+
e.push(u), o()
|
|
1717
1719
|
},
|
|
1718
1720
|
drain: i
|
|
1719
1721
|
}
|
|
@@ -1748,8 +1750,8 @@ function getArgument(n, e, t, r) {
|
|
|
1748
1750
|
i = n[o]
|
|
1749
1751
|
if (!i) return (t || {})[r || e || ''] || null
|
|
1750
1752
|
if (i.includes('=')) return i.split('=')[1]
|
|
1751
|
-
const
|
|
1752
|
-
return
|
|
1753
|
+
const u = n[o + 1]
|
|
1754
|
+
return u && !u.startsWith('-') ? u : (t || {})[r || e || ''] || null
|
|
1753
1755
|
}
|
|
1754
1756
|
function getNumberArgument(n, e, t, r) {
|
|
1755
1757
|
const o = getArgument(n, e, t, r)
|
|
@@ -1761,13 +1763,13 @@ function getNumberArgument(n, e, t, r) {
|
|
|
1761
1763
|
}
|
|
1762
1764
|
}
|
|
1763
1765
|
function getBooleanArgument(n, e, t, r) {
|
|
1764
|
-
const o = n.some(
|
|
1766
|
+
const o = n.some(s => s.endsWith('-' + e)),
|
|
1765
1767
|
i = getArgument(n, e, t, r)
|
|
1766
1768
|
if (!i && o) return !0
|
|
1767
1769
|
if (!i && !o) return null
|
|
1768
|
-
const
|
|
1770
|
+
const u = ['true', '1', 'yes', 'y', 'on'],
|
|
1769
1771
|
c = ['false', '0', 'no', 'n', 'off']
|
|
1770
|
-
if (
|
|
1772
|
+
if (u.includes(i.toLowerCase())) return !0
|
|
1771
1773
|
if (c.includes(i.toLowerCase())) return !1
|
|
1772
1774
|
throw Error(`Invalid boolean argument ${e}: ${i}`)
|
|
1773
1775
|
}
|
|
@@ -1851,7 +1853,7 @@ function isBottommost(n, e, t) {
|
|
|
1851
1853
|
return !n[e][t + 1]
|
|
1852
1854
|
}
|
|
1853
1855
|
function getCorners(n, e, t) {
|
|
1854
|
-
var r, o, i,
|
|
1856
|
+
var r, o, i, u, c, s, l, f
|
|
1855
1857
|
const a = []
|
|
1856
1858
|
return n[e][t]
|
|
1857
1859
|
? isHorizontalLine(n, e, t) || isVerticalLine(n, e, t)
|
|
@@ -1860,7 +1862,7 @@ function getCorners(n, e, t) {
|
|
|
1860
1862
|
isLeftmost(n, e, t) &&
|
|
1861
1863
|
isTopmost(n, e, t) &&
|
|
1862
1864
|
a.push({ x: e, y: t }),
|
|
1863
|
-
!(!((
|
|
1865
|
+
!(!((s = n[e + 1]) === null || s === void 0) && s[t - 1]) &&
|
|
1864
1866
|
isRightmost(n, e, t) &&
|
|
1865
1867
|
isTopmost(n, e, t) &&
|
|
1866
1868
|
a.push({ x: e + 1, y: t }),
|
|
@@ -1876,7 +1878,7 @@ function getCorners(n, e, t) {
|
|
|
1876
1878
|
: (!((r = n[e - 1]) === null || r === void 0) && r[t] && n[e][t - 1] && a.push({ x: e, y: t }),
|
|
1877
1879
|
!((o = n[e + 1]) === null || o === void 0) && o[t] && n[e][t - 1] && a.push({ x: e + 1, y: t }),
|
|
1878
1880
|
!((i = n[e - 1]) === null || i === void 0) && i[t] && n[e][t + 1] && a.push({ x: e, y: t + 1 }),
|
|
1879
|
-
!((
|
|
1881
|
+
!((u = n[e + 1]) === null || u === void 0) && u[t] && n[e][t + 1] && a.push({ x: e + 1, y: t + 1 }),
|
|
1880
1882
|
a)
|
|
1881
1883
|
}
|
|
1882
1884
|
function findCorners(n, e, t, r) {
|
|
@@ -1887,43 +1889,43 @@ function findCorners(n, e, t, r) {
|
|
|
1887
1889
|
{ x: t, y: r }
|
|
1888
1890
|
]
|
|
1889
1891
|
for (let i = 0; i < n.length; i++)
|
|
1890
|
-
for (let
|
|
1891
|
-
const c = getCorners(n, i,
|
|
1892
|
-
for (const
|
|
1892
|
+
for (let u = 0; u < n[0].length; u++) {
|
|
1893
|
+
const c = getCorners(n, i, u)
|
|
1894
|
+
for (const s of c) o.some(l => l.x === s.x && l.y === s.y) || o.push(s)
|
|
1893
1895
|
}
|
|
1894
1896
|
return o.map(i => ({ x: i.x * e, y: i.y * e }))
|
|
1895
1897
|
}
|
|
1896
1898
|
function findLines(n, e) {
|
|
1897
|
-
const t = filterCoordinates(n, (
|
|
1898
|
-
Object.assign(Object.assign({},
|
|
1899
|
+
const t = filterCoordinates(n, (s, l) => n[s][l] === 0 && n[s][l + 1] !== 0, 'row-first').map(s =>
|
|
1900
|
+
Object.assign(Object.assign({}, s), { dx: 1, dy: 0 })
|
|
1899
1901
|
),
|
|
1900
|
-
r = filterCoordinates(n, (
|
|
1901
|
-
Object.assign(Object.assign({},
|
|
1902
|
+
r = filterCoordinates(n, (s, l) => n[s][l] === 0 && n[s][l - 1] !== 0, 'row-first').map(s =>
|
|
1903
|
+
Object.assign(Object.assign({}, s), { dx: 1, dy: 0 })
|
|
1902
1904
|
),
|
|
1903
1905
|
o = filterCoordinates(
|
|
1904
1906
|
n,
|
|
1905
|
-
(
|
|
1907
|
+
(s, l) => {
|
|
1906
1908
|
var f
|
|
1907
|
-
return n[
|
|
1909
|
+
return n[s][l] === 0 && ((f = n[s - 1]) === null || f === void 0 ? void 0 : f[l]) !== 0
|
|
1908
1910
|
},
|
|
1909
1911
|
'column-first'
|
|
1910
|
-
).map(
|
|
1912
|
+
).map(s => Object.assign(Object.assign({}, s), { dx: 0, dy: 1 })),
|
|
1911
1913
|
i = filterCoordinates(
|
|
1912
1914
|
n,
|
|
1913
|
-
(
|
|
1915
|
+
(s, l) => {
|
|
1914
1916
|
var f
|
|
1915
|
-
return n[
|
|
1917
|
+
return n[s][l] === 0 && ((f = n[s + 1]) === null || f === void 0 ? void 0 : f[l]) !== 0
|
|
1916
1918
|
},
|
|
1917
1919
|
'column-first'
|
|
1918
|
-
).map(
|
|
1919
|
-
t.forEach(
|
|
1920
|
-
const
|
|
1921
|
-
c = group([...r, ...t], (
|
|
1922
|
-
return [...
|
|
1923
|
-
.map(
|
|
1924
|
-
.map(
|
|
1925
|
-
start: multiplyPoint(
|
|
1926
|
-
end: multiplyPoint(addPoint(
|
|
1920
|
+
).map(s => Object.assign(Object.assign({}, s), { dx: 0, dy: 1 }))
|
|
1921
|
+
t.forEach(s => s.y++), i.forEach(s => s.x++)
|
|
1922
|
+
const u = group([...o, ...i], (s, l) => s.x === l.x && s.y - 1 === l.y),
|
|
1923
|
+
c = group([...r, ...t], (s, l) => s.y === l.y && s.x - 1 === l.x)
|
|
1924
|
+
return [...u, ...c]
|
|
1925
|
+
.map(s => ({ start: s[0], end: last(s) }))
|
|
1926
|
+
.map(s => ({
|
|
1927
|
+
start: multiplyPoint(s.start, e),
|
|
1928
|
+
end: multiplyPoint(addPoint(s.end, { x: s.start.dx, y: s.start.dy }), e)
|
|
1927
1929
|
}))
|
|
1928
1930
|
}
|
|
1929
1931
|
function getAngleInRadians(n, e) {
|
|
@@ -1936,37 +1938,37 @@ function getLineIntersectionPoint(n, e, t, r) {
|
|
|
1936
1938
|
const o = (r.y - t.y) * (e.x - n.x) - (r.x - t.x) * (e.y - n.y)
|
|
1937
1939
|
if (o === 0) return null
|
|
1938
1940
|
let i = n.y - t.y,
|
|
1939
|
-
|
|
1940
|
-
const c = (r.x - t.x) * i - (r.y - t.y) *
|
|
1941
|
-
|
|
1941
|
+
u = n.x - t.x
|
|
1942
|
+
const c = (r.x - t.x) * i - (r.y - t.y) * u,
|
|
1943
|
+
s = (e.x - n.x) * i - (e.y - n.y) * u
|
|
1942
1944
|
return (
|
|
1943
1945
|
(i = c / o),
|
|
1944
|
-
(
|
|
1945
|
-
i > 0 && i < 1 &&
|
|
1946
|
+
(u = s / o),
|
|
1947
|
+
i > 0 && i < 1 && u > 0 && u < 1 ? { x: n.x + i * (e.x - n.x), y: n.y + i * (e.y - n.y) } : null
|
|
1946
1948
|
)
|
|
1947
1949
|
}
|
|
1948
1950
|
function raycast(n, e, t) {
|
|
1949
1951
|
const r = [],
|
|
1950
1952
|
o = pushPoint(n, t, 1e4)
|
|
1951
1953
|
for (const i of e) {
|
|
1952
|
-
const
|
|
1953
|
-
|
|
1954
|
+
const u = getLineIntersectionPoint(n, o, i.start, i.end)
|
|
1955
|
+
u && r.push(u)
|
|
1954
1956
|
}
|
|
1955
1957
|
return r.length
|
|
1956
|
-
? r.reduce((i,
|
|
1957
|
-
const c = getDistanceBetweenPoints(n,
|
|
1958
|
-
|
|
1959
|
-
return c <
|
|
1958
|
+
? r.reduce((i, u) => {
|
|
1959
|
+
const c = getDistanceBetweenPoints(n, u),
|
|
1960
|
+
s = getDistanceBetweenPoints(n, i)
|
|
1961
|
+
return c < s ? u : i
|
|
1960
1962
|
})
|
|
1961
1963
|
: null
|
|
1962
1964
|
}
|
|
1963
1965
|
function raycastCircle(n, e, t) {
|
|
1964
1966
|
const o = getSortedRayAngles(n, t),
|
|
1965
1967
|
i = []
|
|
1966
|
-
for (const
|
|
1967
|
-
const c = raycast(n, e,
|
|
1968
|
-
|
|
1969
|
-
c && i.push(c),
|
|
1968
|
+
for (const u of o) {
|
|
1969
|
+
const c = raycast(n, e, u - 0.001),
|
|
1970
|
+
s = raycast(n, e, u + 0.001)
|
|
1971
|
+
c && i.push(c), s && i.push(s)
|
|
1970
1972
|
}
|
|
1971
1973
|
return i
|
|
1972
1974
|
}
|