@unchainedshop/utils 5.0.0-alpha.1 → 5.0.0-alpha.10
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 +4 -5
- package/lib/ch/priceToString.d.ts +2 -1
- package/lib/ch/priceToString.js +12 -3
- package/lib/director/BaseDirector.d.ts +0 -4
- package/lib/director/BaseDirector.js +1 -15
- package/lib/executeBulkOperation.d.ts +4 -0
- package/lib/executeBulkOperation.js +19 -0
- 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 +3 -1
- package/lib/utils-index.js +3 -1
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
# @unchainedshop/utils
|
|
5
5
|
|
|
6
|
-
Common utility functions and
|
|
6
|
+
Common utility functions and adapter/director building blocks for the Unchained Engine.
|
|
7
7
|
|
|
8
8
|
## Installation
|
|
9
9
|
|
|
@@ -70,14 +70,13 @@ Available under the `calculation` namespace:
|
|
|
70
70
|
| `objectInvert` | Invert object keys and values |
|
|
71
71
|
| `buildObfuscatedFieldsFilter` | Build filter for obfuscating sensitive fields |
|
|
72
72
|
|
|
73
|
-
### Director/Adapter
|
|
73
|
+
### Director/Adapter Building Blocks
|
|
74
74
|
|
|
75
75
|
| Export | Description |
|
|
76
76
|
|--------|-------------|
|
|
77
|
-
| `BaseAdapter` | Base
|
|
78
|
-
| `BaseDirector` | Factory for creating adapter directors |
|
|
77
|
+
| `BaseAdapter` | Base object for adapter implementations with logging support |
|
|
79
78
|
| `IBaseAdapter` | Interface for adapter implementations |
|
|
80
|
-
| `IBaseDirector` | Interface
|
|
79
|
+
| `IBaseDirector` | Interface implemented by the directors in `@unchainedshop/core` (adapters come from the plugin registry) |
|
|
81
80
|
|
|
82
81
|
### Swiss-specific Helpers
|
|
83
82
|
|
package/lib/ch/priceToString.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
export const priceToString = ({ amount, currencyCode }) => {
|
|
2
|
-
const
|
|
3
|
-
|
|
1
|
+
export const priceToString = ({ amount, currencyCode, locale = 'de-CH', }) => {
|
|
2
|
+
const majorUnitAmount = amount / 100;
|
|
3
|
+
try {
|
|
4
|
+
return new Intl.NumberFormat(locale, {
|
|
5
|
+
style: 'currency',
|
|
6
|
+
currency: currencyCode,
|
|
7
|
+
currencyDisplay: 'code',
|
|
8
|
+
}).format(majorUnitAmount);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return `${currencyCode} ${majorUnitAmount}`;
|
|
12
|
+
}
|
|
4
13
|
};
|
|
5
14
|
export default priceToString;
|
|
@@ -5,7 +5,3 @@ export interface IBaseDirector<Adapter extends IBaseAdapter> {
|
|
|
5
5
|
}) => Adapter[];
|
|
6
6
|
getAdapter: (key: string) => Adapter | null;
|
|
7
7
|
}
|
|
8
|
-
export declare const BaseDirector: <AdapterType extends IBaseAdapter>(directorName: string, options?: {
|
|
9
|
-
adapterSortKey?: string;
|
|
10
|
-
adapterKeyField?: string;
|
|
11
|
-
}) => IBaseDirector<AdapterType>;
|
|
@@ -1,15 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
const Adapters = new Map();
|
|
3
|
-
const keyField = options?.adapterKeyField || 'key';
|
|
4
|
-
return {
|
|
5
|
-
getAdapter: (key) => {
|
|
6
|
-
return Adapters.get(key) || null;
|
|
7
|
-
},
|
|
8
|
-
getAdapters: ({ adapterFilter } = {}) => {
|
|
9
|
-
const sortKey = options?.adapterSortKey || keyField;
|
|
10
|
-
return Array.from(Adapters.values())
|
|
11
|
-
.toSorted((left, right) => left[sortKey] - right[sortKey])
|
|
12
|
-
.filter(adapterFilter || (() => true));
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
};
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
2
|
+
const logger = createLogger('unchained:utils:bulk-operation');
|
|
3
|
+
export async function executeBulkOperation(ids, operation) {
|
|
4
|
+
const successIds = [];
|
|
5
|
+
const failedIds = [];
|
|
6
|
+
for (const id of new Set(ids)) {
|
|
7
|
+
try {
|
|
8
|
+
await operation(id);
|
|
9
|
+
successIds.push(id);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
failedIds.push(id);
|
|
13
|
+
logger.debug(`Bulk operation failed for entity ${String(id)}`, {
|
|
14
|
+
error: error instanceof Error ? error.message : String(error),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return { successIds, failedIds };
|
|
19
|
+
}
|
|
@@ -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 { parsePhoneNumber } from 'awesome-phonenumber';
|
|
2
|
+
export const normalizePhoneNumber = (value, defaultCountry) => {
|
|
3
|
+
if (!value)
|
|
4
|
+
return undefined;
|
|
5
|
+
const parsed = parsePhoneNumber(value, defaultCountry ? { regionCode: defaultCountry } : undefined);
|
|
6
|
+
if (!parsed.valid)
|
|
7
|
+
return undefined;
|
|
8
|
+
return parsed.number.e164;
|
|
9
|
+
};
|
|
10
|
+
export const phoneNumberToParts = (value, defaultCountry) => {
|
|
11
|
+
if (!value)
|
|
12
|
+
return undefined;
|
|
13
|
+
const parsed = parsePhoneNumber(value, defaultCountry ? { regionCode: defaultCountry } : undefined);
|
|
14
|
+
if (!parsed.valid)
|
|
15
|
+
return undefined;
|
|
16
|
+
return { cc: String(parsed.countryCode), subscriber: parsed.number.significant };
|
|
17
|
+
};
|
package/lib/utils-index.d.ts
CHANGED
|
@@ -9,7 +9,9 @@ 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';
|
|
14
|
+
export { executeBulkOperation } from './executeBulkOperation.ts';
|
|
13
15
|
export declare const SortDirection: {
|
|
14
16
|
readonly ASC: "ASC";
|
|
15
17
|
readonly DESC: "DESC";
|
package/lib/utils-index.js
CHANGED
|
@@ -9,7 +9,9 @@ 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";
|
|
14
|
+
export { executeBulkOperation } from "./executeBulkOperation.js";
|
|
13
15
|
export const SortDirection = {
|
|
14
16
|
ASC: 'ASC',
|
|
15
17
|
DESC: 'DESC',
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
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.10",
|
|
5
|
+
"files": [
|
|
6
|
+
"lib"
|
|
7
|
+
],
|
|
5
8
|
"main": "lib/utils-index.js",
|
|
6
9
|
"types": "lib/utils-index.d.ts",
|
|
7
10
|
"type": "module",
|
|
@@ -29,11 +32,11 @@
|
|
|
29
32
|
},
|
|
30
33
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
31
34
|
"dependencies": {
|
|
32
|
-
"@unchainedshop/logger": "^5.0.0-alpha.
|
|
33
|
-
"
|
|
35
|
+
"@unchainedshop/logger": "^5.0.0-alpha.10",
|
|
36
|
+
"awesome-phonenumber": "^7.8.0"
|
|
34
37
|
},
|
|
35
38
|
"devDependencies": {
|
|
36
|
-
"@types/node": "^
|
|
37
|
-
"typescript": "^
|
|
39
|
+
"@types/node": "^26.2.0",
|
|
40
|
+
"typescript": "^6.0.3"
|
|
38
41
|
}
|
|
39
42
|
}
|