Skip to content
Snippets Groups Projects
Unverified Commit b8dbec46 authored by Jed Fox's avatar Jed Fox Committed by GitHub
Browse files

Revert “Make number parsing agnostic to decimal and thousands separators” (#1144)

This reverts #1029. As raised in #1097, the formatting chosen doesn’t
work well when doing math. There may be a way to balance compatibility
with multiple format styles with handling non-currency amounts
correctly, but it will require some more careful consideration. Re-opens
#894.
parent a86fd9cf
No related branches found
No related tags found
No related merge requests found
...@@ -91,12 +91,6 @@ export class AccountPage { ...@@ -91,12 +91,6 @@ export class AccountPage {
return new CloseAccountModal(this.page.locator('css=[aria-modal]')); return new CloseAccountModal(this.page.locator('css=[aria-modal]'));
} }
async _clearFocusedField() {
let isMac = process.platform === 'darwin';
await this.page.keyboard.press(isMac ? 'Meta+A' : 'Control+A');
await this.page.keyboard.press('Backspace');
}
async _fillTransactionFields(transactionRow, transaction) { async _fillTransactionFields(transactionRow, transaction) {
if (transaction.payee) { if (transaction.payee) {
await transactionRow.getByTestId('payee').click(); await transactionRow.getByTestId('payee').click();
...@@ -123,14 +117,12 @@ export class AccountPage { ...@@ -123,14 +117,12 @@ export class AccountPage {
if (transaction.debit) { if (transaction.debit) {
await transactionRow.getByTestId('debit').click(); await transactionRow.getByTestId('debit').click();
await this._clearFocusedField();
await this.page.keyboard.type(transaction.debit); await this.page.keyboard.type(transaction.debit);
await this.page.keyboard.press('Tab'); await this.page.keyboard.press('Tab');
} }
if (transaction.credit) { if (transaction.credit) {
await transactionRow.getByTestId('credit').click(); await transactionRow.getByTestId('credit').click();
await this._clearFocusedField();
await this.page.keyboard.type(transaction.credit); await this.page.keyboard.type(transaction.credit);
await this.page.keyboard.press('Tab'); await this.page.keyboard.press('Tab');
} }
......
export function generateTestCases(
configurableFormats: string[],
inputFormats: Array<{
name: string;
tests: Array<{ places: number; input: string; expected: number }>;
}>,
) {
let cases = [];
for (let configurableFormat of configurableFormats) {
for (let inputFormat of inputFormats) {
for (let test of inputFormat.tests) {
cases.push([
configurableFormat,
inputFormat.name,
test.places,
test.input,
test.expected,
]);
}
}
}
return cases;
}
import { generateTestCases } from '../mocks/number-formats';
import evalArithmetic from './arithmetic'; import evalArithmetic from './arithmetic';
import { setNumberFormat } from './util';
describe('arithmetic', () => { describe('arithmetic', () => {
test('handles negative numbers', () => { test('handles negative numbers', () => {
...@@ -44,76 +41,4 @@ describe('arithmetic', () => { ...@@ -44,76 +41,4 @@ describe('arithmetic', () => {
test('respects current number format', () => { test('respects current number format', () => {
expect(evalArithmetic('1,222.45')).toEqual(1222.45); expect(evalArithmetic('1,222.45')).toEqual(1222.45);
}); });
let configurableFormats = [
'dot-comma',
'comma-dot',
'space-comma',
'space-dot',
];
let inputFormats = [
{
name: 'dot-comma',
tests: [
{ places: 3, input: '1.234.567', expected: 1234567 },
{ places: 2, input: '1.234,56', expected: 1234.56 },
{ places: 1, input: '1.234,5', expected: 1234.5 },
{ places: 0, input: '1.234,', expected: 1234.0 },
],
},
{
name: 'comma-dot',
tests: [
{ places: 3, input: '1,234,567', expected: 1234567 },
{ places: 2, input: '1,234.56', expected: 1234.56 },
{ places: 1, input: '1,234.5', expected: 1234.5 },
{ places: 0, input: '1,234.', expected: 1234.0 },
],
},
{
name: 'dot-dot',
tests: [
{ places: 3, input: '1.234.567', expected: 1234567 },
{ places: 2, input: '1.234.56', expected: 1234.56 },
{ places: 1, input: '1.234.5', expected: 1234.5 },
{ places: 0, input: '1.234.', expected: 1234.0 },
],
},
{
name: 'comma-comma',
tests: [
{ places: 3, input: '1,234,567', expected: 1234567 },
{ places: 2, input: '1,234,56', expected: 1234.56 },
{ places: 1, input: '1,234,5', expected: 1234.5 },
{ places: 0, input: '1,234,', expected: 1234.0 },
],
},
{
name: 'space-comma',
tests: [
{ places: 3, input: '1 234 567', expected: 1234567 },
{ places: 2, input: '1 234,56', expected: 1234.56 },
{ places: 1, input: '1 234,5', expected: 1234.5 },
{ places: 0, input: '1 234,', expected: 1234.0 },
],
},
{
name: 'space-dot',
tests: [
{ places: 3, input: '1 234 567', expected: 1234567 },
{ places: 2, input: '1 234.56', expected: 1234.56 },
{ places: 1, input: '1 234.5', expected: 1234.5 },
{ places: 0, input: '1 234.', expected: 1234.0 },
],
},
];
test.each(generateTestCases(configurableFormats, inputFormats))(
'format is agnostic: %s can parse %s with %d decimal place(s)',
(configurableFormat, inputFormat, places, input, expected) => {
setNumberFormat({ format: configurableFormat, hideFraction: false });
expect(evalArithmetic(input)).toEqual(expected);
},
);
}); });
import { getNumberFormat } from './util';
function fail(state, msg) { function fail(state, msg) {
throw new Error( throw new Error(
msg + ': ' + JSON.stringify(state.str.slice(state.index, 10)), msg + ': ' + JSON.stringify(state.str.slice(state.index, 10)),
...@@ -27,24 +29,6 @@ function nextOperator(state, op) { ...@@ -27,24 +29,6 @@ function nextOperator(state, op) {
return false; return false;
} }
function unifyNumberFormatForParsing(numberStr: string): string {
let unifiedNumberStr = '';
for (let i = 0; i < numberStr.length; i++) {
let ch = numberStr[i];
if (ch === ',' || ch === '.') {
// Skip thousands separators
let remainingChars = numberStr.length - i;
if (remainingChars > 3) {
continue;
}
// Unify decimal separator
ch = '.';
}
unifiedNumberStr += ch;
}
return unifiedNumberStr;
}
function parsePrimary(state) { function parsePrimary(state) {
// We only support numbers // We only support numbers
let isNegative = char(state) === '-'; let isNegative = char(state) === '-';
...@@ -60,14 +44,21 @@ function parsePrimary(state) { ...@@ -60,14 +44,21 @@ function parsePrimary(state) {
// and we should do more strict parsing // and we should do more strict parsing
let numberStr = ''; let numberStr = '';
while (char(state) && char(state).match(/[0-9,.]/)) { while (char(state) && char(state).match(/[0-9,.]/)) {
numberStr += next(state); let thousandsSep = getNumberFormat().separator === ',' ? '.' : ',';
// Don't include the thousands separator
if (char(state) === thousandsSep) {
next(state);
} else {
numberStr += next(state);
}
} }
if (numberStr === '') { if (numberStr === '') {
fail(state, 'Unexpected character'); fail(state, 'Unexpected character');
} }
let number = parseFloat(unifyNumberFormatForParsing(numberStr)); let number = parseFloat(numberStr.replace(getNumberFormat().separator, '.'));
return isNegative ? -number : number; return isNegative ? -number : number;
} }
......
---
category: Bugfix
authors: [j-f1]
---
Revert “Make number parsing agnostic to decimal and thousands separators” because it produced undesirable behavior
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