Skip to content

Commit

Permalink
Merge pull request #14 from bchainhub/update/build-02
Browse files Browse the repository at this point in the history
Update/build 02
  • Loading branch information
rastislavcore authored Apr 12, 2024
2 parents 48b62a8 + 627da10 commit e06236d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 41 deletions.
20 changes: 18 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ interface RoundNumberOptions extends Intl.NumberFormatOptions {
digitized?: boolean;
digitizedSymbol?: string;
useAliases?: boolean;
aliases?: {
[key: string]: string;
};
useCustomCurrency?: boolean;
customCurrency?: {
[key: string]: {
symbol: string;
narrowSymbol: string;
code: string;
name: string;
defaultDecimals: number;
};
};
}
declare class ExchNumberFormat {
version: string;
private replacer;
private formatter;
private intlOptions;
private customCurrencyData;
private aliases;
private totalCurrencyData;
private originalCurrency;
constructor(locales: string | undefined, options?: RoundNumberOptions);
private totalAliases;
private internalAliases;
constructor(locales?: string | undefined, options?: RoundNumberOptions);
format(number: number): string;
formatToParts(number: number): Intl.NumberFormatPart[];
isCurrencySupported(currency: string): boolean;
Expand Down
114 changes: 83 additions & 31 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class ExchNumberFormat {
constructor(locales, options = {}) {
this.version = '1.1.1';
this.version = '1.1.2';
this.replacer = 'XYZ';
this.totalCurrencyData = {};
this.totalAliases = {};
this.internalAliases = {};
this.customCurrencyData = {
'ADA': {
'symbol': '₳',
Expand Down Expand Up @@ -86,13 +90,27 @@ class ExchNumberFormat {
'name': 'Tether',
'defaultDecimals': 2,
},
'XAB': {
'symbol': 't₡',
'narrowSymbol': 't₡',
'code': 'XAB',
'name': 'CoreDevin',
'defaultDecimals': 3,
},
'XCB': {
'symbol': '₡',
'narrowSymbol': '₡',
'code': 'XCB',
'name': 'Core',
'defaultDecimals': 3,
},
'XCE': {
'symbol': '₡',
'narrowSymbol': '₡',
'code': 'XCE',
'name': 'CoreKoliba',
'defaultDecimals': 3,
},
'XMR': {
'symbol': 'ɱ',
'narrowSymbol': 'ɱ',
Expand All @@ -115,7 +133,7 @@ class ExchNumberFormat {
'defaultDecimals': 2,
},
};
this.aliases = {};
this.internalAliases = {};
const defaultOptions = {
localeMatcher: 'best fit',
style: 'currency',
Expand All @@ -130,20 +148,36 @@ class ExchNumberFormat {
digitized: false,
digitizedSymbol: 'd',
useAliases: true,
aliases: {},
useCustomCurrency: true,
customCurrency: {},
};
this.intlOptions = { ...defaultOptions, ...options };
this.totalAliases = { ...this.intlOptions.aliases, ...this.internalAliases };
if (this.intlOptions.useCustomCurrency) {
this.totalCurrencyData = { ...this.customCurrencyData, ...this.intlOptions.customCurrency };
}
else {
this.totalCurrencyData = {};
}
this.originalCurrency = this.intlOptions.currency;
if (this.intlOptions.useAliases && this.originalCurrency && this.aliases[this.originalCurrency]) {
this.originalCurrency = this.aliases[this.originalCurrency];
if (this.intlOptions.useAliases && this.originalCurrency && this.totalAliases[this.originalCurrency]) {
this.originalCurrency = this.totalAliases[this.originalCurrency];
}
let setLocale = locales === 'auto' ? this.determineLocale() : locales;
const currencyData = this.customCurrencyData[this.originalCurrency?.toUpperCase() || ''];
const currencyData = this.totalCurrencyData[this.originalCurrency?.toUpperCase() || ''];
if (this.originalCurrency && currencyData) {
this.intlOptions.currency = 'XYZ';
this.intlOptions.currency = this.replacer;
this.intlOptions.minimumFractionDigits = currencyData.defaultDecimals;
}
try {
this.formatter = new Intl.NumberFormat(setLocale, this.intlOptions);
if (this.intlOptions.currency) {
this.formatter = new Intl.NumberFormat(setLocale, this.intlOptions);
}
else {
this.useDecimalStyle();
this.formatter = new Intl.NumberFormat(setLocale, this.intlOptions);
}
}
catch (error) {
if (error instanceof RangeError) {
Expand All @@ -152,6 +186,7 @@ class ExchNumberFormat {
}
else {
console.error("Error creating Intl.NumberFormat instance");
return;
}
}
}
Expand All @@ -161,24 +196,18 @@ class ExchNumberFormat {
}
formatToParts(number) {
const parts = this.formatter.formatToParts(number);
if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) {
const originalCurrency = this.originalCurrency.toUpperCase();
const currencyData = this.customCurrencyData[originalCurrency];
let symbolToReplace = this.addType(currencyData.symbol);
switch (this.intlOptions.currencyDisplay) {
case 'narrowSymbol':
symbolToReplace = this.addType(currencyData.narrowSymbol);
break;
case 'code':
symbolToReplace = this.addType(currencyData.code);
break;
case 'name':
symbolToReplace = this.addType(currencyData.name);
break;
}
if (this.originalCurrency && this.totalCurrencyData[this.originalCurrency.toUpperCase()]) {
const currencyData = this.totalCurrencyData[this.originalCurrency.toUpperCase()];
parts.forEach(part => {
if (part.type === 'currency') {
part.value = part.value.replace(this.replacer, this.addType(currencyData.symbol));
}
});
}
else if (this.originalCurrency) {
parts.forEach(part => {
if (part.type === 'currency') {
part.value = symbolToReplace;
part.value = this.addType(part.value);
}
});
}
Expand All @@ -190,12 +219,18 @@ class ExchNumberFormat {
return true;
}
catch (error) {
return false;
if (error instanceof RangeError) {
return false;
}
else {
console.warn("Error creating Intl.NumberFormat instance");
return false;
}
}
}
replaceCurrency(formattedString) {
if (this.originalCurrency && this.customCurrencyData[this.originalCurrency.toUpperCase()]) {
const currencyData = this.customCurrencyData[this.originalCurrency.toUpperCase()];
replaceCurrency(input) {
if (this.originalCurrency && this.totalCurrencyData[this.originalCurrency.toUpperCase()]) {
const currencyData = this.totalCurrencyData[this.originalCurrency.toUpperCase()];
let replacementValue;
switch (this.intlOptions.currencyDisplay) {
case 'narrowSymbol':
Expand All @@ -211,9 +246,26 @@ class ExchNumberFormat {
replacementValue = this.addType(currencyData.symbol);
break;
}
return formattedString.replace('XYZ', replacementValue);
return input.replace(this.replacer, replacementValue);
}
else if (this.originalCurrency) {
switch (this.intlOptions.currencyDisplay) {
case 'narrowSymbol':
input = this.addType(input);
break;
case 'code':
input = input.replace(this.originalCurrency, this.addType(this.originalCurrency));
break;
case 'name':
input = this.addType(input);
break;
default:
input = this.addType(input);
break;
}
return input;
}
return formattedString;
return input;
}
useDecimalStyle() {
this.intlOptions.style = 'decimal';
Expand All @@ -232,8 +284,8 @@ class ExchNumberFormat {
}
addType(value) {
let output = value;
if (this.intlOptions.digitalized) {
output = this.intlOptions.digitalizedSymbol + output;
if (this.intlOptions.digitized) {
output = this.intlOptions.digitizedSymbol + output;
}
if (this.intlOptions.wrapped) {
output = this.intlOptions.wrappedSymbol + output;
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.1.1",
"version": "1.1.2",
"description": "Exchange Number Formatting",
"main": "dist/index.js",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface RoundNumberOptions extends Intl.NumberFormatOptions {
}

class ExchNumberFormat {
version: string = '1.1.1';
version: string = '1.1.2';
private replacer: string = 'XYZ';
private formatter!: Intl.NumberFormat;
private intlOptions: RoundNumberOptions;
Expand Down
12 changes: 6 additions & 6 deletions test/roundNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,6 @@ describe('Decomposed', () => {
});

describe('Additional', () => {
it('Print current version', () => {
const formatter = new ExchNumberFormat();
const result = formatter.version;
expect(result).toBe('1.1.1');
});

it('Check if currency is supported - XCB should be', () => {
const formatter = new ExchNumberFormat(undefined);
const result = formatter.isCurrencySupported('xcb');
Expand All @@ -358,4 +352,10 @@ describe('Additional', () => {
const result = formatter.format(1234.1234567899);
expect(result).toBe('¤ 1,234.12');
});

it('Print current version', () => {
const formatter = new ExchNumberFormat();
const result = formatter.version;
expect(result).toBe('1.1.2');
});
});

0 comments on commit e06236d

Please sign in to comment.