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

Added undefined #10

Merged
merged 1 commit into from
Mar 18, 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
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare class ExchNumberFormat {
private intlOptions;
private customCurrencyData;
private originalCurrency;
constructor(locales: string, options?: RoundNumberOptions);
constructor(locales: string | undefined, options?: RoundNumberOptions);
format(number: number): string;
formatToParts(number: number): Intl.NumberFormatPart[];
private replaceCurrency;
Expand Down
48 changes: 44 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
class ExchNumberFormat {
constructor(locales, options = {}) {
this.customCurrencyData = {
'ADA': {
'symbol': '₳',
'narrowSymbol': 'ADA₳',
'code': 'ADA',
'name': 'Cardano',
'defaultDecimals': 2,
},
'BCH': {
'symbol': 'Ƀ',
'narrowSymbol': 'BCHɃ',
Expand All @@ -22,6 +29,20 @@ class ExchNumberFormat {
'name': 'CoreToken',
'defaultDecimals': 2,
},
'DOT': {
'symbol': '•',
'narrowSymbol': 'DOT•',
'code': 'DOT',
'name': 'Polkadot',
'defaultDecimals': 2,
},
'ETC': {
'symbol': 'ξ',
'narrowSymbol': 'ETCξ',
'code': 'ETC',
'name': 'EthereumClassic',
'defaultDecimals': 3,
},
'ETH': {
'symbol': 'Ξ',
'narrowSymbol': 'ETHΞ',
Expand All @@ -34,7 +55,21 @@ class ExchNumberFormat {
'narrowSymbol': 'LTCŁ',
'code': 'LTC',
'name': 'Litecoin',
'defaultDecimals': 4,
'defaultDecimals': 3,
},
'SOL': {
'symbol': 'S◎L',
'narrowSymbol': 'SOLS◎L',
'code': 'SOL',
'name': 'Solana',
'defaultDecimals': 2,
},
'TRX': {
'symbol': '₵',
'narrowSymbol': 'TRX₵',
'code': 'TRX',
'name': 'Tron',
'defaultDecimals': 2,
},
'USC': {
'symbol': 'Ⓢ',
Expand All @@ -55,14 +90,14 @@ class ExchNumberFormat {
'narrowSymbol': 'XCB₡',
'code': 'XCB',
'name': 'Core',
'defaultDecimals': 4,
'defaultDecimals': 3,
},
'XMR': {
'symbol': 'ɱ',
'narrowSymbol': 'XMRɱ',
'code': 'XMR',
'name': 'Monero',
'defaultDecimals': 4,
'defaultDecimals': 3,
},
'XRP': {
'symbol': '✕',
Expand All @@ -84,7 +119,7 @@ class ExchNumberFormat {
};
this.intlOptions = { ...defaultOptions, ...options };
this.originalCurrency = this.intlOptions.currency;
let setLocale;
let setLocale = undefined;
if (locales === 'auto') {
if (typeof window !== 'undefined' && navigator.languages && navigator.languages.length) {
setLocale = navigator.languages[0];
Expand All @@ -103,6 +138,11 @@ class ExchNumberFormat {
const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()];
this.intlOptions.minimumFractionDigits = currencyData.defaultDecimals;
}
else if (!this.originalCurrency) {
this.intlOptions.style = 'decimal';
this.intlOptions.minimumFractionDigits = 2;
this.intlOptions.maximumFractionDigits = 2;
}
this.formatter = new Intl.NumberFormat(setLocale, this.intlOptions);
}
format(number) {
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.6",
"version": "1.0.7",
"description": "Exchange Number Formatting",
"main": "dist/index.js",
"type": "module",
Expand Down
49 changes: 44 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ class ExchNumberFormat {
private customCurrencyData: { [key: string]: { symbol: string, narrowSymbol: string, code: string, name: string, defaultDecimals: number } };
private originalCurrency: string | undefined;

constructor(locales: string, options: RoundNumberOptions = {}) {
constructor(locales: string | undefined, options: RoundNumberOptions = {}) {
// Custom currency data
this.customCurrencyData = {
'ADA': {
'symbol': '₳',
'narrowSymbol': 'ADA₳',
'code': 'ADA',
'name': 'Cardano',
'defaultDecimals': 2,
},
'BCH': {
'symbol': 'Ƀ',
'narrowSymbol': 'BCHɃ',
Expand All @@ -32,6 +39,20 @@ class ExchNumberFormat {
'name': 'CoreToken',
'defaultDecimals': 2,
},
'DOT': {
'symbol': '•',
'narrowSymbol': 'DOT•',
'code': 'DOT',
'name': 'Polkadot',
'defaultDecimals': 2,
},
'ETC': {
'symbol': 'ξ',
'narrowSymbol': 'ETCξ',
'code': 'ETC',
'name': 'EthereumClassic',
'defaultDecimals': 3,
},
'ETH': {
'symbol': 'Ξ',
'narrowSymbol': 'ETHΞ',
Expand All @@ -44,7 +65,21 @@ class ExchNumberFormat {
'narrowSymbol': 'LTCŁ',
'code': 'LTC',
'name': 'Litecoin',
'defaultDecimals': 4,
'defaultDecimals': 3,
},
'SOL': {
'symbol': 'S◎L',
'narrowSymbol': 'SOLS◎L',
'code': 'SOL',
'name': 'Solana',
'defaultDecimals': 2,
},
'TRX': {
'symbol': '₵',
'narrowSymbol': 'TRX₵',
'code': 'TRX',
'name': 'Tron',
'defaultDecimals': 2,
},
'USC': {
'symbol': 'Ⓢ',
Expand All @@ -65,14 +100,14 @@ class ExchNumberFormat {
'narrowSymbol': 'XCB₡',
'code': 'XCB',
'name': 'Core',
'defaultDecimals': 4,
'defaultDecimals': 3,
},
'XMR': {
'symbol': 'ɱ',
'narrowSymbol': 'XMRɱ',
'code': 'XMR',
'name': 'Monero',
'defaultDecimals': 4,
'defaultDecimals': 3,
},
'XRP': {
'symbol': '✕',
Expand Down Expand Up @@ -101,7 +136,7 @@ class ExchNumberFormat {
this.originalCurrency = this.intlOptions.currency;

// Determine the locale
let setLocale: string;
let setLocale: string | undefined = undefined;
if (locales === 'auto') {
// Check if running in a browser environment
if (typeof window !== 'undefined' && navigator.languages && navigator.languages.length) {
Expand All @@ -118,6 +153,10 @@ class ExchNumberFormat {
if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) {
const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()];
this.intlOptions.minimumFractionDigits = currencyData.defaultDecimals;
} else if (!this.originalCurrency) {
this.intlOptions.style = 'decimal';
this.intlOptions.minimumFractionDigits = 2;
this.intlOptions.maximumFractionDigits = 2;
}

// Create an Intl.NumberFormat instance
Expand Down
18 changes: 14 additions & 4 deletions test/roundNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Added currencies', () => {
currencyDisplay: 'name'
});
const result = formatter.format(1234.1234567899);
expect(result).toBe('1 234,1234 Core');
expect(result).toBe('1 234,123 Core');
});

it('XCB with symbol, locale en', () => {
Expand All @@ -30,7 +30,7 @@ describe('Added currencies', () => {
currencyDisplay: 'symbol'
});
const result = formatter.format(1234.1234567899);
expect(result).toBe('₡ 1,234.1235');
expect(result).toBe('₡ 1,234.123');
});

it('USDC with narrowSymbol, locale it-CH currency and default 2 decimals', () => {
Expand Down Expand Up @@ -142,7 +142,17 @@ describe('Different locales', () => {
currency: 'LTC'
});
const result = formatter.format(1234.1234567899);
expect(result).toBe('Ł 1.234,1235');
expect(result).toBe('Ł 1.234,123');
});
});

describe('Defaults', () => {
it('Default locale and no currency', () => {
const formatter = new ExchNumberFormat(undefined, {
style: 'currency'
});
const result = formatter.format(1234.1234567899);
expect(result).toBe('1,234.12');
});
});

Expand Down Expand Up @@ -179,6 +189,6 @@ describe('Decomposed', () => {
currencyDisplay: 'symbol'
});
const result = formatter.formatToParts(1234.1234567899);
expect(result).toEqual([{"type": "currency", "value": "₡"}, {"type": "literal", "value": " "}, {"type": "integer", "value": "1"}, {"type": "group", "value": ","}, {"type": "integer", "value": "234"}, {"type": "decimal", "value": "."}, {"type": "fraction", "value": "1234"}]);
expect(result).toEqual([{"type": "currency", "value": "₡"}, {"type": "literal", "value": " "}, {"type": "integer", "value": "1"}, {"type": "group", "value": ","}, {"type": "integer", "value": "234"}, {"type": "decimal", "value": "."}, {"type": "fraction", "value": "123"}]);
});
});
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export declare class ExchNumberFormat {
private customCurrencyData: { [key: string]: CurrencyData };
private originalCurrency: string | undefined;

constructor(locales: string, options?: RoundNumberOptions);
constructor(locales: string | undefined, options?: RoundNumberOptions);

format(number: number): string;
formatToParts(number: number): Intl.NumberFormatPart[];
Expand Down
Loading