Skip to content

Commit 83f9baa

Browse files
vkachanMikhail Bashkirov
authored andcommitted
fix(input-amount): support negative numbers
1 parent 0b8785c commit 83f9baa

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

packages/input-amount/src/parsers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function getParseMode(value) {
6161
*/
6262
function parseWithLocale(value, options) {
6363
const separator = getDecimalSeparator(options);
64-
const regexNumberAndLocaleSeparator = new RegExp(`[0-9${separator}]`, 'g');
64+
const regexNumberAndLocaleSeparator = new RegExp(`[0-9${separator}-]`, 'g');
6565
let numberAndLocaleSeparator = value.match(regexNumberAndLocaleSeparator).join('');
6666
if (separator === ',') {
6767
numberAndLocaleSeparator = numberAndLocaleSeparator.replace(',', '.');
@@ -110,7 +110,7 @@ function parseHeuristic(value) {
110110
* @param {object} options Locale Options
111111
*/
112112
export function parseAmount(value, options) {
113-
const matchedInput = value.match(/[0-9,. ]/g);
113+
const matchedInput = value.match(/[0-9,.\- ]/g);
114114
if (!matchedInput) {
115115
return undefined;
116116
}

packages/input-amount/stories/index.stories.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ storiesOf('Forms|Input Amount', module)
1010
</lion-input-amount>
1111
`,
1212
)
13+
.add(
14+
'Negative number',
15+
() => html`
16+
<lion-input-amount
17+
.errorValidators="${['required']}"
18+
label="Amount"
19+
.modelValue=${-123456.78}
20+
>
21+
</lion-input-amount>
22+
`,
23+
)
1324
.add(
1425
'Set USD as currency',
1526
() => html`

packages/input-amount/test/formatters.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ describe('formatAmount()', () => {
5454
maximumFractionDigits: 3,
5555
}),
5656
).to.equal('12.346');
57+
expect(
58+
formatAmount(-12.345678, {
59+
locale: 'en-GB',
60+
minimumFractionDigits: 2,
61+
maximumFractionDigits: 2,
62+
}),
63+
).to.equal('-12.35');
5764
});
5865

5966
it('formats the right amount of fraction digits for a certain currency', async () => {

packages/input-amount/test/parsers.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,27 @@ describe('parseAmount()', () => {
112112
expect(parseAmount('1 23.4')).to.equal(123.4);
113113
});
114114

115+
it('parses negative numbers', () => {
116+
expect(parseAmount('-0')).to.equal(0);
117+
expect(parseAmount('-1')).to.equal(-1);
118+
expect(parseAmount('-1234')).to.equal(-1234);
119+
expect(parseAmount('-1.234,5')).to.equal(-1234.5);
120+
expect(parseAmount('-1,234.5')).to.equal(-1234.5);
121+
expect(parseAmount('-1.234,5678')).to.equal(-1234.5678);
122+
expect(parseAmount('-1,234.5678')).to.equal(-1234.5678);
123+
});
124+
115125
it('ignores all non-number symbols (including currency)', () => {
116126
expect(parseAmount('€ 1,234.56')).to.equal(1234.56);
127+
expect(parseAmount('€ -1,234.56')).to.equal(-1234.56);
128+
expect(parseAmount('-€ 1,234.56')).to.equal(-1234.56);
117129
expect(parseAmount('1,234.56 €')).to.equal(1234.56);
130+
expect(parseAmount('-1,234.56 €')).to.equal(-1234.56);
118131
expect(parseAmount('EUR 1,234.56')).to.equal(1234.56);
132+
expect(parseAmount('EUR -1,234.56')).to.equal(-1234.56);
133+
expect(parseAmount('-EUR 1,234.56')).to.equal(-1234.56);
119134
expect(parseAmount('1,234.56 EUR')).to.equal(1234.56);
135+
expect(parseAmount('-1,234.56 EUR')).to.equal(-1234.56);
120136
expect(parseAmount('Number is 1,234.56')).to.equal(1234.56);
121137
});
122138

0 commit comments

Comments
 (0)