@unchainedshop/utils 2.12.2 → 2.13.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.
@@ -1,7 +1,9 @@
1
+ import { Currency } from '@unchainedshop/types/currencies.js';
1
2
  import type { Locale as LocaleType, Locales as LocalesType } from 'locale';
2
3
  export declare const systemLocale: LocaleType;
3
4
  export declare const resolveBestSupported: (acceptLanguage: string, supportedLocales: LocalesType) => LocaleType;
4
5
  export declare const resolveBestCountry: (localeCountry: any, shopCountry: any, countries: any) => any;
6
+ export declare const resolveBestCurrency: (localeCurrency: any, currencies: Array<Currency>) => string;
5
7
  export declare const resolveUserRemoteAddress: (req: any) => {
6
8
  remoteAddress: any;
7
9
  remotePort: any;
@@ -1 +1 @@
1
- {"version":3,"file":"locale-helpers.d.ts","sourceRoot":"","sources":["../src/locale-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,UAAU,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAC;AAO3E,eAAO,MAAM,YAAY,YAAuD,CAAC;AAEjF,eAAO,MAAM,oBAAoB,mBACf,MAAM,oBACJ,WAAW,KAC5B,UAKF,CAAC;AAEF,eAAO,MAAM,kBAAkB,+DAa9B,CAAC;AAEF,eAAO,MAAM,wBAAwB;;;CAYpC,CAAC"}
1
+ {"version":3,"file":"locale-helpers.d.ts","sourceRoot":"","sources":["../src/locale-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oCAAoC,CAAC;AAC9D,OAAO,KAAK,EAAE,MAAM,IAAI,UAAU,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAC;AAO3E,eAAO,MAAM,YAAY,YAAuD,CAAC;AAEjF,eAAO,MAAM,oBAAoB,mBACf,MAAM,oBACJ,WAAW,KAC5B,UAKF,CAAC;AAEF,eAAO,MAAM,kBAAkB,+DAa9B,CAAC;AAEF,eAAO,MAAM,mBAAmB,oCAAgC,KAAK,CAAC,QAAQ,CAAC,WAc9E,CAAC;AAEF,eAAO,MAAM,wBAAwB;;;CAYpC,CAAC"}
@@ -1,6 +1,6 @@
1
1
  import localePkg from 'locale';
2
2
  const { Locales, Locale } = localePkg;
3
- const { UNCHAINED_LANG = 'de', UNCHAINED_COUNTRY = 'CH' } = process.env;
3
+ const { UNCHAINED_LANG = 'de', UNCHAINED_COUNTRY = 'CH', UNCHAINED_CURRENCY = 'CHF' } = process.env;
4
4
  export const systemLocale = new Locale(`${UNCHAINED_LANG}-${UNCHAINED_COUNTRY}`);
5
5
  export const resolveBestSupported = (acceptLanguage, supportedLocales) => {
6
6
  const acceptLocale = new Locales(acceptLanguage);
@@ -23,6 +23,19 @@ export const resolveBestCountry = (localeCountry, shopCountry, countries) => {
23
23
  }
24
24
  return localeCountry || systemLocale.country;
25
25
  };
26
+ export const resolveBestCurrency = (localeCurrency, currencies) => {
27
+ if (localeCurrency) {
28
+ const resolvedCurrency = currencies.find((currency) => currency.isoCode === localeCurrency);
29
+ if (resolvedCurrency) {
30
+ return resolvedCurrency.isoCode;
31
+ }
32
+ }
33
+ const fallbackCurrency = currencies.find((currency) => currency.isoCode === UNCHAINED_CURRENCY);
34
+ if (fallbackCurrency) {
35
+ return fallbackCurrency.isoCode;
36
+ }
37
+ return currencies?.[0]?.isoCode || UNCHAINED_CURRENCY;
38
+ };
26
39
  export const resolveUserRemoteAddress = (req) => {
27
40
  const remoteAddress = req.headers['x-real-ip'] ||
28
41
  req.headers['x-forwarded-for'] ||
@@ -1 +1 @@
1
- {"version":3,"file":"locale-helpers.js","sourceRoot":"","sources":["../src/locale-helpers.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,QAAQ,CAAC;AAE/B,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;AAEtC,MAAM,EAAE,cAAc,GAAG,IAAI,EAAE,iBAAiB,GAAG,IAAI,EAAE,GAAG,OAAO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AAC;IAE7F,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,CAAC;AACvC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/utils",
3
- "version": "2.12.2",
3
+ "version": "2.13.1",
4
4
  "main": "lib/utils-index.js",
5
5
  "exports": {
6
6
  ".": "./lib/utils-index.js",
@@ -30,16 +30,16 @@
30
30
  },
31
31
  "homepage": "https://github.com/unchainedshop/unchained#readme",
32
32
  "dependencies": {
33
- "@unchainedshop/logger": "^2.12.2",
33
+ "@unchainedshop/logger": "^2.13.0",
34
34
  "abort-controller": "3.0.0",
35
35
  "hashids": "^2.3.0",
36
36
  "locale": "0.1.0",
37
37
  "simpl-schema": "^3.4.6"
38
38
  },
39
39
  "devDependencies": {
40
- "@types/node": "^20.14.10",
41
- "@unchainedshop/types": "^2.12.2",
40
+ "@types/node": "^20.14.12",
41
+ "@unchainedshop/types": "^2.13.0",
42
42
  "jest": "^29.7.0",
43
- "typescript": "^5.5.3"
43
+ "typescript": "^5.5.4"
44
44
  }
45
45
  }
@@ -1,9 +1,10 @@
1
+ import { Currency } from '@unchainedshop/types/currencies.js';
1
2
  import type { Locale as LocaleType, Locales as LocalesType } from 'locale';
2
3
  import localePkg from 'locale';
3
4
 
4
5
  const { Locales, Locale } = localePkg;
5
6
 
6
- const { UNCHAINED_LANG = 'de', UNCHAINED_COUNTRY = 'CH' } = process.env;
7
+ const { UNCHAINED_LANG = 'de', UNCHAINED_COUNTRY = 'CH', UNCHAINED_CURRENCY = 'CHF' } = process.env;
7
8
 
8
9
  export const systemLocale = new Locale(`${UNCHAINED_LANG}-${UNCHAINED_COUNTRY}`);
9
10
 
@@ -32,6 +33,22 @@ export const resolveBestCountry = (localeCountry, shopCountry, countries) => {
32
33
  return localeCountry || systemLocale.country;
33
34
  };
34
35
 
36
+ export const resolveBestCurrency = (localeCurrency, currencies: Array<Currency>) => {
37
+ if (localeCurrency) {
38
+ const resolvedCurrency = currencies.find((currency) => currency.isoCode === localeCurrency);
39
+ if (resolvedCurrency) {
40
+ return resolvedCurrency.isoCode;
41
+ }
42
+ }
43
+
44
+ const fallbackCurrency = currencies.find((currency) => currency.isoCode === UNCHAINED_CURRENCY);
45
+ if (fallbackCurrency) {
46
+ return fallbackCurrency.isoCode;
47
+ }
48
+
49
+ return currencies?.[0]?.isoCode || UNCHAINED_CURRENCY;
50
+ };
51
+
35
52
  export const resolveUserRemoteAddress = (req) => {
36
53
  const remoteAddress =
37
54
  req.headers['x-real-ip'] ||