cafe-utility 32.1.0 → 33.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 +4 -2
  2. package/index.js +13 -12
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -109,8 +109,10 @@ declare function asHexString(
109
109
  options?: {
110
110
  name?: string
111
111
  byteLength?: number
112
+ uneven?: boolean
113
+ strictPrefix?: boolean
112
114
  }
113
- ): string
115
+ ): `0x${string}`
114
116
  declare function asSafeString(
115
117
  string: any,
116
118
  options?: {
@@ -450,7 +452,7 @@ declare function minutes(value: number): number
450
452
  declare function hours(value: number): number
451
453
  declare function days(value: number): number
452
454
  declare function makeDate(numberWithUnit: string): number
453
- declare function makeStorage(numberWithUnit: string): number
455
+ declare function makeStorage(numberWithUnit: string, conversionMultiplier?: number): number
454
456
  declare function getPreLine(string: string): string
455
457
  declare function getCached<T>(key: string, ttlMillis: number, handler: () => Promise<T>): Promise<T>
456
458
  declare function deleteFromCache(key: string): void
package/index.js CHANGED
@@ -373,10 +373,11 @@ function asString(n, e) {
373
373
  }
374
374
  function asHexString(n, e) {
375
375
  if (!isHexString(n)) throw new TypeError(`Expected hex string${e?.name ? ` for ${e.name}` : ''}, got: ` + n)
376
+ if (e?.strictPrefix && !n.startsWith('0x')) throw new TypeError(`Expected hex string with 0x prefix${e?.name ? ` for ${e.name}` : ''}, got: ` + n)
376
377
  const t = n.replace(/^0x/, '')
377
- if (t.length % 2 !== 0) throw RangeError(`Expected even number of hex digits${e?.name ? ` for ${e.name}` : ''}; got: ` + n)
378
+ if (t.length % 2 !== 0 && !e?.uneven) throw RangeError(`Expected even number of hex digits${e?.name ? ` for ${e.name}` : ''}; got: ` + n)
378
379
  if (e && e.byteLength && t.length !== e.byteLength * 2) throw RangeError(`Expected hex string${e?.name ? ` for ${e.name}` : ''} of byte length ${e.byteLength}; got: ` + t)
379
- return t
380
+ return `0x${t}`
380
381
  }
381
382
  function asSafeString(n, e) {
382
383
  if (
@@ -1416,21 +1417,21 @@ function makeDate(n) {
1416
1417
  .replace(/^-?[0-9.]+/, '')
1417
1418
  .trim()
1418
1419
  .toLowerCase(),
1419
- r = dateUnits[t]
1420
+ r = t === '' ? 1 : dateUnits[t]
1420
1421
  if (!r) throw Error(`Unknown unit: "${t}"`)
1421
- return e * r
1422
+ return Math.ceil(e * r)
1422
1423
  }
1423
- const storageUnits = { b: 1, byte: 1, bytes: 1, kb: 1024, kilobyte: 1024, kilobytes: 1024, mb: 1024 ** 2, megabyte: 1024 ** 2, megabytes: 1024 ** 2, gb: 1024 ** 3, gigabyte: 1024 ** 3, gigabytes: 1024 ** 3, tb: 1024 ** 4, terabyte: 1024 ** 4, terabytes: 1024 ** 4 }
1424
- function makeStorage(n) {
1425
- const e = parseFloat(n)
1426
- if (isNaN(e)) throw Error('makeDate got NaN for input')
1427
- const t = n
1424
+ 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 }
1425
+ function makeStorage(n, e = 1024) {
1426
+ const t = parseFloat(n)
1427
+ if (isNaN(t)) throw Error('makeStorage got NaN for input')
1428
+ const r = n
1428
1429
  .replace(/^-?[0-9.]+/, '')
1429
1430
  .trim()
1430
1431
  .toLowerCase(),
1431
- r = storageUnits[t]
1432
- if (!r) throw Error(`Unknown unit: "${t}"`)
1433
- return e * r
1432
+ o = r === '' ? 0 : storageUnitExponents[r]
1433
+ if (o == null) throw Error(`Unknown unit: "${r}"`)
1434
+ return Math.ceil(t * e ** o)
1434
1435
  }
1435
1436
  function getPreLine(n) {
1436
1437
  return n.replace(/ +/g, ' ').replace(/^ /gm, '')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "32.1.0",
3
+ "version": "33.0.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {