@unchainedshop/utils 5.0.0-alpha.1 → 5.0.0-alpha.3
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/lib/generate-random-hash.js +3 -7
- package/lib/memoize-with-ttl.d.ts +8 -7
- package/lib/memoize-with-ttl.js +23 -31
- package/lib/phone-number.d.ts +5 -0
- package/lib/phone-number.js +17 -0
- package/lib/utils-index.d.ts +2 -1
- package/lib/utils-index.js +2 -1
- package/package.json +3 -3
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
const
|
|
3
|
-
export default () => {
|
|
4
|
-
const randomBytes = crypto.getRandomValues(new Uint32Array(1));
|
|
5
|
-
const randomNumber = (randomBytes[0] % 999999998) + 1;
|
|
6
|
-
return hashids.encode(randomNumber);
|
|
7
|
-
};
|
|
1
|
+
import { randomInt } from 'node:crypto';
|
|
2
|
+
const ALPHABET = 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
|
|
3
|
+
export default () => Array.from({ length: 6 }, () => ALPHABET[randomInt(ALPHABET.length)]).join('');
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
cacheKey?: (args: any[]) => string;
|
|
4
|
-
}
|
|
5
|
-
export declare function memoizeWithTTL<T extends AsyncFunction>(fn: T, ttlMs: number, options?: MemoizeOptions): T & {
|
|
1
|
+
export interface MemoizedWithTTL<A extends unknown[], R> {
|
|
2
|
+
(...args: A): Promise<R>;
|
|
6
3
|
clear: () => void;
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
delete: (key: unknown) => boolean;
|
|
5
|
+
}
|
|
6
|
+
export default function memoizeWithTTL<A extends unknown[], R>(fn: (...args: A) => Promise<R>, { ttl, cacheKey }: {
|
|
7
|
+
ttl: number;
|
|
8
|
+
cacheKey?: (args: A) => unknown;
|
|
9
|
+
}): MemoizedWithTTL<A, R>;
|
package/lib/memoize-with-ttl.js
CHANGED
|
@@ -1,35 +1,27 @@
|
|
|
1
|
-
function
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
set(key,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const cache = createTTLCache(ttlMs);
|
|
24
|
-
const memoized = async function (...args) {
|
|
25
|
-
const key = options?.cacheKey ? options.cacheKey(args) : JSON.stringify(args);
|
|
26
|
-
const cached = cache.get(key);
|
|
27
|
-
if (cached !== undefined)
|
|
28
|
-
return cached;
|
|
29
|
-
const result = await fn(...args);
|
|
30
|
-
cache.set(key, result);
|
|
31
|
-
return result;
|
|
1
|
+
export default function memoizeWithTTL(fn, { ttl, cacheKey = (args) => args[0] }) {
|
|
2
|
+
const cache = new Map();
|
|
3
|
+
const memoized = (...args) => {
|
|
4
|
+
const key = cacheKey(args);
|
|
5
|
+
const now = Date.now();
|
|
6
|
+
const hit = cache.get(key);
|
|
7
|
+
if (hit && (!hit.settled || hit.expires > now))
|
|
8
|
+
return hit.promise;
|
|
9
|
+
for (const [expiredKey, entry] of cache) {
|
|
10
|
+
if (entry.settled && entry.expires <= now)
|
|
11
|
+
cache.delete(expiredKey);
|
|
12
|
+
}
|
|
13
|
+
const entry = { promise: fn(...args), settled: false, expires: Infinity };
|
|
14
|
+
cache.set(key, entry);
|
|
15
|
+
entry.promise.then(() => {
|
|
16
|
+
entry.settled = true;
|
|
17
|
+
entry.expires = Date.now() + ttl;
|
|
18
|
+
}, () => {
|
|
19
|
+
if (cache.get(key) === entry)
|
|
20
|
+
cache.delete(key);
|
|
21
|
+
});
|
|
22
|
+
return entry.promise;
|
|
32
23
|
};
|
|
33
24
|
memoized.clear = () => cache.clear();
|
|
25
|
+
memoized.delete = (key) => cache.delete(key);
|
|
34
26
|
return memoized;
|
|
35
27
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const normalizePhoneNumber: (value?: string | null, defaultCountry?: string | null) => string | undefined;
|
|
2
|
+
export declare const phoneNumberToParts: (value?: string | null, defaultCountry?: string | null) => {
|
|
3
|
+
cc: string;
|
|
4
|
+
subscriber: string;
|
|
5
|
+
} | undefined;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
2
|
+
export const normalizePhoneNumber = (value, defaultCountry) => {
|
|
3
|
+
if (!value)
|
|
4
|
+
return undefined;
|
|
5
|
+
const parsed = parsePhoneNumberFromString(value, (defaultCountry || undefined));
|
|
6
|
+
if (!parsed?.isValid())
|
|
7
|
+
return undefined;
|
|
8
|
+
return parsed.number;
|
|
9
|
+
};
|
|
10
|
+
export const phoneNumberToParts = (value, defaultCountry) => {
|
|
11
|
+
if (!value)
|
|
12
|
+
return undefined;
|
|
13
|
+
const parsed = parsePhoneNumberFromString(value, (defaultCountry || undefined));
|
|
14
|
+
if (!parsed?.isValid())
|
|
15
|
+
return undefined;
|
|
16
|
+
return { cc: parsed.countryCallingCode, subscriber: parsed.nationalNumber };
|
|
17
|
+
};
|
package/lib/utils-index.d.ts
CHANGED
|
@@ -9,7 +9,8 @@ export { default as buildObfuscatedFieldsFilter } from './build-obfuscated-field
|
|
|
9
9
|
export { default as sha256 } from './sha256.ts';
|
|
10
10
|
export { default as sha1 } from './sha1.ts';
|
|
11
11
|
export { timingSafeEqual, timingSafeStringEqual } from './timing-safe-equal.ts';
|
|
12
|
-
export {
|
|
12
|
+
export { normalizePhoneNumber, phoneNumberToParts } from './phone-number.ts';
|
|
13
|
+
export { default as memoizeWithTTL } from './memoize-with-ttl.ts';
|
|
13
14
|
export declare const SortDirection: {
|
|
14
15
|
readonly ASC: "ASC";
|
|
15
16
|
readonly DESC: "DESC";
|
package/lib/utils-index.js
CHANGED
|
@@ -9,7 +9,8 @@ export { default as buildObfuscatedFieldsFilter } from "./build-obfuscated-field
|
|
|
9
9
|
export { default as sha256 } from "./sha256.js";
|
|
10
10
|
export { default as sha1 } from "./sha1.js";
|
|
11
11
|
export { timingSafeEqual, timingSafeStringEqual } from "./timing-safe-equal.js";
|
|
12
|
-
export {
|
|
12
|
+
export { normalizePhoneNumber, phoneNumberToParts } from "./phone-number.js";
|
|
13
|
+
export { default as memoizeWithTTL } from "./memoize-with-ttl.js";
|
|
13
14
|
export const SortDirection = {
|
|
14
15
|
ASC: 'ASC',
|
|
15
16
|
DESC: 'DESC',
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/utils",
|
|
3
3
|
"description": "Common utility functions and base classes for the Unchained Engine",
|
|
4
|
-
"version": "5.0.0-alpha.
|
|
4
|
+
"version": "5.0.0-alpha.3",
|
|
5
5
|
"main": "lib/utils-index.js",
|
|
6
6
|
"types": "lib/utils-index.d.ts",
|
|
7
7
|
"type": "module",
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@unchainedshop/logger": "^5.0.0-alpha.1",
|
|
33
|
-
"
|
|
33
|
+
"libphonenumber-js": "^1.11.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@types/node": "^
|
|
36
|
+
"@types/node": "^26.2.0",
|
|
37
37
|
"typescript": "^5.8.3"
|
|
38
38
|
}
|
|
39
39
|
}
|