Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigator ready but optional #8

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading