cafe-utility 33.9.0 → 33.11.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 +21 -1
  2. package/index.js +32 -11
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -466,8 +466,26 @@ declare function days(value: number): number
466
466
  declare function makeDate(numberWithUnit: string): number
467
467
  declare function makeStorage(numberWithUnit: string, conversionMultiplier?: number): number
468
468
  declare function getPreLine(string: string): string
469
- declare function getCached<T>(key: string, ttlMillis: number, handler: () => Promise<T>): Promise<T>
469
+ declare function getCached<T>(
470
+ key: string,
471
+ ttlMillis: number,
472
+ handler: () => Promise<T>,
473
+ callbacks?: {
474
+ onMiss?: () => void
475
+ onFailure?: (err: unknown) => void
476
+ }
477
+ ): Promise<T>
478
+ declare function getCachedDeferred<T>(
479
+ key: string,
480
+ ttlMillis: number,
481
+ handler: () => Promise<T>,
482
+ callbacks?: {
483
+ onMiss?: () => void
484
+ onFailure?: (err: unknown) => void
485
+ }
486
+ ): Promise<T>
470
487
  declare function deleteFromCache(key: string): void
488
+ declare function clearCache(): void
471
489
  declare function deleteExpiredFromCache(): void
472
490
  declare function cacheSize(): number
473
491
  declare function joinUrl(parts: unknown[], relativeToFile?: boolean): string
@@ -1203,9 +1221,11 @@ export declare const Assertions: {
1203
1221
  }
1204
1222
  export declare const Cache: {
1205
1223
  get: typeof getCached
1224
+ getDeferred: typeof getCachedDeferred
1206
1225
  delete: typeof deleteFromCache
1207
1226
  deleteExpired: typeof deleteExpiredFromCache
1208
1227
  size: typeof cacheSize
1228
+ clear: typeof clearCache
1209
1229
  }
1210
1230
  export declare const Vector: {
1211
1231
  addPoint: typeof addPoint
package/index.js CHANGED
@@ -1478,17 +1478,38 @@ function getPreLine(n) {
1478
1478
  return n.replace(/ +/g, ' ').replace(/^ /gm, '')
1479
1479
  }
1480
1480
  const tinyCache = new Map()
1481
- async function getCached(n, e, t) {
1482
- const r = Date.now(),
1483
- i = tinyCache.get(n)
1484
- if (i && i.validUntil > r) return i.value
1485
- const o = await t(),
1486
- u = r + e
1487
- return tinyCache.set(n, { value: o, validUntil: u }), o
1481
+ async function getCached(n, e, t, r) {
1482
+ const i = Date.now(),
1483
+ o = tinyCache.get(n)
1484
+ if (o && o.validUntil > i) return o.value
1485
+ r?.onMiss?.()
1486
+ try {
1487
+ const u = await t()
1488
+ return tinyCache.set(n, { value: u, validUntil: i + e }), u
1489
+ } catch (u) {
1490
+ throw (r?.onFailure?.(u), u)
1491
+ }
1492
+ }
1493
+ function getCachedDeferred(n, e, t, r) {
1494
+ const i = Date.now(),
1495
+ o = tinyCache.get(n)
1496
+ if (o && o.validUntil > i) return o.value
1497
+ r?.onMiss?.()
1498
+ const u = t()
1499
+ return (
1500
+ tinyCache.set(n, { value: u, validUntil: i + e }),
1501
+ u.catch(s => {
1502
+ tinyCache.delete(n), r?.onFailure?.(s)
1503
+ }),
1504
+ u
1505
+ )
1488
1506
  }
1489
1507
  function deleteFromCache(n) {
1490
1508
  tinyCache.delete(n)
1491
1509
  }
1510
+ function clearCache() {
1511
+ tinyCache.clear()
1512
+ }
1492
1513
  function deleteExpiredFromCache() {
1493
1514
  const n = Date.now()
1494
1515
  for (const [e, t] of tinyCache.entries()) t.validUntil <= n && tinyCache.delete(e)
@@ -2140,8 +2161,8 @@ function keccakPermutate(n) {
2140
2161
  B = (n[11] << 4) | (n[10] >>> 28),
2141
2162
  P = (n[10] << 4) | (n[11] >>> 28),
2142
2163
  v = (n[13] << 12) | (n[12] >>> 20),
2143
- L = (n[12] << 12) | (n[13] >>> 20),
2144
- U = (n[14] << 6) | (n[15] >>> 26),
2164
+ U = (n[12] << 12) | (n[13] >>> 20),
2165
+ L = (n[14] << 6) | (n[15] >>> 26),
2145
2166
  N = (n[15] << 6) | (n[14] >>> 26),
2146
2167
  j = (n[17] << 23) | (n[16] >>> 9),
2147
2168
  F = (n[16] << 23) | (n[17] >>> 9),
@@ -2177,7 +2198,7 @@ function keccakPermutate(n) {
2177
2198
  wn = (n[46] << 24) | (n[47] >>> 8),
2178
2199
  xn = (n[48] << 14) | (n[49] >>> 18),
2179
2200
  yn = (n[49] << 14) | (n[48] >>> 18)
2180
- ;(n[0] = A ^ (~v & K)), (n[1] = M ^ (~L & Z)), (n[2] = v ^ (~K & un)), (n[3] = L ^ (~Z & cn)), (n[4] = K ^ (~un & xn)), (n[5] = Z ^ (~cn & yn)), (n[6] = un ^ (~xn & A)), (n[7] = cn ^ (~yn & M)), (n[8] = xn ^ (~A & v)), (n[9] = yn ^ (~M & L)), (n[10] = C ^ (~z & W)), (n[11] = R ^ (~q & H)), (n[12] = z ^ (~W & en)), (n[13] = q ^ (~H & tn)), (n[14] = W ^ (~en & pn)), (n[15] = H ^ (~tn & mn)), (n[16] = en ^ (~pn & C)), (n[17] = tn ^ (~mn & R)), (n[18] = pn ^ (~C & z)), (n[19] = mn ^ (~R & q)), (n[20] = k ^ (~U & Q)), (n[21] = S ^ (~N & G)), (n[22] = U ^ (~Q & sn)), (n[23] = N ^ (~G & fn)), (n[24] = Q ^ (~sn & ln)), (n[25] = G ^ (~fn & an)), (n[26] = sn ^ (~ln & k)), (n[27] = fn ^ (~an & S)), (n[28] = ln ^ (~k & U)), (n[29] = an ^ (~S & N)), (n[30] = I ^ (~B & V)), (n[31] = D ^ (~P & J)), (n[32] = B ^ (~V & rn)), (n[33] = P ^ (~J & on)), (n[34] = V ^ (~rn & gn)), (n[35] = J ^ (~on & wn)), (n[36] = rn ^ (~gn & I)), (n[37] = on ^ (~wn & D)), (n[38] = gn ^ (~I & B)), (n[39] = wn ^ (~D & P)), (n[40] = O ^ (~j & Y)), (n[41] = T ^ (~F & X)), (n[42] = j ^ (~Y & _)), (n[43] = F ^ (~X & nn)), (n[44] = Y ^ (~_ & hn)), (n[45] = X ^ (~nn & dn)), (n[46] = _ ^ (~hn & O)), (n[47] = nn ^ (~dn & T)), (n[48] = hn ^ (~O & j)), (n[49] = dn ^ (~T & F)), (n[0] ^= IOTA_CONSTANTS[e * 2]), (n[1] ^= IOTA_CONSTANTS[e * 2 + 1])
2201
+ ;(n[0] = A ^ (~v & K)), (n[1] = M ^ (~U & Z)), (n[2] = v ^ (~K & un)), (n[3] = U ^ (~Z & cn)), (n[4] = K ^ (~un & xn)), (n[5] = Z ^ (~cn & yn)), (n[6] = un ^ (~xn & A)), (n[7] = cn ^ (~yn & M)), (n[8] = xn ^ (~A & v)), (n[9] = yn ^ (~M & U)), (n[10] = C ^ (~z & W)), (n[11] = R ^ (~q & H)), (n[12] = z ^ (~W & en)), (n[13] = q ^ (~H & tn)), (n[14] = W ^ (~en & pn)), (n[15] = H ^ (~tn & mn)), (n[16] = en ^ (~pn & C)), (n[17] = tn ^ (~mn & R)), (n[18] = pn ^ (~C & z)), (n[19] = mn ^ (~R & q)), (n[20] = k ^ (~L & Q)), (n[21] = S ^ (~N & G)), (n[22] = L ^ (~Q & sn)), (n[23] = N ^ (~G & fn)), (n[24] = Q ^ (~sn & ln)), (n[25] = G ^ (~fn & an)), (n[26] = sn ^ (~ln & k)), (n[27] = fn ^ (~an & S)), (n[28] = ln ^ (~k & L)), (n[29] = an ^ (~S & N)), (n[30] = I ^ (~B & V)), (n[31] = D ^ (~P & J)), (n[32] = B ^ (~V & rn)), (n[33] = P ^ (~J & on)), (n[34] = V ^ (~rn & gn)), (n[35] = J ^ (~on & wn)), (n[36] = rn ^ (~gn & I)), (n[37] = on ^ (~wn & D)), (n[38] = gn ^ (~I & B)), (n[39] = wn ^ (~D & P)), (n[40] = O ^ (~j & Y)), (n[41] = T ^ (~F & X)), (n[42] = j ^ (~Y & _)), (n[43] = F ^ (~X & nn)), (n[44] = Y ^ (~_ & hn)), (n[45] = X ^ (~nn & dn)), (n[46] = _ ^ (~hn & O)), (n[47] = nn ^ (~dn & T)), (n[48] = hn ^ (~O & j)), (n[49] = dn ^ (~T & F)), (n[0] ^= IOTA_CONSTANTS[e * 2]), (n[1] ^= IOTA_CONSTANTS[e * 2 + 1])
2181
2202
  }
2182
2203
  }
2183
2204
  function bytesToNumbers(n) {
@@ -2871,5 +2892,5 @@ class Lock {
2871
2892
  (exports.Types = { isFunction, isObject, isStrictlyObject, isEmptyArray, isEmptyObject, isUndefined, isString, isNumber, isBoolean, isDate, isBlank, isId, isIntegerString, isHexString, isUrl, isBigint, isNullable, asString, asHexString, asSafeString, asIntegerString, asNumber, asFunction, asInteger, asBoolean, asDate, asNullableString, asEmptiableString, asId, asTime, asArray, asObject, asNullableObject, asStringMap, asNumericDictionary, asUrl, asBigint, asEmptiable, asNullable, asOptional, enforceObjectShape, enforceArrayShape, isPng, isJpg, isWebp, isImage }),
2872
2893
  (exports.Strings = { tokenizeByCount, tokenizeByLength, searchHex, searchSubstring, randomHex: randomHexString, randomLetter: randomLetterString, randomAlphanumeric: randomAlphanumericString, randomRichAscii: randomRichAsciiString, randomUnicode: randomUnicodeString, includesAny, slugify, normalForm, enumify, escapeHtml, decodeHtmlEntities, after, afterLast, before, beforeLast, betweenWide, betweenNarrow, getPreLine, containsWord, containsWords, joinUrl, getFuzzyMatchScore, sortByFuzzyScore, splitOnce, splitAll, randomize, expand, shrinkTrim, capitalize, decapitalize, csvEscape, parseCsv, surroundInOut, getExtension, getBasename, normalizeEmail, normalizeFilename, parseFilename, camelToTitle, slugToTitle, slugToCamel, joinHumanly, findWeightedPair, extractBlock, extractAllBlocks, replaceBlocks, indexOfEarliest, lastIndexOfBefore, parseHtmlAttributes, readNextWord, readWordsAfterAll, resolveVariables, resolveVariableWithDefaultSyntax, resolveRemainingVariablesWithDefaults, isLetter, isDigit, isLetterOrDigit, isValidObjectPathCharacter, insert: insertString, indexOfRegex, allIndexOf, lineMatches, linesMatchInOrder, represent, resolveMarkdownLinks, buildUrl, isChinese, replaceBetweenStrings, describeMarkdown, isBalanced, textToFormat, splitFormatting, splitHashtags, splitUrls, route, explodeReplace, generateVariants, replaceWord, replacePascalCaseWords, stripHtml, breakLine, measureTextWidth, toLines, levenshteinDistance, findCommonPrefix, findCommonDirectory }),
2873
2894
  (exports.Assertions = { asEqual, asTrue, asTruthy, asFalse, asFalsy, asEither }),
2874
- (exports.Cache = { get: getCached, delete: deleteFromCache, deleteExpired: deleteExpiredFromCache, size: cacheSize }),
2895
+ (exports.Cache = { get: getCached, getDeferred: getCachedDeferred, delete: deleteFromCache, deleteExpired: deleteExpiredFromCache, size: cacheSize, clear: clearCache }),
2875
2896
  (exports.Vector = { addPoint, subtractPoint, multiplyPoint, normalizePoint, pushPoint, filterCoordinates, findCorners, findLines, raycast, raycastCircle, getLineIntersectionPoint })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "33.9.0",
3
+ "version": "33.11.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {