Skip to content

Commit

Permalink
Uppercase fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislavcore committed Mar 12, 2024
1 parent b5ba159 commit bdc9747
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import ExchNumberFormat from 'exchange-rounding';
const bitcoinFormatter = new ExchNumberFormat('en-US', {
style: 'currency',
currency: 'BTC',
customCurrency: 'BTC',
roundingMode: 'halfFloor',
currencyDisplay: 'symbol'
});
Expand Down Expand Up @@ -69,7 +68,6 @@ Creates a new `ExchNumberFormat` instance.
#### RoundNumberOptions

- Inherits all options from `Intl.NumberFormatOptions`.
- `customCurrency` (String): Specify a custom currency code, e.g., 'BTC', 'ETH'.
- `roundingMode` (String): Rounding mode according to the `Intl.NumberFormat` documentation.

### format(number)
Expand Down
1 change: 0 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
interface RoundNumberOptions extends Intl.NumberFormatOptions {
customCurrency?: string;
roundingMode?: 'ceil' | 'floor' | 'expand' | 'trunc' | 'halfCeil' | 'halfFloor' | 'halfExpand' | 'halfTrunc' | 'halfEven';
}
declare class ExchNumberFormat {
Expand Down
12 changes: 7 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class ExchNumberFormat {
'narrowSymbol': 'ETHΞ',
'code': 'ETH',
'name': 'Ethereum',
'defaultDecimals': 6,
'defaultDecimals': 4,
},
'LTC': {
'symbol': 'Ł',
'narrowSymbol': 'LTCŁ',
'code': 'LTC',
'name': 'Litecoin',
'defaultDecimals': 8,
'defaultDecimals': 4,
},
'USC': {
'symbol': 'Ⓢ',
Expand Down Expand Up @@ -112,7 +112,8 @@ class ExchNumberFormat {
formatToParts(number) {
const parts = this.formatter.formatToParts(number);
if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) {
const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()];
const originalCurrency = this.originalCurrency.toUpperCase();
const currencyData = this.customCurrencyData[originalCurrency];
let symbolToReplace = currencyData.symbol;
switch (this.intlOptions.currencyDisplay) {
case 'narrowSymbol':
Expand All @@ -135,7 +136,8 @@ class ExchNumberFormat {
}
replaceCurrency(formattedString) {
if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) {
const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()];
const originalCurrency = this.originalCurrency.toUpperCase();
const currencyData = this.customCurrencyData[originalCurrency];
let symbolToReplace = currencyData.symbol;
switch (this.intlOptions.currencyDisplay) {
case 'narrowSymbol':
Expand All @@ -148,7 +150,7 @@ class ExchNumberFormat {
symbolToReplace = currencyData.name;
break;
}
return formattedString.replace(this.originalCurrency, symbolToReplace);
return formattedString.replace(originalCurrency, symbolToReplace);
}
return formattedString;
}
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.5",
"version": "1.0.6",
"description": "Exchange Number Formatting",
"main": "dist/index.js",
"type": "module",
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
interface RoundNumberOptions extends Intl.NumberFormatOptions {
customCurrency?: string;
roundingMode?: 'ceil' | 'floor' | 'expand' | 'trunc' | 'halfCeil' | 'halfFloor' | 'halfExpand' | 'halfTrunc' | 'halfEven';
}

Expand Down Expand Up @@ -38,14 +37,14 @@ class ExchNumberFormat {
'narrowSymbol': 'ETHΞ',
'code': 'ETH',
'name': 'Ethereum',
'defaultDecimals': 6,
'defaultDecimals': 4,
},
'LTC': {
'symbol': 'Ł',
'narrowSymbol': 'LTCŁ',
'code': 'LTC',
'name': 'Litecoin',
'defaultDecimals': 8,
'defaultDecimals': 4,
},
'USC': {
'symbol': 'Ⓢ',
Expand Down Expand Up @@ -134,7 +133,8 @@ class ExchNumberFormat {
const parts = this.formatter.formatToParts(number);

if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) {
const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()];
const originalCurrency = this.originalCurrency.toUpperCase();
const currencyData = this.customCurrencyData[originalCurrency];
let symbolToReplace = currencyData.symbol;

switch (this.intlOptions.currencyDisplay) {
Expand All @@ -161,7 +161,8 @@ class ExchNumberFormat {

private replaceCurrency(formattedString: string): string {
if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) {
const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()];
const originalCurrency = this.originalCurrency.toUpperCase();
const currencyData = this.customCurrencyData[originalCurrency];
let symbolToReplace = currencyData.symbol;
switch (this.intlOptions.currencyDisplay) {
case 'narrowSymbol':
Expand All @@ -174,7 +175,7 @@ class ExchNumberFormat {
symbolToReplace = currencyData.name;
break;
}
return formattedString.replace(this.originalCurrency, symbolToReplace);
return formattedString.replace(originalCurrency, symbolToReplace);
}
return formattedString;
}
Expand Down
23 changes: 22 additions & 1 deletion test/roundNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ describe('Added currencies', () => {
expect(result).toBe('1 234,1234 Core');
});

it('XCB with symbol, locale en', () => {
const formatter = new ExchNumberFormat('en', {
style: 'currency',
currency: 'xcb',
currencyDisplay: 'symbol'
});
const result = formatter.format(1234.1234567899);
expect(result).toBe('₡ 1,234.1235');
});

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

Expand Down Expand Up @@ -160,4 +170,15 @@ describe('Decomposed', () => {
const result = formatter.formatToParts(1234.1234567899);
expect(result).toEqual([{"type": "currency", "value": "USD"}, {"type": "literal", "value": " "}, {"type": "integer", "value": "001"}, {"type": "group", "value": "’"}, {"type": "integer", "value": "234"}, {"type": "decimal", "value": "."}, {"type": "fraction", "value": "1235"}]);
});

it('XCB with symbol, locale en currency and default 4 decimals', () => {
const formatter = new ExchNumberFormat('en', {
style: 'currency',
currency: 'xcb',
roundingMode: 'floor',
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"}]);
});
});
1 change: 0 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface RoundNumberOptions extends Intl.NumberFormatOptions {
customCurrency?: string;
roundingMode?: 'ceil' | 'floor' | 'expand' | 'trunc' | 'halfCeil' | 'halfFloor' | 'halfExpand' | 'halfTrunc' | 'halfEven';
}

Expand Down

0 comments on commit bdc9747

Please sign in to comment.