Skip to content

Commit

Permalink
Navigator ready but optional
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislavcore committed Mar 12, 2024
1 parent e6bba2f commit ebd5518
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
16 changes: 15 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"strict": true,
"typeRoots": ["./node_modules/@types", "./types"],
"removeComments": true,
"esModuleInterop": true,
"declaration": true
},
"include": ["src/**/*.ts"],
Expand Down
1 change: 1 addition & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "Node16",
"esModuleInterop": true,
"outDir": "./dist-test"
},
"include": ["src/**/*.ts", "test/**/*.ts"],
Expand Down

0 comments on commit ebd5518

Please sign in to comment.