Skip to content
Snippets Groups Projects
Unverified Commit ec3475d8 authored by Julian Wachholz's avatar Julian Wachholz Committed by GitHub
Browse files

Fix number formatting with non-breaking space (#2981)

parent 5ea9c587
No related branches found
No related tags found
No related merge requests found
import { evalArithmetic } from './arithmetic';
import { setNumberFormat } from './util';
describe('arithmetic', () => {
test('handles negative numbers', () => {
......@@ -40,5 +41,8 @@ describe('arithmetic', () => {
test('respects current number format', () => {
expect(evalArithmetic('1,222.45')).toEqual(1222.45);
setNumberFormat({ format: 'space-comma', hideFraction: false });
expect(evalArithmetic('1\xa0222,45')).toEqual(1222.45);
});
});
......@@ -38,7 +38,7 @@ function parsePrimary(state) {
}
let numberStr = '';
while (char(state) && char(state).match(/[0-9,. ]|\p{Sc}/u)) {
while (char(state) && char(state).match(/[0-9,.\xa0 ]|\p{Sc}/u)) {
numberStr += next(state);
}
......
......@@ -84,15 +84,4 @@ describe('utility functions', () => {
formatter = getNumberFormat().formatter;
expect(formatter.format(Number('1234.56'))).toBe('1\xa0235');
});
test('number formatting works with space-dot format', () => {
setNumberFormat({ format: 'space-dot', hideFraction: false });
let formatter = getNumberFormat().formatter;
// grouping separator space char is a non-breaking space, or UTF-16 \xa0
expect(formatter.format(Number('1234.56'))).toBe('1\xa0234.56');
setNumberFormat({ format: 'space-dot', hideFraction: true });
formatter = getNumberFormat().formatter;
expect(formatter.format(Number('1234.56'))).toBe('1\xa0235');
});
});
......@@ -217,12 +217,7 @@ export function appendDecimals(
return amountToCurrency(currencyToAmount(result));
}
type NumberFormats =
| 'comma-dot'
| 'dot-comma'
| 'space-comma'
| 'space-dot'
| 'comma-dot-in';
type NumberFormats = 'comma-dot' | 'dot-comma' | 'space-comma' | 'comma-dot-in';
export const numberFormats: Array<{
value: NumberFormats;
......@@ -231,8 +226,7 @@ export const numberFormats: Array<{
}> = [
{ value: 'comma-dot', label: '1,000.33', labelNoFraction: '1,000' },
{ value: 'dot-comma', label: '1.000,33', labelNoFraction: '1.000' },
{ value: 'space-comma', label: '1 000,33', labelNoFraction: '1 000' },
{ value: 'space-dot', label: '1 000.33', labelNoFraction: '1 000' },
{ value: 'space-comma', label: '1\xa0000,33', labelNoFraction: '1\xa0000' },
{ value: 'comma-dot-in', label: '1,00,000.33', labelNoFraction: '1,00,000' },
];
......@@ -269,12 +263,6 @@ export function getNumberFormat({
regex = /[^-0-9,]/g;
separator = ',';
break;
case 'space-dot':
locale = 'dje';
regex = /[^-0-9,.]/g;
separator = '.';
separatorRegex = /[,.]/g;
break;
case 'comma-dot-in':
locale = 'en-IN';
regex = /[^-0-9.]/g;
......
---
category: Bugfix
authors: [julianwachholz]
---
Fix number parsing with and remove unsupported "space-dot" format
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment