Skip to content

Commit 187582a

Browse files
committed
fix: make numberFormat always return a string
1 parent 0e87ea9 commit 187582a

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

tests/util/index.spec.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,38 @@ describe('Utility Functions', () => {
4242
});
4343

4444
describe('numberFormat', () => {
45-
it('should return false for invalid currency code', () => {
45+
it('returns the plain number as string for an invalid currency code', () => {
4646
const result = numberFormat('INVALID', 100);
47-
expect(result).to.equal(false);
47+
expect(result).to.equal('100');
4848
});
4949

50-
it('should return original number if no locale found', () => {
50+
it('formats the number using the currency locale', () => {
5151
const result = numberFormat('USD', 1234.56);
52-
expect(result).to.not.be.false;
52+
expect(result).to.equal('1,234.56');
5353
});
5454

55-
it('should return original number for NaN input', () => {
55+
it('returns a string for NaN input', () => {
5656
const result = numberFormat('USD', NaN);
57-
expect(result).to.be.NaN;
57+
expect(result).to.equal('NaN');
5858
});
5959

60-
it('should handle zero correctly', () => {
60+
it('handles zero correctly', () => {
6161
const result = numberFormat('USD', 0);
62-
expect(result).to.not.be.false;
62+
expect(result).to.equal('0');
6363
});
6464

65-
it('should handle negative numbers', () => {
65+
it('handles negative numbers', () => {
6666
const result = numberFormat('USD', -100);
67-
expect(result).to.not.be.false;
67+
expect(result).to.equal('-100');
68+
});
69+
70+
it('always returns a string', () => {
71+
const results = [
72+
numberFormat('INVALID', 100),
73+
numberFormat('USD', 1000),
74+
numberFormat('USD', NaN),
75+
];
76+
results.forEach(result => expect(result).to.be.a('string'));
6877
});
6978
});
7079

util/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,17 @@ const plural = (n: number): string => {
6363
exports.plural = plural;
6464

6565
// This function formats a number to locale strings.
66-
// If Iso code or locale code doesn´t exist, the function will return a number without format.
67-
const numberFormat = (code: string, number: number) => {
68-
if (!isIso4217(code)) return false;
66+
// If Iso code or locale code doesn´t exist, the function will return the number as a plain string.
67+
const numberFormat = (code: string, number: number): string => {
68+
if (!isIso4217(code)) return String(number);
6969

70-
if (!currencies[code]) return number;
70+
if (!currencies[code]) return String(number);
7171

7272
const locale = currencies[code].locale;
73-
const numberToLocaleString = Intl.NumberFormat(locale);
7473

75-
if (!locale || isNaN(number)) return number;
74+
if (!locale || isNaN(number)) return String(number);
7675

77-
return numberToLocaleString.format(number);
76+
return Intl.NumberFormat(locale).format(number);
7877
};
7978

8079
// This function checks if the current buyer and seller were doing circular operations

0 commit comments

Comments
 (0)