cafe-utility 23.11.0 → 24.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.
Files changed (3) hide show
  1. package/index.d.ts +14 -5
  2. package/index.js +162 -144
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -449,10 +449,6 @@ declare function pushToBidirectionalMap<K, T>(object: BidirectionalMap<K, T>, ke
449
449
  declare function unshiftToBidirectionalMap<K, T>(object: BidirectionalMap<K, T>, key: K, item: T, limit?: number): void;
450
450
  declare function addToTemporalBidirectionalMap<K, T>(object: BidirectionalMap<K, TemporalData<T>>, key: K, item: T, expiration: number, limit?: number): void;
451
451
  declare function getFromTemporalBidirectionalMap<K, T>(object: BidirectionalMap<K, TemporalData<T>>, key: K): T | null;
452
- declare function makeAsyncQueue(concurrency?: number): {
453
- enqueue(fn: () => Promise<void>): void;
454
- drain: () => Promise<void>;
455
- };
456
452
  export declare class Optional<T> {
457
453
  value: T | null | undefined;
458
454
  private constructor();
@@ -568,6 +564,20 @@ export declare class PubSubChannel<T> {
568
564
  private subscribers;
569
565
  subscribe(callback: (data: T) => void): () => void;
570
566
  publish(data: T): void;
567
+ clear(): void;
568
+ getSubscriberCount(): number;
569
+ }
570
+ export declare class AsyncQueue {
571
+ queue: (() => Promise<void>)[];
572
+ concurrency: number;
573
+ capacity: number;
574
+ running: number;
575
+ onProcessed: PubSubChannel<void>;
576
+ onDrained: PubSubChannel<void>;
577
+ constructor(concurrency: number, capacity: number);
578
+ private process;
579
+ enqueue(fn: () => Promise<void>): Promise<void>;
580
+ drain(): Promise<void>;
571
581
  }
572
582
  export declare const Binary: {
573
583
  hexToUint8Array: typeof hexToUint8Array;
@@ -687,7 +697,6 @@ export declare const Promises: {
687
697
  raceFulfilled: typeof raceFulfilled;
688
698
  invert: typeof invertPromise;
689
699
  runInParallelBatches: typeof runInParallelBatches;
690
- makeAsyncQueue: typeof makeAsyncQueue;
691
700
  };
692
701
  export declare const Dates: {
693
702
  getTimestamp: typeof getTimestamp;
package/index.js CHANGED
@@ -13,6 +13,7 @@ Object.defineProperty(exports, '__esModule', { value: !0 }),
13
13
  exports.Arrays =
14
14
  exports.Random =
15
15
  exports.Binary =
16
+ exports.AsyncQueue =
16
17
  exports.PubSubChannel =
17
18
  exports.AsyncLazy =
18
19
  exports.Lazy =
@@ -145,13 +146,13 @@ function pickManyUnique(n, e, t, r = Math.random) {
145
146
  return o
146
147
  }
147
148
  function pickGuaranteed(n, e, t, r, o, i = Math.random) {
148
- const u = n.filter(c => c !== e && c !== t),
149
- s = []
150
- for (e !== null && s.push(e); u.length && s.length < r; ) {
151
- const c = exports.Random.intBetween(0, u.length - 1, i)
152
- o(u[c], s) && s.push(u[c]), u.splice(c, 1)
149
+ const u = n.filter(s => s !== e && s !== t),
150
+ c = []
151
+ for (e !== null && c.push(e); u.length && c.length < r; ) {
152
+ const s = exports.Random.intBetween(0, u.length - 1, i)
153
+ o(u[s], c) && c.push(u[s]), u.splice(s, 1)
153
154
  }
154
- return shuffle(s, i), { values: s, indexOfGuaranteed: e !== null ? s.indexOf(e) : -1 }
155
+ return shuffle(c, i), { values: c, indexOfGuaranteed: e !== null ? c.indexOf(e) : -1 }
155
156
  }
156
157
  function last(n) {
157
158
  if (!n.length) throw Error('Received empty array')
@@ -191,10 +192,10 @@ function setDeep(n, e, t) {
191
192
  let o = n
192
193
  for (let i = 0; i < r.length; i++) {
193
194
  const u = r[i],
194
- s = i < r.length - 1 && r[i + 1].includes(']'),
195
+ c = i < r.length - 1 && r[i + 1].includes(']'),
195
196
  f = u.includes(']') ? u.replace(/\[|\]/g, '') : u
196
197
  if (i === r.length - 1) return (o[f] = t), t
197
- isObject(o[f]) || (s ? (o[f] = []) : (o[f] = {})), (o = o[f])
198
+ isObject(o[f]) || (c ? (o[f] = []) : (o[f] = {})), (o = o[f])
198
199
  }
199
200
  return t
200
201
  }
@@ -268,9 +269,9 @@ function rgbToHex(n) {
268
269
  function haversineDistanceToMeters(n, e, t, r) {
269
270
  const i = (n * Math.PI) / 180,
270
271
  u = (t * Math.PI) / 180,
271
- s = ((t - n) * Math.PI) / 180,
272
- c = ((r - e) * Math.PI) / 180,
273
- f = Math.sin(s / 2) * Math.sin(s / 2) + Math.cos(i) * Math.cos(u) * Math.sin(c / 2) * Math.sin(c / 2)
272
+ c = ((t - n) * Math.PI) / 180,
273
+ s = ((r - e) * Math.PI) / 180,
274
+ f = Math.sin(c / 2) * Math.sin(c / 2) + Math.cos(i) * Math.cos(u) * Math.sin(s / 2) * Math.sin(s / 2)
274
275
  return 6371e3 * (2 * Math.atan2(Math.sqrt(f), Math.sqrt(1 - f)))
275
276
  }
276
277
  function roundToNearest(n, e) {
@@ -806,9 +807,9 @@ function expand(n) {
806
807
  o = n.slice(0, t.index),
807
808
  i = n.slice(t.index + t[0].length)
808
809
  let u = []
809
- for (const s of r) {
810
- const c = expand(o + s + i)
811
- u = u.concat(c)
810
+ for (const c of r) {
811
+ const s = expand(o + c + i)
812
+ u = u.concat(s)
812
813
  }
813
814
  return u
814
815
  }
@@ -1052,11 +1053,11 @@ function baseToUint8Array(n, e) {
1052
1053
  let o = 0,
1053
1054
  i = 0
1054
1055
  const u = []
1055
- for (let s = 0; s < n.length; s++) {
1056
- const c = n[s]
1057
- if (c === t) break
1058
- const f = e.indexOf(c)
1059
- if (f === -1) throw new Error(`Invalid character: ${c}`)
1056
+ for (let c = 0; c < n.length; c++) {
1057
+ const s = n[c]
1058
+ if (s === t) break
1059
+ const f = e.indexOf(s)
1060
+ if (f === -1) throw new Error(`Invalid character: ${s}`)
1060
1061
  ;(i = (i << Math.log2(r)) | f), (o += Math.log2(r)), o >= 8 && ((o -= 8), u.push((i >> o) & 255))
1061
1062
  }
1062
1063
  return new Uint8Array(u)
@@ -1110,7 +1111,7 @@ function generateVariants(n, e, t, r = Math.random) {
1110
1111
  const o = exports.Arrays.shuffle(
1111
1112
  e.map(u => ({
1112
1113
  variants: exports.Arrays.shuffle(
1113
- u.variants.map(s => s),
1114
+ u.variants.map(c => c),
1114
1115
  r
1115
1116
  ),
1116
1117
  avoid: u.avoid
@@ -1119,14 +1120,14 @@ function generateVariants(n, e, t, r = Math.random) {
1119
1120
  ),
1120
1121
  i = []
1121
1122
  for (const u of o) {
1122
- const s = u.variants.filter(f => f !== u.avoid),
1123
- c = s.find(f => n.includes(f))
1124
- if (c && (pushAll(i, explodeReplace(n, c, s)), i.length >= t)) break
1123
+ const c = u.variants.filter(f => f !== u.avoid),
1124
+ s = c.find(f => n.includes(f))
1125
+ if (s && (pushAll(i, explodeReplace(n, s, c)), i.length >= t)) break
1125
1126
  }
1126
1127
  if (i.length < t)
1127
1128
  for (const u of o) {
1128
- const s = u.variants.find(c => n.includes(c))
1129
- if (s && (pushAll(i, explodeReplace(n, s, u.variants)), i.length >= t)) break
1129
+ const c = u.variants.find(s => n.includes(s))
1130
+ if (c && (pushAll(i, explodeReplace(n, c, u.variants)), i.length >= t)) break
1130
1131
  }
1131
1132
  return i.slice(0, t)
1132
1133
  }
@@ -1161,9 +1162,9 @@ function toLines(n, e, t = {}) {
1161
1162
  let o = '',
1162
1163
  i = 0
1163
1164
  for (let u = 0; u < n.length; u++) {
1164
- const s = n[u],
1165
- c = t[s] || 1
1166
- if (((o += s), (i += c), i > e)) {
1165
+ const c = n[u],
1166
+ s = t[c] || 1
1167
+ if (((o += c), (i += s), i > e)) {
1167
1168
  const { line: f, rest: l } = breakLine(o)
1168
1169
  r.push(f),
1169
1170
  (o = l),
@@ -1218,8 +1219,8 @@ function resolveVariableWithDefaultSyntax(n, e, t, r = '$', o = ':') {
1218
1219
  if (n[i + e.length + 1] === o)
1219
1220
  if (n[i + e.length + 2] === o) n = n.replace(`${r}${e}${o}${o}`, t)
1220
1221
  else {
1221
- const s = readNextWord(n, i + e.length + 2, ['_'])
1222
- n = n.replace(`${r}${e}${o}${s}`, t)
1222
+ const c = readNextWord(n, i + e.length + 2, ['_'])
1223
+ n = n.replace(`${r}${e}${o}${c}`, t)
1223
1224
  }
1224
1225
  else n = n.replace(`${r}${e}`, t)
1225
1226
  i = n.indexOf(`${r}${e}`, i + t.length)
@@ -1247,8 +1248,8 @@ function resolveMarkdownLinks(n, e) {
1247
1248
  o = n.indexOf(')', t)
1248
1249
  if (r !== -1 && o !== -1) {
1249
1250
  const [i, u] = n.slice(r + 1, o).split(']('),
1250
- s = e(i, u)
1251
- n = n.slice(0, r) + s + n.slice(o + 1)
1251
+ c = e(i, u)
1252
+ n = n.slice(0, r) + c + n.slice(o + 1)
1252
1253
  }
1253
1254
  t = n.indexOf('](', t + 1)
1254
1255
  }
@@ -1286,8 +1287,8 @@ function reposition(n, e, t, r) {
1286
1287
  const o = n.find(u => u[e] === t),
1287
1288
  i = n.find(u => u[e] === t + r)
1288
1289
  o && i ? ((o[e] = t + r), (i[e] = t)) : o && (o[e] = t + r),
1289
- n.sort((u, s) => asNumber(u[e]) - asNumber(s[e])),
1290
- n.forEach((u, s) => (u[e] = s + 1))
1290
+ n.sort((u, c) => asNumber(u[e]) - asNumber(c[e])),
1291
+ n.forEach((u, c) => (u[e] = c + 1))
1291
1292
  }
1292
1293
  function unwrapSingleKey(n) {
1293
1294
  const e = Object.keys(n)
@@ -1310,7 +1311,7 @@ function parseCsv(n, e = ',', t = '"') {
1310
1311
  let o = '',
1311
1312
  i = !1
1312
1313
  const u = n.split('')
1313
- for (const s of u) s === e && !i ? (r.push(o), (o = '')) : s === t && ((!o && !i) || i) ? (i = !i) : (o += s)
1314
+ for (const c of u) c === e && !i ? (r.push(o), (o = '')) : c === t && ((!o && !i) || i) ? (i = !i) : (o += c)
1314
1315
  return r.push(o), r
1315
1316
  }
1316
1317
  function humanizeProgress(n) {
@@ -1428,12 +1429,12 @@ function secondsToHumanTime(n, e = DefaultTimeDeltaLabels) {
1428
1429
  }
1429
1430
  function countCycles(n, e, t) {
1430
1431
  var r, o, i
1431
- const s = ((r = t?.now) !== null && r !== void 0 ? r : Date.now()) - n,
1432
- c = Math.floor(s / e),
1432
+ const c = ((r = t?.now) !== null && r !== void 0 ? r : Date.now()) - n,
1433
+ s = Math.floor(c / e),
1433
1434
  f =
1434
1435
  e / ((o = t?.precision) !== null && o !== void 0 ? o : 1) -
1435
- Math.ceil((s % e) / ((i = t?.precision) !== null && i !== void 0 ? i : 1))
1436
- return { cycles: c, remaining: f }
1436
+ Math.ceil((c % e) / ((i = t?.precision) !== null && i !== void 0 ? i : 1))
1437
+ return { cycles: s, remaining: f }
1437
1438
  }
1438
1439
  const throttleTimers = {}
1439
1440
  function throttle(n, e) {
@@ -1450,9 +1451,9 @@ function getProgress(n, e, t, r) {
1450
1451
  const o = e / t,
1451
1452
  i = r - n,
1452
1453
  u = i / e,
1453
- s = u * t,
1454
- c = s - i
1455
- return { deltaMs: i, progress: o, baseTimeMs: u, totalTimeMs: s, remainingTimeMs: c }
1454
+ c = u * t,
1455
+ s = c - i
1456
+ return { deltaMs: i, progress: o, baseTimeMs: u, totalTimeMs: c, remainingTimeMs: s }
1456
1457
  }
1457
1458
  const dayNumberIndex = {
1458
1459
  0: 'sunday',
@@ -1620,9 +1621,9 @@ function organiseWithLimits(n, e, t, r, o) {
1620
1621
  for (const u of Object.keys(e)) i[u] = []
1621
1622
  ;(i[r] = []), o && (n = n.sort(o))
1622
1623
  for (const u of n) {
1623
- const s = u[t],
1624
- c = e[s] ? s : r
1625
- i[c].length >= e[c] ? i[r].push(u) : i[c].push(u)
1624
+ const c = u[t],
1625
+ s = e[c] ? c : r
1626
+ i[s].length >= e[s] ? i[r].push(u) : i[s].push(u)
1626
1627
  }
1627
1628
  return i
1628
1629
  }
@@ -1691,11 +1692,11 @@ function formatNumber(n, e) {
1691
1692
  const o = (t = e?.longForm) !== null && t !== void 0 ? t : !1,
1692
1693
  i = e?.unit ? ` ${e.unit}` : '',
1693
1694
  u = o ? longNumberUnits : shortNumberUnits,
1694
- s = (r = e?.precision) !== null && r !== void 0 ? r : 1
1695
+ c = (r = e?.precision) !== null && r !== void 0 ? r : 1
1695
1696
  if (n < thresholds[0]) return `${n}${i}`
1696
- for (let c = 0; c < thresholds.length - 1; c++)
1697
- if (n < thresholds[c + 1]) return `${(n / thresholds[c]).toFixed(s)}${o ? ' ' : ''}${u[c]}${i}`
1698
- return `${(n / thresholds[thresholds.length - 1]).toFixed(s)}${o ? ' ' : ''}${u[thresholds.length - 1]}${i}`
1697
+ for (let s = 0; s < thresholds.length - 1; s++)
1698
+ if (n < thresholds[s + 1]) return `${(n / thresholds[s]).toFixed(c)}${o ? ' ' : ''}${u[s]}${i}`
1699
+ return `${(n / thresholds[thresholds.length - 1]).toFixed(c)}${o ? ' ' : ''}${u[thresholds.length - 1]}${i}`
1699
1700
  }
1700
1701
  function makeNumber(n) {
1701
1702
  const e = parseFloat(n)
@@ -1774,20 +1775,20 @@ function flip(n) {
1774
1775
  }
1775
1776
  function getAllPermutations(n) {
1776
1777
  const e = Object.keys(n),
1777
- t = e.map(s => n[s].length),
1778
- r = t.reduce((s, c) => (s *= c))
1778
+ t = e.map(c => n[c].length),
1779
+ r = t.reduce((c, s) => (c *= s))
1779
1780
  let o = 1
1780
1781
  const i = [1]
1781
- for (let s = 0; s < t.length - 1; s++) (o *= t[s]), i.push(o)
1782
+ for (let c = 0; c < t.length - 1; c++) (o *= t[c]), i.push(o)
1782
1783
  const u = []
1783
- for (let s = 0; s < r; s++) {
1784
- const c = {}
1784
+ for (let c = 0; c < r; c++) {
1785
+ const s = {}
1785
1786
  for (let f = 0; f < e.length; f++) {
1786
1787
  const l = n[e[f]],
1787
- a = Math.floor(s / i[f]) % l.length
1788
- c[e[f]] = l[a]
1788
+ a = Math.floor(c / i[f]) % l.length
1789
+ s[e[f]] = l[a]
1789
1790
  }
1790
- u.push(c)
1791
+ u.push(s)
1791
1792
  }
1792
1793
  return u
1793
1794
  }
@@ -1800,14 +1801,14 @@ function getFlatNotation(n, e, t) {
1800
1801
  function flattenInner(n, e, t, r, o) {
1801
1802
  if (!isObject(e)) return e
1802
1803
  for (const [i, u] of Object.entries(e)) {
1803
- const s = getFlatNotation(t, i, r)
1804
+ const c = getFlatNotation(t, i, r)
1804
1805
  Array.isArray(u)
1805
1806
  ? o
1806
- ? flattenInner(n, u, s, !0, o)
1807
- : (n[s] = u.map(c => flattenInner(Array.isArray(c) ? [] : {}, c, '', !1, o)))
1807
+ ? flattenInner(n, u, c, !0, o)
1808
+ : (n[c] = u.map(s => flattenInner(Array.isArray(s) ? [] : {}, s, '', !1, o)))
1808
1809
  : isObject(u)
1809
- ? flattenInner(n, u, s, !1, o)
1810
- : (n[s] = u)
1810
+ ? flattenInner(n, u, c, !1, o)
1811
+ : (n[c] = u)
1811
1812
  }
1812
1813
  return n
1813
1814
  }
@@ -1873,9 +1874,9 @@ function makeUnique(n, e) {
1873
1874
  function countUnique(n, e, t, r, o) {
1874
1875
  const i = e ? n.map(e) : n,
1875
1876
  u = {}
1876
- for (const c of i) u[c] = (u[c] || 0) + 1
1877
- const s = r ? sortObjectValues(u, o ? (c, f) => c[1] - f[1] : (c, f) => f[1] - c[1]) : u
1878
- return t ? Object.keys(s) : s
1877
+ for (const s of i) u[s] = (u[s] || 0) + 1
1878
+ const c = r ? sortObjectValues(u, o ? (s, f) => s[1] - f[1] : (s, f) => f[1] - s[1]) : u
1879
+ return t ? Object.keys(c) : c
1879
1880
  }
1880
1881
  function sortObjectValues(n, e) {
1881
1882
  return Object.fromEntries(Object.entries(n).sort(e))
@@ -1940,36 +1941,6 @@ function getFromTemporalBidirectionalMap(n, e) {
1940
1941
  const t = n.map.get(e)
1941
1942
  return t && t.validUntil > Date.now() ? t.data : null
1942
1943
  }
1943
- function makeAsyncQueue(n = 1) {
1944
- const e = [],
1945
- t = []
1946
- let r = 0
1947
- async function o() {
1948
- if (e.length > 0 && r < n) {
1949
- r++
1950
- const u = e.shift()
1951
- try {
1952
- u && (await u())
1953
- } finally {
1954
- if (--r === 0) for (; t.length > 0; ) t.shift()()
1955
- o()
1956
- }
1957
- }
1958
- }
1959
- async function i() {
1960
- return r
1961
- ? new Promise(u => {
1962
- t.push(u)
1963
- })
1964
- : Promise.resolve()
1965
- }
1966
- return {
1967
- enqueue(u) {
1968
- e.push(u), o()
1969
- },
1970
- drain: i
1971
- }
1972
- }
1973
1944
  class Optional {
1974
1945
  constructor(e) {
1975
1946
  this.value = e
@@ -2040,28 +2011,28 @@ class Node {
2040
2011
  function createHierarchy(n, e, t, r, o = !1) {
2041
2012
  const i = new Map(),
2042
2013
  u = []
2043
- n.forEach(c => {
2044
- const f = new Node(c)
2045
- i.set(c[e], f)
2014
+ n.forEach(s => {
2015
+ const f = new Node(s)
2016
+ i.set(s[e], f)
2046
2017
  }),
2047
- n.forEach(c => {
2048
- const f = i.get(c[e])
2018
+ n.forEach(s => {
2019
+ const f = i.get(s[e])
2049
2020
  if (!f) return
2050
- const l = c[t]
2021
+ const l = s[t]
2051
2022
  if (l) {
2052
2023
  const a = i.get(l)
2053
2024
  a && a.children.push(f)
2054
2025
  } else u.push(f)
2055
2026
  })
2056
- const s = c => {
2057
- c.children.sort((f, l) => {
2027
+ const c = s => {
2028
+ s.children.sort((f, l) => {
2058
2029
  const a = f.value[r],
2059
2030
  h = l.value[r]
2060
2031
  return o ? h - a : a - h
2061
2032
  }),
2062
- c.children.forEach(s)
2033
+ s.children.forEach(c)
2063
2034
  }
2064
- return u.forEach(s), u
2035
+ return u.forEach(c), u
2065
2036
  }
2066
2037
  function log2Reduce(n, e) {
2067
2038
  if (Math.log2(n.length) % 1 !== 0) throw new Error('Array length must be a power of 2')
@@ -2188,7 +2159,7 @@ function tickPlaybook(n) {
2188
2159
  )
2189
2160
  }
2190
2161
  function getArgument(n, e, t, r) {
2191
- const o = n.findIndex(s => s === `--${e}` || s.startsWith(`--${e}=`)),
2162
+ const o = n.findIndex(c => c === `--${e}` || c.startsWith(`--${e}=`)),
2192
2163
  i = n[o]
2193
2164
  if (!i) return (t || {})[r || e || ''] || null
2194
2165
  if (i.includes('=')) return i.split('=')[1]
@@ -2205,14 +2176,14 @@ function getNumberArgument(n, e, t, r) {
2205
2176
  }
2206
2177
  }
2207
2178
  function getBooleanArgument(n, e, t, r) {
2208
- const o = n.some(c => c.endsWith('-' + e)),
2179
+ const o = n.some(s => s.endsWith('-' + e)),
2209
2180
  i = getArgument(n, e, t, r)
2210
2181
  if (!i && o) return !0
2211
2182
  if (!i && !o) return null
2212
2183
  const u = ['true', '1', 'yes', 'y', 'on'],
2213
- s = ['false', '0', 'no', 'n', 'off']
2184
+ c = ['false', '0', 'no', 'n', 'off']
2214
2185
  if (u.includes(i.toLowerCase())) return !0
2215
- if (s.includes(i.toLowerCase())) return !1
2186
+ if (c.includes(i.toLowerCase())) return !1
2216
2187
  throw Error(`Invalid boolean argument ${e}: ${i}`)
2217
2188
  }
2218
2189
  function requireStringArgument(n, e, t, r) {
@@ -2295,16 +2266,16 @@ function isBottommost(n, e, t) {
2295
2266
  return !n[e][t + 1]
2296
2267
  }
2297
2268
  function getCorners(n, e, t) {
2298
- var r, o, i, u, s, c, f, l
2269
+ var r, o, i, u, c, s, f, l
2299
2270
  const a = []
2300
2271
  return n[e][t]
2301
2272
  ? isHorizontalLine(n, e, t) || isVerticalLine(n, e, t)
2302
2273
  ? []
2303
- : (!(!((s = n[e - 1]) === null || s === void 0) && s[t - 1]) &&
2274
+ : (!(!((c = n[e - 1]) === null || c === void 0) && c[t - 1]) &&
2304
2275
  isLeftmost(n, e, t) &&
2305
2276
  isTopmost(n, e, t) &&
2306
2277
  a.push({ x: e, y: t }),
2307
- !(!((c = n[e + 1]) === null || c === void 0) && c[t - 1]) &&
2278
+ !(!((s = n[e + 1]) === null || s === void 0) && s[t - 1]) &&
2308
2279
  isRightmost(n, e, t) &&
2309
2280
  isTopmost(n, e, t) &&
2310
2281
  a.push({ x: e + 1, y: t }),
@@ -2332,42 +2303,42 @@ function findCorners(n, e, t, r) {
2332
2303
  ]
2333
2304
  for (let i = 0; i < n.length; i++)
2334
2305
  for (let u = 0; u < n[0].length; u++) {
2335
- const s = getCorners(n, i, u)
2336
- for (const c of s) o.some(f => f.x === c.x && f.y === c.y) || o.push(c)
2306
+ const c = getCorners(n, i, u)
2307
+ for (const s of c) o.some(f => f.x === s.x && f.y === s.y) || o.push(s)
2337
2308
  }
2338
2309
  return o.map(i => ({ x: i.x * e, y: i.y * e }))
2339
2310
  }
2340
2311
  function findLines(n, e) {
2341
- const t = filterCoordinates(n, (c, f) => n[c][f] === 0 && n[c][f + 1] !== 0, 'row-first').map(c =>
2342
- Object.assign(Object.assign({}, c), { dx: 1, dy: 0 })
2312
+ const t = filterCoordinates(n, (s, f) => n[s][f] === 0 && n[s][f + 1] !== 0, 'row-first').map(s =>
2313
+ Object.assign(Object.assign({}, s), { dx: 1, dy: 0 })
2343
2314
  ),
2344
- r = filterCoordinates(n, (c, f) => n[c][f] === 0 && n[c][f - 1] !== 0, 'row-first').map(c =>
2345
- Object.assign(Object.assign({}, c), { dx: 1, dy: 0 })
2315
+ r = filterCoordinates(n, (s, f) => n[s][f] === 0 && n[s][f - 1] !== 0, 'row-first').map(s =>
2316
+ Object.assign(Object.assign({}, s), { dx: 1, dy: 0 })
2346
2317
  ),
2347
2318
  o = filterCoordinates(
2348
2319
  n,
2349
- (c, f) => {
2320
+ (s, f) => {
2350
2321
  var l
2351
- return n[c][f] === 0 && ((l = n[c - 1]) === null || l === void 0 ? void 0 : l[f]) !== 0
2322
+ return n[s][f] === 0 && ((l = n[s - 1]) === null || l === void 0 ? void 0 : l[f]) !== 0
2352
2323
  },
2353
2324
  'column-first'
2354
- ).map(c => Object.assign(Object.assign({}, c), { dx: 0, dy: 1 })),
2325
+ ).map(s => Object.assign(Object.assign({}, s), { dx: 0, dy: 1 })),
2355
2326
  i = filterCoordinates(
2356
2327
  n,
2357
- (c, f) => {
2328
+ (s, f) => {
2358
2329
  var l
2359
- return n[c][f] === 0 && ((l = n[c + 1]) === null || l === void 0 ? void 0 : l[f]) !== 0
2330
+ return n[s][f] === 0 && ((l = n[s + 1]) === null || l === void 0 ? void 0 : l[f]) !== 0
2360
2331
  },
2361
2332
  'column-first'
2362
- ).map(c => Object.assign(Object.assign({}, c), { dx: 0, dy: 1 }))
2363
- t.forEach(c => c.y++), i.forEach(c => c.x++)
2364
- const u = group([...o, ...i], (c, f) => c.x === f.x && c.y - 1 === f.y),
2365
- s = group([...r, ...t], (c, f) => c.y === f.y && c.x - 1 === f.x)
2366
- return [...u, ...s]
2367
- .map(c => ({ start: c[0], end: last(c) }))
2368
- .map(c => ({
2369
- start: multiplyPoint(c.start, e),
2370
- end: multiplyPoint(addPoint(c.end, { x: c.start.dx, y: c.start.dy }), e)
2333
+ ).map(s => Object.assign(Object.assign({}, s), { dx: 0, dy: 1 }))
2334
+ t.forEach(s => s.y++), i.forEach(s => s.x++)
2335
+ const u = group([...o, ...i], (s, f) => s.x === f.x && s.y - 1 === f.y),
2336
+ c = group([...r, ...t], (s, f) => s.y === f.y && s.x - 1 === f.x)
2337
+ return [...u, ...c]
2338
+ .map(s => ({ start: s[0], end: last(s) }))
2339
+ .map(s => ({
2340
+ start: multiplyPoint(s.start, e),
2341
+ end: multiplyPoint(addPoint(s.end, { x: s.start.dx, y: s.start.dy }), e)
2371
2342
  }))
2372
2343
  }
2373
2344
  function getAngleInRadians(n, e) {
@@ -2381,11 +2352,11 @@ function getLineIntersectionPoint(n, e, t, r) {
2381
2352
  if (o === 0) return null
2382
2353
  let i = n.y - t.y,
2383
2354
  u = n.x - t.x
2384
- const s = (r.x - t.x) * i - (r.y - t.y) * u,
2385
- c = (e.x - n.x) * i - (e.y - n.y) * u
2355
+ const c = (r.x - t.x) * i - (r.y - t.y) * u,
2356
+ s = (e.x - n.x) * i - (e.y - n.y) * u
2386
2357
  return (
2387
- (i = s / o),
2388
- (u = c / o),
2358
+ (i = c / o),
2359
+ (u = s / o),
2389
2360
  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
2390
2361
  )
2391
2362
  }
@@ -2398,9 +2369,9 @@ function raycast(n, e, t) {
2398
2369
  }
2399
2370
  return r.length
2400
2371
  ? r.reduce((i, u) => {
2401
- const s = getDistanceBetweenPoints(n, u),
2402
- c = getDistanceBetweenPoints(n, i)
2403
- return s < c ? u : i
2372
+ const c = getDistanceBetweenPoints(n, u),
2373
+ s = getDistanceBetweenPoints(n, i)
2374
+ return c < s ? u : i
2404
2375
  })
2405
2376
  : null
2406
2377
  }
@@ -2408,9 +2379,9 @@ function raycastCircle(n, e, t) {
2408
2379
  const o = getSortedRayAngles(n, t),
2409
2380
  i = []
2410
2381
  for (const u of o) {
2411
- const s = raycast(n, e, u - 0.001),
2412
- c = raycast(n, e, u + 0.001)
2413
- s && i.push(s), c && i.push(c)
2382
+ const c = raycast(n, e, u - 0.001),
2383
+ s = raycast(n, e, u + 0.001)
2384
+ c && i.push(c), s && i.push(s)
2414
2385
  }
2415
2386
  return i
2416
2387
  }
@@ -2429,8 +2400,55 @@ class PubSubChannel {
2429
2400
  publish(e) {
2430
2401
  this.subscribers.forEach(t => t(e))
2431
2402
  }
2403
+ clear() {
2404
+ this.subscribers = []
2405
+ }
2406
+ getSubscriberCount() {
2407
+ return this.subscribers.length
2408
+ }
2409
+ }
2410
+ exports.PubSubChannel = PubSubChannel
2411
+ class AsyncQueue {
2412
+ constructor(e, t) {
2413
+ ;(this.queue = []),
2414
+ (this.running = 0),
2415
+ (this.onProcessed = new PubSubChannel()),
2416
+ (this.onDrained = new PubSubChannel()),
2417
+ (this.concurrency = e),
2418
+ (this.capacity = t)
2419
+ }
2420
+ process() {
2421
+ if (this.running >= this.concurrency) return
2422
+ const e = this.queue.shift()
2423
+ e &&
2424
+ (this.running++,
2425
+ e().finally(() => {
2426
+ this.running--,
2427
+ this.process(),
2428
+ this.onProcessed.publish(),
2429
+ this.running === 0 && this.onDrained.publish()
2430
+ }))
2431
+ }
2432
+ enqueue(e) {
2433
+ if (this.queue.length < this.capacity) return this.queue.push(e), this.process(), Promise.resolve()
2434
+ if (this.onProcessed.getSubscriberCount()) throw Error('Queue capacity is full')
2435
+ return new Promise(t => {
2436
+ this.onProcessed.subscribe(() => {
2437
+ this.queue.length < this.capacity && (this.queue.push(e), this.process(), this.onProcessed.clear(), t())
2438
+ })
2439
+ })
2440
+ }
2441
+ drain() {
2442
+ if (this.running === 0) return Promise.resolve()
2443
+ if (this.onDrained.getSubscriberCount()) throw Error('Already draining')
2444
+ return new Promise(e => {
2445
+ this.onDrained.subscribe(() => {
2446
+ this.onDrained.clear(), e()
2447
+ })
2448
+ })
2449
+ }
2432
2450
  }
2433
- ;(exports.PubSubChannel = PubSubChannel),
2451
+ ;(exports.AsyncQueue = AsyncQueue),
2434
2452
  (exports.Binary = {
2435
2453
  hexToUint8Array,
2436
2454
  uint8ArrayToHex,
@@ -2538,7 +2556,7 @@ class PubSubChannel {
2538
2556
  triangularNumber,
2539
2557
  searchFloat
2540
2558
  }),
2541
- (exports.Promises = { raceFulfilled, invert: invertPromise, runInParallelBatches, makeAsyncQueue }),
2559
+ (exports.Promises = { raceFulfilled, invert: invertPromise, runInParallelBatches }),
2542
2560
  (exports.Dates = {
2543
2561
  getTimestamp,
2544
2562
  getTimeDelta,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "23.11.0",
3
+ "version": "24.0.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {