@unchainedshop/utils 4.8.29 → 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/BaseAdapter.d.ts +1 -0
- package/lib/director/BaseDirector.d.ts +0 -6
- package/lib/director/BaseDirector.js +1 -21
- package/lib/locale-helpers.js +33 -4
- package/lib/phone-number.js +7 -7
- package/lib/utils-index.d.ts +6 -1
- package/lib/utils-index.js +2 -1
- package/package.json +7 -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;
|
|
@@ -4,10 +4,4 @@ export interface IBaseDirector<Adapter extends IBaseAdapter> {
|
|
|
4
4
|
adapterFilter?: (adapter: Adapter) => boolean;
|
|
5
5
|
}) => Adapter[];
|
|
6
6
|
getAdapter: (key: string) => Adapter | null;
|
|
7
|
-
registerAdapter: (A: Adapter) => void;
|
|
8
|
-
unregisterAdapter: (key: string) => boolean;
|
|
9
7
|
}
|
|
10
|
-
export declare const BaseDirector: <AdapterType extends IBaseAdapter>(directorName: string, options?: {
|
|
11
|
-
adapterSortKey?: string;
|
|
12
|
-
adapterKeyField?: string;
|
|
13
|
-
}) => IBaseDirector<AdapterType>;
|
|
@@ -1,21 +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
|
-
registerAdapter: (Adapter) => {
|
|
15
|
-
Adapters.set(Adapter[keyField], Adapter);
|
|
16
|
-
},
|
|
17
|
-
unregisterAdapter: (key) => {
|
|
18
|
-
return Adapters.delete(key);
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
};
|
|
1
|
+
export {};
|
package/lib/locale-helpers.js
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
|
-
import { resolveAcceptLanguage } from 'resolve-accept-language';
|
|
2
1
|
const { UNCHAINED_LANG = 'de', UNCHAINED_COUNTRY = 'CH', UNCHAINED_CURRENCY = 'CHF' } = process.env;
|
|
2
|
+
function parseAcceptLanguage(header) {
|
|
3
|
+
if (!header)
|
|
4
|
+
return [];
|
|
5
|
+
return header
|
|
6
|
+
.split(',')
|
|
7
|
+
.map((part) => {
|
|
8
|
+
const [locale, ...params] = part.trim().split(';');
|
|
9
|
+
const qParam = params.find((p) => p.trim().startsWith('q='));
|
|
10
|
+
const quality = qParam ? parseFloat(qParam.trim().slice(2)) : 1;
|
|
11
|
+
return { locale: locale.trim(), quality: Number.isNaN(quality) ? 0 : quality };
|
|
12
|
+
})
|
|
13
|
+
.sort((a, b) => b.quality - a.quality);
|
|
14
|
+
}
|
|
15
|
+
function resolveAcceptLanguage(acceptLanguage, supportedLocales, defaultLocale) {
|
|
16
|
+
if (!supportedLocales.length) {
|
|
17
|
+
return { match: defaultLocale };
|
|
18
|
+
}
|
|
19
|
+
const parsed = parseAcceptLanguage(acceptLanguage);
|
|
20
|
+
for (const { locale } of parsed) {
|
|
21
|
+
const exact = supportedLocales.find((s) => s.toLowerCase() === locale.toLowerCase());
|
|
22
|
+
if (exact)
|
|
23
|
+
return { match: exact };
|
|
24
|
+
const lang = locale.split('-')[0].toLowerCase();
|
|
25
|
+
const langMatch = supportedLocales.find((s) => s.toLowerCase().startsWith(lang + '-'));
|
|
26
|
+
if (langMatch)
|
|
27
|
+
return { match: langMatch };
|
|
28
|
+
}
|
|
29
|
+
if (supportedLocales.includes(defaultLocale)) {
|
|
30
|
+
return { match: defaultLocale };
|
|
31
|
+
}
|
|
32
|
+
return { match: supportedLocales[0] || defaultLocale };
|
|
33
|
+
}
|
|
3
34
|
export const systemLocale = new Intl.Locale(`${UNCHAINED_LANG}-${UNCHAINED_COUNTRY}`);
|
|
4
35
|
export const determineFallbackLocale = (countries, languages) => {
|
|
5
36
|
try {
|
|
@@ -29,9 +60,7 @@ export const resolveBestSupported = (acceptLanguage, acceptCountry, { countries
|
|
|
29
60
|
}, []);
|
|
30
61
|
const fallbackLocale = determineFallbackLocale(countries, languages);
|
|
31
62
|
try {
|
|
32
|
-
const { match } = resolveAcceptLanguage(acceptLanguage || '', supportedLocales, fallbackLocale.baseName
|
|
33
|
-
returnMatchType: true,
|
|
34
|
-
});
|
|
63
|
+
const { match } = resolveAcceptLanguage(acceptLanguage || '', supportedLocales, fallbackLocale.baseName);
|
|
35
64
|
return new Intl.Locale(match);
|
|
36
65
|
}
|
|
37
66
|
catch {
|
package/lib/phone-number.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parsePhoneNumber } from 'awesome-phonenumber';
|
|
2
2
|
export const normalizePhoneNumber = (value, defaultCountry) => {
|
|
3
3
|
if (!value)
|
|
4
4
|
return undefined;
|
|
5
|
-
const parsed =
|
|
6
|
-
if (!parsed
|
|
5
|
+
const parsed = parsePhoneNumber(value, defaultCountry ? { regionCode: defaultCountry } : undefined);
|
|
6
|
+
if (!parsed.valid)
|
|
7
7
|
return undefined;
|
|
8
|
-
return parsed.number;
|
|
8
|
+
return parsed.number.e164;
|
|
9
9
|
};
|
|
10
10
|
export const phoneNumberToParts = (value, defaultCountry) => {
|
|
11
11
|
if (!value)
|
|
12
12
|
return undefined;
|
|
13
|
-
const parsed =
|
|
14
|
-
if (!parsed
|
|
13
|
+
const parsed = parsePhoneNumber(value, defaultCountry ? { regionCode: defaultCountry } : undefined);
|
|
14
|
+
if (!parsed.valid)
|
|
15
15
|
return undefined;
|
|
16
|
-
return { cc: parsed.
|
|
16
|
+
return { cc: String(parsed.countryCode), subscriber: parsed.number.significant };
|
|
17
17
|
};
|
package/lib/utils-index.d.ts
CHANGED
|
@@ -5,12 +5,13 @@ export { default as objectInvert } from './object-invert.ts';
|
|
|
5
5
|
export { default as findUnusedSlug } from './find-unused-slug.ts';
|
|
6
6
|
export { default as slugify } from './slugify.ts';
|
|
7
7
|
export { default as generateRandomHash } from './generate-random-hash.ts';
|
|
8
|
-
export { default as memoizeWithTTL } from './memoize-with-ttl.ts';
|
|
9
8
|
export { default as buildObfuscatedFieldsFilter } from './build-obfuscated-fields-filter.ts';
|
|
10
9
|
export { default as sha256 } from './sha256.ts';
|
|
11
10
|
export { default as sha1 } from './sha1.ts';
|
|
12
11
|
export { timingSafeEqual, timingSafeStringEqual } from './timing-safe-equal.ts';
|
|
13
12
|
export { normalizePhoneNumber, phoneNumberToParts } from './phone-number.ts';
|
|
13
|
+
export { default as memoizeWithTTL } from './memoize-with-ttl.ts';
|
|
14
|
+
export { executeBulkOperation } from './executeBulkOperation.ts';
|
|
14
15
|
export declare const SortDirection: {
|
|
15
16
|
readonly ASC: "ASC";
|
|
16
17
|
readonly DESC: "DESC";
|
|
@@ -29,6 +30,10 @@ export interface PricingCalculation {
|
|
|
29
30
|
amount: number;
|
|
30
31
|
baseCategory?: string;
|
|
31
32
|
meta?: any;
|
|
33
|
+
discountId?: string;
|
|
34
|
+
isTaxable?: boolean;
|
|
35
|
+
isNetPrice?: boolean;
|
|
36
|
+
rate?: number;
|
|
32
37
|
}
|
|
33
38
|
export type NodeOrTree<T> = string | Tree<T>;
|
|
34
39
|
export type Tree<T> = NodeOrTree<T>[];
|
package/lib/utils-index.js
CHANGED
|
@@ -5,12 +5,13 @@ export { default as objectInvert } from "./object-invert.js";
|
|
|
5
5
|
export { default as findUnusedSlug } from "./find-unused-slug.js";
|
|
6
6
|
export { default as slugify } from "./slugify.js";
|
|
7
7
|
export { default as generateRandomHash } from "./generate-random-hash.js";
|
|
8
|
-
export { default as memoizeWithTTL } from "./memoize-with-ttl.js";
|
|
9
8
|
export { default as buildObfuscatedFieldsFilter } from "./build-obfuscated-fields-filter.js";
|
|
10
9
|
export { default as sha256 } from "./sha256.js";
|
|
11
10
|
export { default as sha1 } from "./sha1.js";
|
|
12
11
|
export { timingSafeEqual, timingSafeStringEqual } from "./timing-safe-equal.js";
|
|
13
12
|
export { normalizePhoneNumber, phoneNumberToParts } from "./phone-number.js";
|
|
13
|
+
export { default as memoizeWithTTL } from "./memoize-with-ttl.js";
|
|
14
|
+
export { executeBulkOperation } from "./executeBulkOperation.js";
|
|
14
15
|
export const SortDirection = {
|
|
15
16
|
ASC: 'ASC',
|
|
16
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": "
|
|
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,12 +32,11 @@
|
|
|
29
32
|
},
|
|
30
33
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
31
34
|
"dependencies": {
|
|
32
|
-
"@unchainedshop/logger": "^
|
|
33
|
-
"
|
|
34
|
-
"resolve-accept-language": "^3.1.11"
|
|
35
|
+
"@unchainedshop/logger": "^5.0.0-alpha.10",
|
|
36
|
+
"awesome-phonenumber": "^7.8.0"
|
|
35
37
|
},
|
|
36
38
|
"devDependencies": {
|
|
37
39
|
"@types/node": "^26.2.0",
|
|
38
|
-
"typescript": "^
|
|
40
|
+
"typescript": "^6.0.3"
|
|
39
41
|
}
|
|
40
42
|
}
|