@cacheable/utils 1.0.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/README.md +3 -4
- package/dist/index.cjs +79 -73
- package/dist/index.d.cts +24 -24
- package/dist/index.d.ts +24 -24
- package/dist/index.js +79 -73
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
6
6
|
[](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
|
|
7
|
-
[](https://www.npmjs.com/package/@cacheable/utils)
|
|
8
|
+
[](https://www.npmjs.com/package/@cacheable/utils)
|
|
9
9
|
[](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
|
|
10
10
|
|
|
11
11
|
`@cacheable/utils` is a collecton of utility functions, helpers, and types for `cacheable` and other caching libraries. It provides a robust set of features to enhance caching capabilities, including:
|
|
@@ -108,11 +108,10 @@ If you want to get a number hash you can use the `hashToNumber` function:
|
|
|
108
108
|
```typescript
|
|
109
109
|
import { hash, hashToNumber } from '@cacheable/utils';
|
|
110
110
|
|
|
111
|
-
const hashValue = hash({foo: 'bar'}, HashAlgorithm.DJB2);
|
|
112
111
|
const min = 0;
|
|
113
112
|
const max = 10;
|
|
114
113
|
|
|
115
|
-
const result = hashToNumber(
|
|
114
|
+
const result = hashToNumber({foo: 'bar'}, min, max, HashAlgorithm.DJB2);
|
|
116
115
|
console.log(result); // A number between 0 and 10 based on the hash value
|
|
117
116
|
```
|
|
118
117
|
|
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) {
|
|
@@ -120,10 +187,18 @@ function hash(object, algorithm = "sha256" /* SHA256 */) {
|
|
|
120
187
|
hasher.update(objectString);
|
|
121
188
|
return hasher.digest("hex");
|
|
122
189
|
}
|
|
123
|
-
function hashToNumber(
|
|
124
|
-
const
|
|
190
|
+
function hashToNumber(object, min = 0, max = 10, algorithm = "sha256" /* SHA256 */) {
|
|
191
|
+
const hashResult = hash(object, algorithm);
|
|
192
|
+
const hashNumber = Number.parseInt(hashResult, 16);
|
|
125
193
|
const range = max - min + 1;
|
|
126
|
-
|
|
194
|
+
const result = min + hashNumber % range;
|
|
195
|
+
if (result < min) {
|
|
196
|
+
return min;
|
|
197
|
+
}
|
|
198
|
+
if (result > max) {
|
|
199
|
+
return max;
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
127
202
|
}
|
|
128
203
|
function djb2Hash(string_) {
|
|
129
204
|
let hash2 = 5381;
|
|
@@ -133,71 +208,6 @@ function djb2Hash(string_) {
|
|
|
133
208
|
return hash2.toString();
|
|
134
209
|
}
|
|
135
210
|
|
|
136
|
-
// src/shorthand-time.ts
|
|
137
|
-
var shorthandToMilliseconds = (shorthand) => {
|
|
138
|
-
let milliseconds;
|
|
139
|
-
if (shorthand === void 0) {
|
|
140
|
-
return void 0;
|
|
141
|
-
}
|
|
142
|
-
if (typeof shorthand === "number") {
|
|
143
|
-
milliseconds = shorthand;
|
|
144
|
-
} else if (typeof shorthand === "string") {
|
|
145
|
-
shorthand = shorthand.trim();
|
|
146
|
-
if (Number.isNaN(Number(shorthand))) {
|
|
147
|
-
const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand);
|
|
148
|
-
if (!match) {
|
|
149
|
-
throw new Error(`Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`);
|
|
150
|
-
}
|
|
151
|
-
const [, value, unit] = match;
|
|
152
|
-
const numericValue = Number.parseFloat(value);
|
|
153
|
-
const unitLower = unit.toLowerCase();
|
|
154
|
-
switch (unitLower) {
|
|
155
|
-
case "ms": {
|
|
156
|
-
milliseconds = numericValue;
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
case "s": {
|
|
160
|
-
milliseconds = numericValue * 1e3;
|
|
161
|
-
break;
|
|
162
|
-
}
|
|
163
|
-
case "m": {
|
|
164
|
-
milliseconds = numericValue * 1e3 * 60;
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
case "h": {
|
|
168
|
-
milliseconds = numericValue * 1e3 * 60 * 60;
|
|
169
|
-
break;
|
|
170
|
-
}
|
|
171
|
-
case "hr": {
|
|
172
|
-
milliseconds = numericValue * 1e3 * 60 * 60;
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
case "d": {
|
|
176
|
-
milliseconds = numericValue * 1e3 * 60 * 60 * 24;
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
/* c8 ignore next 3 */
|
|
180
|
-
default: {
|
|
181
|
-
milliseconds = Number(shorthand);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
} else {
|
|
185
|
-
milliseconds = Number(shorthand);
|
|
186
|
-
}
|
|
187
|
-
} else {
|
|
188
|
-
throw new TypeError("Time must be a string or a number.");
|
|
189
|
-
}
|
|
190
|
-
return milliseconds;
|
|
191
|
-
};
|
|
192
|
-
var shorthandToTime = (shorthand, fromDate) => {
|
|
193
|
-
fromDate ??= /* @__PURE__ */ new Date();
|
|
194
|
-
const milliseconds = shorthandToMilliseconds(shorthand);
|
|
195
|
-
if (milliseconds === void 0) {
|
|
196
|
-
return fromDate.getTime();
|
|
197
|
-
}
|
|
198
|
-
return fromDate.getTime() + milliseconds;
|
|
199
|
-
};
|
|
200
|
-
|
|
201
211
|
// src/sleep.ts
|
|
202
212
|
var sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
203
213
|
|
|
@@ -329,28 +339,24 @@ var Stats = class {
|
|
|
329
339
|
}
|
|
330
340
|
this._clears++;
|
|
331
341
|
}
|
|
332
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
333
342
|
incrementVSize(value) {
|
|
334
343
|
if (!this._enabled) {
|
|
335
344
|
return;
|
|
336
345
|
}
|
|
337
346
|
this._vsize += this.roughSizeOfObject(value);
|
|
338
347
|
}
|
|
339
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
340
348
|
decreaseVSize(value) {
|
|
341
349
|
if (!this._enabled) {
|
|
342
350
|
return;
|
|
343
351
|
}
|
|
344
352
|
this._vsize -= this.roughSizeOfObject(value);
|
|
345
353
|
}
|
|
346
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
347
354
|
incrementKSize(key) {
|
|
348
355
|
if (!this._enabled) {
|
|
349
356
|
return;
|
|
350
357
|
}
|
|
351
358
|
this._ksize += this.roughSizeOfString(key);
|
|
352
359
|
}
|
|
353
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
354
360
|
decreaseKSize(key) {
|
|
355
361
|
if (!this._enabled) {
|
|
356
362
|
return;
|
|
@@ -441,7 +447,7 @@ function calculateTtlFromExpiration(ttl, expires) {
|
|
|
441
447
|
if (expiresFromTtl === void 0) {
|
|
442
448
|
return ttlFromExpires;
|
|
443
449
|
}
|
|
444
|
-
if (expires > expiresFromTtl) {
|
|
450
|
+
if (expires && expires > expiresFromTtl) {
|
|
445
451
|
return ttl;
|
|
446
452
|
}
|
|
447
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 {
|
|
@@ -59,26 +78,7 @@ declare enum HashAlgorithm {
|
|
|
59
78
|
* @returns {string} The hash of the object
|
|
60
79
|
*/
|
|
61
80
|
declare function hash(object: any, algorithm?: HashAlgorithm): string;
|
|
62
|
-
declare function hashToNumber(
|
|
63
|
-
|
|
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;
|
|
81
|
+
declare function hashToNumber(object: any, min?: number, max?: number, algorithm?: HashAlgorithm): number;
|
|
82
82
|
|
|
83
83
|
declare const sleep: (ms: number) => Promise<unknown>;
|
|
84
84
|
|
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 {
|
|
@@ -59,26 +78,7 @@ declare enum HashAlgorithm {
|
|
|
59
78
|
* @returns {string} The hash of the object
|
|
60
79
|
*/
|
|
61
80
|
declare function hash(object: any, algorithm?: HashAlgorithm): string;
|
|
62
|
-
declare function hashToNumber(
|
|
63
|
-
|
|
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;
|
|
81
|
+
declare function hashToNumber(object: any, min?: number, max?: number, algorithm?: HashAlgorithm): number;
|
|
82
82
|
|
|
83
83
|
declare const sleep: (ms: number) => Promise<unknown>;
|
|
84
84
|
|
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) {
|
|
@@ -74,10 +141,18 @@ function hash(object, algorithm = "sha256" /* SHA256 */) {
|
|
|
74
141
|
hasher.update(objectString);
|
|
75
142
|
return hasher.digest("hex");
|
|
76
143
|
}
|
|
77
|
-
function hashToNumber(
|
|
78
|
-
const
|
|
144
|
+
function hashToNumber(object, min = 0, max = 10, algorithm = "sha256" /* SHA256 */) {
|
|
145
|
+
const hashResult = hash(object, algorithm);
|
|
146
|
+
const hashNumber = Number.parseInt(hashResult, 16);
|
|
79
147
|
const range = max - min + 1;
|
|
80
|
-
|
|
148
|
+
const result = min + hashNumber % range;
|
|
149
|
+
if (result < min) {
|
|
150
|
+
return min;
|
|
151
|
+
}
|
|
152
|
+
if (result > max) {
|
|
153
|
+
return max;
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
81
156
|
}
|
|
82
157
|
function djb2Hash(string_) {
|
|
83
158
|
let hash2 = 5381;
|
|
@@ -87,71 +162,6 @@ function djb2Hash(string_) {
|
|
|
87
162
|
return hash2.toString();
|
|
88
163
|
}
|
|
89
164
|
|
|
90
|
-
// src/shorthand-time.ts
|
|
91
|
-
var shorthandToMilliseconds = (shorthand) => {
|
|
92
|
-
let milliseconds;
|
|
93
|
-
if (shorthand === void 0) {
|
|
94
|
-
return void 0;
|
|
95
|
-
}
|
|
96
|
-
if (typeof shorthand === "number") {
|
|
97
|
-
milliseconds = shorthand;
|
|
98
|
-
} else if (typeof shorthand === "string") {
|
|
99
|
-
shorthand = shorthand.trim();
|
|
100
|
-
if (Number.isNaN(Number(shorthand))) {
|
|
101
|
-
const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand);
|
|
102
|
-
if (!match) {
|
|
103
|
-
throw new Error(`Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`);
|
|
104
|
-
}
|
|
105
|
-
const [, value, unit] = match;
|
|
106
|
-
const numericValue = Number.parseFloat(value);
|
|
107
|
-
const unitLower = unit.toLowerCase();
|
|
108
|
-
switch (unitLower) {
|
|
109
|
-
case "ms": {
|
|
110
|
-
milliseconds = numericValue;
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
case "s": {
|
|
114
|
-
milliseconds = numericValue * 1e3;
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
case "m": {
|
|
118
|
-
milliseconds = numericValue * 1e3 * 60;
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
case "h": {
|
|
122
|
-
milliseconds = numericValue * 1e3 * 60 * 60;
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
case "hr": {
|
|
126
|
-
milliseconds = numericValue * 1e3 * 60 * 60;
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
case "d": {
|
|
130
|
-
milliseconds = numericValue * 1e3 * 60 * 60 * 24;
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
/* c8 ignore next 3 */
|
|
134
|
-
default: {
|
|
135
|
-
milliseconds = Number(shorthand);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
milliseconds = Number(shorthand);
|
|
140
|
-
}
|
|
141
|
-
} else {
|
|
142
|
-
throw new TypeError("Time must be a string or a number.");
|
|
143
|
-
}
|
|
144
|
-
return milliseconds;
|
|
145
|
-
};
|
|
146
|
-
var shorthandToTime = (shorthand, fromDate) => {
|
|
147
|
-
fromDate ??= /* @__PURE__ */ new Date();
|
|
148
|
-
const milliseconds = shorthandToMilliseconds(shorthand);
|
|
149
|
-
if (milliseconds === void 0) {
|
|
150
|
-
return fromDate.getTime();
|
|
151
|
-
}
|
|
152
|
-
return fromDate.getTime() + milliseconds;
|
|
153
|
-
};
|
|
154
|
-
|
|
155
165
|
// src/sleep.ts
|
|
156
166
|
var sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
157
167
|
|
|
@@ -283,28 +293,24 @@ var Stats = class {
|
|
|
283
293
|
}
|
|
284
294
|
this._clears++;
|
|
285
295
|
}
|
|
286
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
287
296
|
incrementVSize(value) {
|
|
288
297
|
if (!this._enabled) {
|
|
289
298
|
return;
|
|
290
299
|
}
|
|
291
300
|
this._vsize += this.roughSizeOfObject(value);
|
|
292
301
|
}
|
|
293
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
294
302
|
decreaseVSize(value) {
|
|
295
303
|
if (!this._enabled) {
|
|
296
304
|
return;
|
|
297
305
|
}
|
|
298
306
|
this._vsize -= this.roughSizeOfObject(value);
|
|
299
307
|
}
|
|
300
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
301
308
|
incrementKSize(key) {
|
|
302
309
|
if (!this._enabled) {
|
|
303
310
|
return;
|
|
304
311
|
}
|
|
305
312
|
this._ksize += this.roughSizeOfString(key);
|
|
306
313
|
}
|
|
307
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
308
314
|
decreaseKSize(key) {
|
|
309
315
|
if (!this._enabled) {
|
|
310
316
|
return;
|
|
@@ -395,7 +401,7 @@ function calculateTtlFromExpiration(ttl, expires) {
|
|
|
395
401
|
if (expiresFromTtl === void 0) {
|
|
396
402
|
return ttlFromExpires;
|
|
397
403
|
}
|
|
398
|
-
if (expires > expiresFromTtl) {
|
|
404
|
+
if (expires && expires > expiresFromTtl) {
|
|
399
405
|
return ttl;
|
|
400
406
|
}
|
|
401
407
|
return ttlFromExpires;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cacheable/utils",
|
|
3
|
-
"version": "1.
|
|
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
|
-
"@
|
|
25
|
-
"@
|
|
26
|
-
"@keyv/
|
|
27
|
-
"@
|
|
28
|
-
"@types/node": "^24.
|
|
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.
|
|
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
|
-
"
|
|
53
|
-
"test
|
|
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
|
}
|