Skip to content

Commit c7f4b0b

Browse files
committed
Allow parsing of strings as numbers
1 parent 9f43bd1 commit c7f4b0b

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

demo/examples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default [
1818
{
1919
description: 'Japanese yen doesn\'t use a minor unit',
2020
numberFormat: new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }),
21-
defaultValue: -123456,
21+
defaultValue: '-1234',
2222
},
2323
{
2424
description: 'Percent',

src/abstract-number-format-input/__test__/index.test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ const simpleAllowNull = formattedNumber(numberFormat, true);
99
describe('format', () => {
1010
it('formats null, undefined, empty string as empty string', () => {
1111
[undefined, null, '', undefined].forEach(value =>
12-
expect(simpleAllowNull.format(value)).toBe('')
12+
expect(simpleAllowNull.format(value)).toBe('')
1313
);
1414
});
1515

16-
it('Throws if value is not a finite number', () => {
17-
[false, 'booya', '123', true].forEach(value =>
18-
expect(simpleAllowNull.format.bind(null, value)).toThrow('Invariant')
19-
);
16+
it('parses strings before formatting them', () => {
17+
expect(simpleAllowNull.format('123')).toBe('123.00');
18+
});
19+
20+
it('Throws if value is not a finite number or string number', () => {
21+
// TODO: '2booya2' should throw.
22+
[false, '2-2', true].forEach(value => {
23+
expect(simpleAllowNull.format.bind(null, value)).toThrow('Invariant');
24+
});
2025
});
2126

2227
it('Formats a finite number to a string using numberFormat', () => {
@@ -86,4 +91,3 @@ describe('flipSign', () => {
8691
expect(simpleFormatted.flipSign( '-1.23')).toBe(1.23);
8792
});
8893
});
89-

src/abstract-number-format-input/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export default function formattedNumberReducer(numberFormat) {
4747
}
4848

4949
function format(number) {
50-
if (number === null || number === undefined || number === '') return '';
50+
if (number === null || number === undefined) return '';
51+
if (typeof number === 'string') return format(parse(number));
5152
invariant(Number.isFinite(number), `Illegal number value: ${JSON.stringify(number)}`);
5253
return addTrailingZeros(numberFormat.format(number));
5354
}

src/abstract-number-format-input/util/__test__/parseNumber.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe('parse', () => {
4949
});
5050

5151
it('throws when magnitude is unparsable', () => {
52+
// TODO: '2booya2' should throw.
5253
['-', '.', '-.', '3-3', '3.3-'].forEach(val => {
5354
expect(parseNumber.bind(null, val, defaultDecimalChar)).toThrow('Invariant');
5455
});

0 commit comments

Comments
 (0)