@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 CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  # @unchainedshop/utils
5
5
 
6
- Common utility functions and base classes for the Unchained Engine.
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 Base Classes
73
+ ### Director/Adapter Building Blocks
74
74
 
75
75
  | Export | Description |
76
76
  |--------|-------------|
77
- | `BaseAdapter` | Base class for all adapters with logging support |
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 for director implementations |
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
 
@@ -1,5 +1,6 @@
1
- export declare const priceToString: ({ amount, currencyCode }: {
1
+ export declare const priceToString: ({ amount, currencyCode, locale, }: {
2
2
  amount: number;
3
3
  currencyCode: string;
4
+ locale?: Intl.Locale | string;
4
5
  }) => string;
5
6
  export default priceToString;
@@ -1,5 +1,14 @@
1
- export const priceToString = ({ amount, currencyCode }) => {
2
- const fixedPrice = amount / 100;
3
- return `${currencyCode} ${fixedPrice}`;
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;
@@ -3,6 +3,7 @@ export interface IBaseAdapter {
3
3
  key: string;
4
4
  label: string;
5
5
  version: string;
6
+ adapterType?: symbol;
6
7
  asString: () => string;
7
8
  log: (message: string, options?: {
8
9
  level?: LogLevel;
@@ -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 const BaseDirector = (directorName, options) => {
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 {};
@@ -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 {
@@ -1,17 +1,17 @@
1
- import { parsePhoneNumberFromString } from 'libphonenumber-js';
1
+ import { parsePhoneNumber } from 'awesome-phonenumber';
2
2
  export const normalizePhoneNumber = (value, defaultCountry) => {
3
3
  if (!value)
4
4
  return undefined;
5
- const parsed = parsePhoneNumberFromString(value, (defaultCountry || undefined));
6
- if (!parsed?.isValid())
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 = parsePhoneNumberFromString(value, (defaultCountry || undefined));
14
- if (!parsed?.isValid())
13
+ const parsed = parsePhoneNumber(value, defaultCountry ? { regionCode: defaultCountry } : undefined);
14
+ if (!parsed.valid)
15
15
  return undefined;
16
- return { cc: parsed.countryCallingCode, subscriber: parsed.nationalNumber };
16
+ return { cc: String(parsed.countryCode), subscriber: parsed.number.significant };
17
17
  };
@@ -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>[];
@@ -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.8.29",
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": "^4.8.27",
33
- "libphonenumber-js": "^1.13.11",
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": "^5.8.3"
40
+ "typescript": "^6.0.3"
39
41
  }
40
42
  }