@cacheable/utils 1.1.0 → 1.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/dist/index.cjs CHANGED
@@ -44,6 +44,73 @@ __export(index_exports, {
44
44
  });
45
45
  module.exports = __toCommonJS(index_exports);
46
46
 
47
+ // src/shorthand-time.ts
48
+ var shorthandToMilliseconds = (shorthand) => {
49
+ let milliseconds;
50
+ if (shorthand === void 0) {
51
+ return void 0;
52
+ }
53
+ if (typeof shorthand === "number") {
54
+ milliseconds = shorthand;
55
+ } else if (typeof shorthand === "string") {
56
+ shorthand = shorthand.trim();
57
+ if (Number.isNaN(Number(shorthand))) {
58
+ const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand);
59
+ if (!match) {
60
+ throw new Error(
61
+ `Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`
62
+ );
63
+ }
64
+ const [, value, unit] = match;
65
+ const numericValue = Number.parseFloat(value);
66
+ const unitLower = unit.toLowerCase();
67
+ switch (unitLower) {
68
+ case "ms": {
69
+ milliseconds = numericValue;
70
+ break;
71
+ }
72
+ case "s": {
73
+ milliseconds = numericValue * 1e3;
74
+ break;
75
+ }
76
+ case "m": {
77
+ milliseconds = numericValue * 1e3 * 60;
78
+ break;
79
+ }
80
+ case "h": {
81
+ milliseconds = numericValue * 1e3 * 60 * 60;
82
+ break;
83
+ }
84
+ case "hr": {
85
+ milliseconds = numericValue * 1e3 * 60 * 60;
86
+ break;
87
+ }
88
+ case "d": {
89
+ milliseconds = numericValue * 1e3 * 60 * 60 * 24;
90
+ break;
91
+ }
92
+ /* c8 ignore next 3 */
93
+ default: {
94
+ milliseconds = Number(shorthand);
95
+ }
96
+ }
97
+ } else {
98
+ milliseconds = Number(shorthand);
99
+ }
100
+ } else {
101
+ throw new TypeError("Time must be a string or a number.");
102
+ }
103
+ return milliseconds;
104
+ };
105
+ var shorthandToTime = (shorthand, fromDate) => {
106
+ fromDate ??= /* @__PURE__ */ new Date();
107
+ const milliseconds = shorthandToMilliseconds(shorthand);
108
+ if (milliseconds === void 0) {
109
+ return fromDate.getTime();
110
+ }
111
+ return fromDate.getTime() + milliseconds;
112
+ };
113
+
47
114
  // src/coalesce-async.ts
48
115
  var callbacks = /* @__PURE__ */ new Map();
49
116
  function hasKey(key) {
@@ -141,71 +208,6 @@ function djb2Hash(string_) {
141
208
  return hash2.toString();
142
209
  }
143
210
 
144
- // src/shorthand-time.ts
145
- var shorthandToMilliseconds = (shorthand) => {
146
- let milliseconds;
147
- if (shorthand === void 0) {
148
- return void 0;
149
- }
150
- if (typeof shorthand === "number") {
151
- milliseconds = shorthand;
152
- } else if (typeof shorthand === "string") {
153
- shorthand = shorthand.trim();
154
- if (Number.isNaN(Number(shorthand))) {
155
- const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand);
156
- if (!match) {
157
- throw new Error(`Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`);
158
- }
159
- const [, value, unit] = match;
160
- const numericValue = Number.parseFloat(value);
161
- const unitLower = unit.toLowerCase();
162
- switch (unitLower) {
163
- case "ms": {
164
- milliseconds = numericValue;
165
- break;
166
- }
167
- case "s": {
168
- milliseconds = numericValue * 1e3;
169
- break;
170
- }
171
- case "m": {
172
- milliseconds = numericValue * 1e3 * 60;
173
- break;
174
- }
175
- case "h": {
176
- milliseconds = numericValue * 1e3 * 60 * 60;
177
- break;
178
- }
179
- case "hr": {
180
- milliseconds = numericValue * 1e3 * 60 * 60;
181
- break;
182
- }
183
- case "d": {
184
- milliseconds = numericValue * 1e3 * 60 * 60 * 24;
185
- break;
186
- }
187
- /* c8 ignore next 3 */
188
- default: {
189
- milliseconds = Number(shorthand);
190
- }
191
- }
192
- } else {
193
- milliseconds = Number(shorthand);
194
- }
195
- } else {
196
- throw new TypeError("Time must be a string or a number.");
197
- }
198
- return milliseconds;
199
- };
200
- var shorthandToTime = (shorthand, fromDate) => {
201
- fromDate ??= /* @__PURE__ */ new Date();
202
- const milliseconds = shorthandToMilliseconds(shorthand);
203
- if (milliseconds === void 0) {
204
- return fromDate.getTime();
205
- }
206
- return fromDate.getTime() + milliseconds;
207
- };
208
-
209
211
  // src/sleep.ts
210
212
  var sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
211
213
 
@@ -337,28 +339,24 @@ var Stats = class {
337
339
  }
338
340
  this._clears++;
339
341
  }
340
- // eslint-disable-next-line @typescript-eslint/naming-convention
341
342
  incrementVSize(value) {
342
343
  if (!this._enabled) {
343
344
  return;
344
345
  }
345
346
  this._vsize += this.roughSizeOfObject(value);
346
347
  }
347
- // eslint-disable-next-line @typescript-eslint/naming-convention
348
348
  decreaseVSize(value) {
349
349
  if (!this._enabled) {
350
350
  return;
351
351
  }
352
352
  this._vsize -= this.roughSizeOfObject(value);
353
353
  }
354
- // eslint-disable-next-line @typescript-eslint/naming-convention
355
354
  incrementKSize(key) {
356
355
  if (!this._enabled) {
357
356
  return;
358
357
  }
359
358
  this._ksize += this.roughSizeOfString(key);
360
359
  }
361
- // eslint-disable-next-line @typescript-eslint/naming-convention
362
360
  decreaseKSize(key) {
363
361
  if (!this._enabled) {
364
362
  return;
@@ -449,7 +447,7 @@ function calculateTtlFromExpiration(ttl, expires) {
449
447
  if (expiresFromTtl === void 0) {
450
448
  return ttlFromExpires;
451
449
  }
452
- if (expires > expiresFromTtl) {
450
+ if (expires && expires > expiresFromTtl) {
453
451
  return ttl;
454
452
  }
455
453
  return ttlFromExpires;
package/dist/index.d.cts CHANGED
@@ -1,3 +1,22 @@
1
+ /**
2
+ * Converts a shorthand time string or number into milliseconds.
3
+ * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds.
4
+ * If the input is undefined, it returns undefined.
5
+ * If the input is a string that does not match the expected format, it throws an error.
6
+ * @param shorthand - A shorthand time string or number representing milliseconds.
7
+ * @returns The equivalent time in milliseconds or undefined.
8
+ */
9
+ declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined;
10
+ /**
11
+ * Converts a shorthand time string or number into a timestamp.
12
+ * If the shorthand is undefined, it returns the current date's timestamp.
13
+ * If the shorthand is a valid time format, it adds that duration to the current date's timestamp.
14
+ * @param shorthand - A shorthand time string or number representing milliseconds.
15
+ * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided.
16
+ * @returns The timestamp in milliseconds since epoch.
17
+ */
18
+ declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number;
19
+
1
20
  /**
2
21
  * CacheableItem
3
22
  * @typedef {Object} CacheableItem
@@ -38,12 +57,12 @@ type CacheableStoreItem = {
38
57
  */
39
58
  declare function coalesceAsync<T>(
40
59
  /**
41
- * Any identifier to group requests together.
42
- */
60
+ * Any identifier to group requests together.
61
+ */
43
62
  key: string,
44
63
  /**
45
- * The function to run.
46
- */
64
+ * The function to run.
65
+ */
47
66
  fnc: () => T | PromiseLike<T>): Promise<T>;
48
67
 
49
68
  declare enum HashAlgorithm {
@@ -61,25 +80,6 @@ declare enum HashAlgorithm {
61
80
  declare function hash(object: any, algorithm?: HashAlgorithm): string;
62
81
  declare function hashToNumber(object: any, min?: number, max?: number, algorithm?: HashAlgorithm): number;
63
82
 
64
- /**
65
- * Converts a shorthand time string or number into milliseconds.
66
- * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds.
67
- * If the input is undefined, it returns undefined.
68
- * If the input is a string that does not match the expected format, it throws an error.
69
- * @param shorthand - A shorthand time string or number representing milliseconds.
70
- * @returns The equivalent time in milliseconds or undefined.
71
- */
72
- declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined;
73
- /**
74
- * Converts a shorthand time string or number into a timestamp.
75
- * If the shorthand is undefined, it returns the current date's timestamp.
76
- * If the shorthand is a valid time format, it adds that duration to the current date's timestamp.
77
- * @param shorthand - A shorthand time string or number representing milliseconds.
78
- * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided.
79
- * @returns The timestamp in milliseconds since epoch.
80
- */
81
- declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number;
82
-
83
83
  declare const sleep: (ms: number) => Promise<unknown>;
84
84
 
85
85
  type StatsOptions = {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,22 @@
1
+ /**
2
+ * Converts a shorthand time string or number into milliseconds.
3
+ * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds.
4
+ * If the input is undefined, it returns undefined.
5
+ * If the input is a string that does not match the expected format, it throws an error.
6
+ * @param shorthand - A shorthand time string or number representing milliseconds.
7
+ * @returns The equivalent time in milliseconds or undefined.
8
+ */
9
+ declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined;
10
+ /**
11
+ * Converts a shorthand time string or number into a timestamp.
12
+ * If the shorthand is undefined, it returns the current date's timestamp.
13
+ * If the shorthand is a valid time format, it adds that duration to the current date's timestamp.
14
+ * @param shorthand - A shorthand time string or number representing milliseconds.
15
+ * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided.
16
+ * @returns The timestamp in milliseconds since epoch.
17
+ */
18
+ declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number;
19
+
1
20
  /**
2
21
  * CacheableItem
3
22
  * @typedef {Object} CacheableItem
@@ -38,12 +57,12 @@ type CacheableStoreItem = {
38
57
  */
39
58
  declare function coalesceAsync<T>(
40
59
  /**
41
- * Any identifier to group requests together.
42
- */
60
+ * Any identifier to group requests together.
61
+ */
43
62
  key: string,
44
63
  /**
45
- * The function to run.
46
- */
64
+ * The function to run.
65
+ */
47
66
  fnc: () => T | PromiseLike<T>): Promise<T>;
48
67
 
49
68
  declare enum HashAlgorithm {
@@ -61,25 +80,6 @@ declare enum HashAlgorithm {
61
80
  declare function hash(object: any, algorithm?: HashAlgorithm): string;
62
81
  declare function hashToNumber(object: any, min?: number, max?: number, algorithm?: HashAlgorithm): number;
63
82
 
64
- /**
65
- * Converts a shorthand time string or number into milliseconds.
66
- * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds.
67
- * If the input is undefined, it returns undefined.
68
- * If the input is a string that does not match the expected format, it throws an error.
69
- * @param shorthand - A shorthand time string or number representing milliseconds.
70
- * @returns The equivalent time in milliseconds or undefined.
71
- */
72
- declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined;
73
- /**
74
- * Converts a shorthand time string or number into a timestamp.
75
- * If the shorthand is undefined, it returns the current date's timestamp.
76
- * If the shorthand is a valid time format, it adds that duration to the current date's timestamp.
77
- * @param shorthand - A shorthand time string or number representing milliseconds.
78
- * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided.
79
- * @returns The timestamp in milliseconds since epoch.
80
- */
81
- declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number;
82
-
83
83
  declare const sleep: (ms: number) => Promise<unknown>;
84
84
 
85
85
  type StatsOptions = {
package/dist/index.js CHANGED
@@ -1,3 +1,70 @@
1
+ // src/shorthand-time.ts
2
+ var shorthandToMilliseconds = (shorthand) => {
3
+ let milliseconds;
4
+ if (shorthand === void 0) {
5
+ return void 0;
6
+ }
7
+ if (typeof shorthand === "number") {
8
+ milliseconds = shorthand;
9
+ } else if (typeof shorthand === "string") {
10
+ shorthand = shorthand.trim();
11
+ if (Number.isNaN(Number(shorthand))) {
12
+ const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand);
13
+ if (!match) {
14
+ throw new Error(
15
+ `Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`
16
+ );
17
+ }
18
+ const [, value, unit] = match;
19
+ const numericValue = Number.parseFloat(value);
20
+ const unitLower = unit.toLowerCase();
21
+ switch (unitLower) {
22
+ case "ms": {
23
+ milliseconds = numericValue;
24
+ break;
25
+ }
26
+ case "s": {
27
+ milliseconds = numericValue * 1e3;
28
+ break;
29
+ }
30
+ case "m": {
31
+ milliseconds = numericValue * 1e3 * 60;
32
+ break;
33
+ }
34
+ case "h": {
35
+ milliseconds = numericValue * 1e3 * 60 * 60;
36
+ break;
37
+ }
38
+ case "hr": {
39
+ milliseconds = numericValue * 1e3 * 60 * 60;
40
+ break;
41
+ }
42
+ case "d": {
43
+ milliseconds = numericValue * 1e3 * 60 * 60 * 24;
44
+ break;
45
+ }
46
+ /* c8 ignore next 3 */
47
+ default: {
48
+ milliseconds = Number(shorthand);
49
+ }
50
+ }
51
+ } else {
52
+ milliseconds = Number(shorthand);
53
+ }
54
+ } else {
55
+ throw new TypeError("Time must be a string or a number.");
56
+ }
57
+ return milliseconds;
58
+ };
59
+ var shorthandToTime = (shorthand, fromDate) => {
60
+ fromDate ??= /* @__PURE__ */ new Date();
61
+ const milliseconds = shorthandToMilliseconds(shorthand);
62
+ if (milliseconds === void 0) {
63
+ return fromDate.getTime();
64
+ }
65
+ return fromDate.getTime() + milliseconds;
66
+ };
67
+
1
68
  // src/coalesce-async.ts
2
69
  var callbacks = /* @__PURE__ */ new Map();
3
70
  function hasKey(key) {
@@ -95,71 +162,6 @@ function djb2Hash(string_) {
95
162
  return hash2.toString();
96
163
  }
97
164
 
98
- // src/shorthand-time.ts
99
- var shorthandToMilliseconds = (shorthand) => {
100
- let milliseconds;
101
- if (shorthand === void 0) {
102
- return void 0;
103
- }
104
- if (typeof shorthand === "number") {
105
- milliseconds = shorthand;
106
- } else if (typeof shorthand === "string") {
107
- shorthand = shorthand.trim();
108
- if (Number.isNaN(Number(shorthand))) {
109
- const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand);
110
- if (!match) {
111
- throw new Error(`Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`);
112
- }
113
- const [, value, unit] = match;
114
- const numericValue = Number.parseFloat(value);
115
- const unitLower = unit.toLowerCase();
116
- switch (unitLower) {
117
- case "ms": {
118
- milliseconds = numericValue;
119
- break;
120
- }
121
- case "s": {
122
- milliseconds = numericValue * 1e3;
123
- break;
124
- }
125
- case "m": {
126
- milliseconds = numericValue * 1e3 * 60;
127
- break;
128
- }
129
- case "h": {
130
- milliseconds = numericValue * 1e3 * 60 * 60;
131
- break;
132
- }
133
- case "hr": {
134
- milliseconds = numericValue * 1e3 * 60 * 60;
135
- break;
136
- }
137
- case "d": {
138
- milliseconds = numericValue * 1e3 * 60 * 60 * 24;
139
- break;
140
- }
141
- /* c8 ignore next 3 */
142
- default: {
143
- milliseconds = Number(shorthand);
144
- }
145
- }
146
- } else {
147
- milliseconds = Number(shorthand);
148
- }
149
- } else {
150
- throw new TypeError("Time must be a string or a number.");
151
- }
152
- return milliseconds;
153
- };
154
- var shorthandToTime = (shorthand, fromDate) => {
155
- fromDate ??= /* @__PURE__ */ new Date();
156
- const milliseconds = shorthandToMilliseconds(shorthand);
157
- if (milliseconds === void 0) {
158
- return fromDate.getTime();
159
- }
160
- return fromDate.getTime() + milliseconds;
161
- };
162
-
163
165
  // src/sleep.ts
164
166
  var sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
165
167
 
@@ -291,28 +293,24 @@ var Stats = class {
291
293
  }
292
294
  this._clears++;
293
295
  }
294
- // eslint-disable-next-line @typescript-eslint/naming-convention
295
296
  incrementVSize(value) {
296
297
  if (!this._enabled) {
297
298
  return;
298
299
  }
299
300
  this._vsize += this.roughSizeOfObject(value);
300
301
  }
301
- // eslint-disable-next-line @typescript-eslint/naming-convention
302
302
  decreaseVSize(value) {
303
303
  if (!this._enabled) {
304
304
  return;
305
305
  }
306
306
  this._vsize -= this.roughSizeOfObject(value);
307
307
  }
308
- // eslint-disable-next-line @typescript-eslint/naming-convention
309
308
  incrementKSize(key) {
310
309
  if (!this._enabled) {
311
310
  return;
312
311
  }
313
312
  this._ksize += this.roughSizeOfString(key);
314
313
  }
315
- // eslint-disable-next-line @typescript-eslint/naming-convention
316
314
  decreaseKSize(key) {
317
315
  if (!this._enabled) {
318
316
  return;
@@ -403,7 +401,7 @@ function calculateTtlFromExpiration(ttl, expires) {
403
401
  if (expiresFromTtl === void 0) {
404
402
  return ttlFromExpires;
405
403
  }
406
- if (expires > expiresFromTtl) {
404
+ if (expires && expires > expiresFromTtl) {
407
405
  return ttl;
408
406
  }
409
407
  return ttlFromExpires;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cacheable/utils",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Cacheable Utilities for Caching Libraries",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -21,18 +21,17 @@
21
21
  "license": "MIT",
22
22
  "private": false,
23
23
  "devDependencies": {
24
- "@faker-js/faker": "^9.9.0",
25
- "@keyv/redis": "^5.0.0",
26
- "@keyv/valkey": "^1.0.7",
27
- "@types/eslint": "^9.6.1",
28
- "@types/node": "^24.1.0",
24
+ "@biomejs/biome": "^2.2.2",
25
+ "@faker-js/faker": "^10.0.0",
26
+ "@keyv/redis": "^5.1.1",
27
+ "@keyv/valkey": "^1.0.8",
28
+ "@types/node": "^24.3.0",
29
29
  "@vitest/coverage-v8": "^3.2.4",
30
30
  "lru-cache": "^11.1.0",
31
31
  "rimraf": "^6.0.1",
32
32
  "tsup": "^8.5.0",
33
- "typescript": "^5.8.3",
34
- "vitest": "^3.2.4",
35
- "xo": "^1.2.1"
33
+ "typescript": "^5.9.2",
34
+ "vitest": "^3.2.4"
36
35
  },
37
36
  "keywords": [
38
37
  "cacheable",
@@ -49,8 +48,9 @@
49
48
  "scripts": {
50
49
  "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
51
50
  "prepublish": "pnpm build",
52
- "test": "xo --fix && vitest run --coverage",
53
- "test:ci": "xo && vitest run --coverage",
51
+ "lint": "biome check --write --error-on-warnings",
52
+ "test": "pnpm lint && vitest run --coverage",
53
+ "test:ci": "biome check --error-on-warnings && vitest run --coverage",
54
54
  "clean": "rimraf ./dist ./coverage ./node_modules"
55
55
  }
56
56
  }