diff --git a/dist/index.js b/dist/index.js index c6de35c..1d59e53 100644 --- a/dist/index.js +++ b/dist/index.js @@ -84,7 +84,21 @@ class ExchNumberFormat { }; this.intlOptions = { ...defaultOptions, ...options }; this.originalCurrency = this.intlOptions.currency; - let setLocale = locales === 'auto' ? (navigator.languages && navigator.languages.length ? navigator.languages[0] : navigator.language) : locales; + let setLocale; + if (locales === 'auto') { + if (typeof window !== 'undefined' && navigator.languages && navigator.languages.length) { + setLocale = navigator.languages[0]; + } + else if (typeof window !== 'undefined' && navigator.language) { + setLocale = navigator.language; + } + else { + setLocale = 'en'; + } + } + else { + setLocale = locales; + } if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) { const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()]; this.intlOptions.minimumFractionDigits = currencyData.defaultDecimals; diff --git a/package.json b/package.json index e3c69b4..d8d9c2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "exchange-rounding", - "version": "1.0.4", + "version": "1.0.5", "description": "Exchange Number Formatting", "main": "dist/index.js", "type": "module", diff --git a/src/index.ts b/src/index.ts index efdb794..af57dc8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -102,7 +102,19 @@ class ExchNumberFormat { this.originalCurrency = this.intlOptions.currency; // Determine the locale - let setLocale = locales === 'auto' ? (navigator.languages && navigator.languages.length ? navigator.languages[0] : navigator.language) : locales; + let setLocale: string; + if (locales === 'auto') { + // Check if running in a browser environment + if (typeof window !== 'undefined' && navigator.languages && navigator.languages.length) { + setLocale = navigator.languages[0]; + } else if (typeof window !== 'undefined' && navigator.language) { + setLocale = navigator.language; + } else { + setLocale = 'en'; // Default to 'en' or any other default locale + } + } else { + setLocale = locales; + } if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) { const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()]; diff --git a/tsconfig.json b/tsconfig.json index 9264567..cde644f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "strict": true, "typeRoots": ["./node_modules/@types", "./types"], "removeComments": true, + "esModuleInterop": true, "declaration": true }, "include": ["src/**/*.ts"], diff --git a/tsconfig.test.json b/tsconfig.test.json index 5ac4d30..d3774e7 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -2,6 +2,7 @@ "extends": "./tsconfig.json", "compilerOptions": { "module": "Node16", + "esModuleInterop": true, "outDir": "./dist-test" }, "include": ["src/**/*.ts", "test/**/*.ts"],