Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Make comma's and period interchangeable when entering figures #2672

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 4 additions & 19 deletions packages/loot-core/src/shared/arithmetic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-strict-ignore
import { getNumberFormat } from './util';
import { currencyToAmount } from './util';

function fail(state, msg) {
throw new Error(
Expand Down Expand Up @@ -37,31 +37,16 @@ function parsePrimary(state) {
next(state);
}

if (/\p{Sc}/u.test(char(state))) {
next(state);
}

// TODO: The regex should probably respect the number format better,
// and we should do more strict parsing
let numberStr = '';
while (char(state) && char(state).match(/[0-9,.]/)) {
const thousandsSep = getNumberFormat().separator === ',' ? '.' : ',';

// Don't include the thousands separator
if (char(state) === thousandsSep) {
next(state);
} else {
numberStr += next(state);
}
while (char(state) && char(state).match(/[0-9,. ]|\p{Sc}/u)) {
numberStr += next(state);
}

if (numberStr === '') {
fail(state, 'Unexpected character');
}

const number = parseFloat(
numberStr.replace(getNumberFormat().separator, '.'),
);
const number = currencyToAmount(numberStr);
return isNegative ? -number : number;
}

Expand Down
28 changes: 20 additions & 8 deletions packages/loot-core/src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,14 @@ export function getNumberFormat({
format?: NumberFormats;
hideFraction: boolean;
} = numberFormatConfig) {
let locale, regex, separator;
let locale, regex, separator, separatorRegex;

switch (format) {
case 'space-comma':
locale = 'en-SE';
regex = /[^-0-9,]/g;
regex = /[^-0-9,.]/g;
separator = ',';
separatorRegex = /[,.]/g;
break;
case 'dot-comma':
locale = 'de-DE';
Expand All @@ -270,8 +271,9 @@ export function getNumberFormat({
break;
case 'space-dot':
locale = 'dje';
regex = /[^-0-9.]/g;
regex = /[^-0-9,.]/g;
separator = '.';
separatorRegex = /[,.]/g;
break;
case 'comma-dot-in':
locale = 'en-IN';
Expand All @@ -293,6 +295,7 @@ export function getNumberFormat({
maximumFractionDigits: hideFraction ? 0 : 2,
}),
regex,
separatorRegex,
};
}

Expand Down Expand Up @@ -342,11 +345,20 @@ export function amountToCurrencyNoDecimal(n) {
}

export function currencyToAmount(str: string) {
const amount = parseFloat(
str
.replace(getNumberFormat().regex, '')
.replace(getNumberFormat().separator, '.'),
);
let amount;
if (getNumberFormat().separatorRegex) {
amount = parseFloat(
str
.replace(getNumberFormat().regex, '')
.replace(getNumberFormat().separatorRegex, '.'),
);
} else {
amount = parseFloat(
str
.replace(getNumberFormat().regex, '')
.replace(getNumberFormat().separator, '.'),
);
}
return isNaN(amount) ? null : amount;
}

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2672.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [Wizmaster]
---

Comma and period decimal separator can both be used for number format not using those as thousand separator.