cafe-utility 14.0.0 → 14.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.
Files changed (3) hide show
  1. package/index.d.ts +13 -9
  2. package/index.js +19 -9
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -54,6 +54,7 @@ declare function isBoolean(value: any): value is boolean;
54
54
  declare function isDate(value: any): value is Date;
55
55
  declare function isBlank(value: any): boolean;
56
56
  declare function isId(value: any): value is number;
57
+ declare function isNumbool(value: any): value is 0 | 1;
57
58
  declare function randomLetterString(length: number, generator?: () => number): string;
58
59
  declare function randomAlphanumericString(length: number, generator?: () => number): string;
59
60
  declare function randomRichAsciiString(length: number, generator?: () => number): string;
@@ -69,6 +70,7 @@ declare function asDate(date: any): Date;
69
70
  declare function asNullableString(string: any): string | null;
70
71
  declare function asEmptiableString(string: any): string;
71
72
  declare function asId(value: any): number;
73
+ declare function asNumbool(value: any): 0 | 1;
72
74
  declare function asTime(value: any): string;
73
75
  declare function asArray(value: any): unknown[];
74
76
  declare function asObject(value: any): Record<string, unknown>;
@@ -338,16 +340,16 @@ interface TemporalData<T> {
338
340
  validUntil: number;
339
341
  data: T;
340
342
  }
341
- interface BidirectionalMap<T> {
342
- map: CafeObject<T>;
343
- keys: string[];
343
+ interface BidirectionalMap<K, T> {
344
+ map: Map<K, T>;
345
+ keys: K[];
344
346
  }
345
- declare function createBidirectionalMap<T>(): BidirectionalMap<T>;
346
- declare function createTemporalBidirectionalMap<T>(): BidirectionalMap<TemporalData<T>>;
347
- declare function pushToBidirectionalMap<T>(object: BidirectionalMap<T>, key: string, item: T, limit?: number): void;
348
- declare function unshiftToBidirectionalMap<T>(object: BidirectionalMap<T>, key: string, item: T, limit?: number): void;
349
- declare function addToTemporalBidirectionalMap<T>(object: BidirectionalMap<TemporalData<T>>, key: string, item: T, expiration: number, limit?: number): void;
350
- declare function getFromTemporalBidirectionalMap<T>(object: BidirectionalMap<TemporalData<T>>, key: string): T | null;
347
+ declare function createBidirectionalMap<K, T>(): BidirectionalMap<K, T>;
348
+ declare function createTemporalBidirectionalMap<K, T>(): BidirectionalMap<K, TemporalData<T>>;
349
+ declare function pushToBidirectionalMap<K, T>(object: BidirectionalMap<K, T>, key: K, item: T, limit?: number): void;
350
+ declare function unshiftToBidirectionalMap<K, T>(object: BidirectionalMap<K, T>, key: K, item: T, limit?: number): void;
351
+ declare function addToTemporalBidirectionalMap<K, T>(object: BidirectionalMap<K, TemporalData<T>>, key: K, item: T, expiration: number, limit?: number): void;
352
+ declare function getFromTemporalBidirectionalMap<K, T>(object: BidirectionalMap<K, TemporalData<T>>, key: K): T | null;
351
353
  declare function makeAsyncQueue(concurrency?: number): {
352
354
  enqueue(fn: () => Promise<void>): void;
353
355
  drain: () => Promise<void>;
@@ -573,6 +575,7 @@ export declare const Types: {
573
575
  isDate: typeof isDate;
574
576
  isBlank: typeof isBlank;
575
577
  isId: typeof isId;
578
+ isNumbool: typeof isNumbool;
576
579
  isNullable: typeof isNullable;
577
580
  asString: typeof asString;
578
581
  asNumber: typeof asNumber;
@@ -582,6 +585,7 @@ export declare const Types: {
582
585
  asNullableString: typeof asNullableString;
583
586
  asEmptiableString: typeof asEmptiableString;
584
587
  asId: typeof asId;
588
+ asNumbool: typeof asNumbool;
585
589
  asTime: typeof asTime;
586
590
  asArray: typeof asArray;
587
591
  asObject: typeof asObject;
package/index.js CHANGED
@@ -277,6 +277,9 @@ function isBlank(n) {
277
277
  function isId(n) {
278
278
  return isNumber(n) && Number.isInteger(n) && n >= 1
279
279
  }
280
+ function isNumbool(n) {
281
+ return n === 0 || n === 1
282
+ }
280
283
  const symbols = '!@#$%^&*()_+-=[]{}|;:<>?,./',
281
284
  symbolsArray = '!@#$%^&*()_+-=[]{}|;:<>?,./'.split(''),
282
285
  lowercaseAlphabet = 'abcdefghijklmnopqrstuvwxyz',
@@ -367,6 +370,11 @@ function asId(n) {
367
370
  if (!isId(e)) throw new TypeError('Expected id, got: ' + n)
368
371
  return e
369
372
  }
373
+ function asNumbool(n) {
374
+ if (n === 1 || n === '1') return 1
375
+ if (n === 0 || n === '0') return 0
376
+ throw new TypeError('Expected numbool, got: ' + n)
377
+ }
370
378
  function asTime(n) {
371
379
  if (!isString(n)) throw new TypeError('Expected time, got: ' + n)
372
380
  const e = n.split(':')
@@ -1604,36 +1612,36 @@ function group(n, e) {
1604
1612
  return t
1605
1613
  }
1606
1614
  function createBidirectionalMap() {
1607
- return { map: {}, keys: [] }
1615
+ return { map: new Map(), keys: [] }
1608
1616
  }
1609
1617
  function createTemporalBidirectionalMap() {
1610
- return { map: {}, keys: [] }
1618
+ return { map: new Map(), keys: [] }
1611
1619
  }
1612
1620
  function pushToBidirectionalMap(n, e, t, r = 100) {
1613
- if (n.map[e]) {
1621
+ if (n.map.has(e)) {
1614
1622
  const o = n.keys.indexOf(e)
1615
1623
  n.keys.splice(o, 1)
1616
1624
  }
1617
- if (((n.map[e] = t), n.keys.push(e), n.keys.length > r)) {
1625
+ if ((n.map.set(e, t), n.keys.push(e), n.keys.length > r)) {
1618
1626
  const o = n.keys.shift()
1619
- o && delete n.map[o]
1627
+ o && n.map.delete(o)
1620
1628
  }
1621
1629
  }
1622
1630
  function unshiftToBidirectionalMap(n, e, t, r = 100) {
1623
- if (n.map[e]) {
1631
+ if (n.map.has(e)) {
1624
1632
  const o = n.keys.indexOf(e)
1625
1633
  n.keys.splice(o, 1)
1626
1634
  }
1627
- if (((n.map[e] = t), n.keys.unshift(e), n.keys.length > r)) {
1635
+ if ((n.map.set(e, t), n.keys.unshift(e), n.keys.length > r)) {
1628
1636
  const o = n.keys.shift()
1629
- o && delete n.map[o]
1637
+ o && n.map.delete(o)
1630
1638
  }
1631
1639
  }
1632
1640
  function addToTemporalBidirectionalMap(n, e, t, r, o = 100) {
1633
1641
  pushToBidirectionalMap(n, e, { validUntil: Date.now() + r, data: t }, o)
1634
1642
  }
1635
1643
  function getFromTemporalBidirectionalMap(n, e) {
1636
- const t = n.map[e]
1644
+ const t = n.map.get(e)
1637
1645
  return t && t.validUntil > Date.now() ? t.data : null
1638
1646
  }
1639
1647
  function makeAsyncQueue(n = 1) {
@@ -2083,6 +2091,7 @@ function raycastCircle(n, e, t) {
2083
2091
  isDate,
2084
2092
  isBlank,
2085
2093
  isId,
2094
+ isNumbool,
2086
2095
  isNullable,
2087
2096
  asString,
2088
2097
  asNumber,
@@ -2092,6 +2101,7 @@ function raycastCircle(n, e, t) {
2092
2101
  asNullableString,
2093
2102
  asEmptiableString,
2094
2103
  asId,
2104
+ asNumbool,
2095
2105
  asTime,
2096
2106
  asArray,
2097
2107
  asObject,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "14.0.0",
3
+ "version": "14.1.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {