Skip to content

A local-first personal finance app

fix: Handle unicode minus in number parsing (#6247)

Matiss Janis Aboltins43970c14b14b
diff --git a/packages/loot-core/src/shared/util.test.ts b/packages/loot-core/src/shared/util.test.ts
index 4eeddffa7a..092ee449c2 100644
--- a/packages/loot-core/src/shared/util.test.ts
+++ b/packages/loot-core/src/shared/util.test.ts
@@ -3,6 +3,7 @@ import {
getNumberFormat,
setNumberFormat,
currencyToAmount,
+ stringToInteger,
titleFirst,
} from './util';
@@ -49,11 +50,18 @@ describe('utility functions', () => {
expect(looselyParseAmount('-3')).toBe(-3);
expect(looselyParseAmount('-3.45')).toBe(-3.45);
expect(looselyParseAmount('-3,45')).toBe(-3.45);
+ // Unicode minus
+ expect(looselyParseAmount('−3')).toBe(-3);
+ expect(looselyParseAmount('−3.45')).toBe(-3.45);
+ expect(looselyParseAmount('−3,45')).toBe(-3.45);
});
test('looseParseAmount works with parentheses (negative)', () => {
expect(looselyParseAmount('(3.45)')).toBe(-3.45);
expect(looselyParseAmount('(3)')).toBe(-3);
+ // Parentheses with Unicode minus
+ expect(looselyParseAmount('(−3.45)')).toBe(-3.45);
+ expect(looselyParseAmount('(−3)')).toBe(-3);
});
test('looseParseAmount ignores non-numeric characters', () => {
@@ -142,6 +150,10 @@ describe('utility functions', () => {
expect(currencyToAmount('-3')).toBe(-3);
expect(currencyToAmount('-3.45')).toBe(-3.45);
expect(currencyToAmount('-3,45')).toBe(-3.45);
+ // Unicode minus
+ expect(currencyToAmount('−3')).toBe(-3);
+ expect(currencyToAmount('−3.45')).toBe(-3.45);
+ expect(currencyToAmount('−3,45')).toBe(-3.45);
});
test('currencyToAmount works with non-fractional numbers', () => {
@@ -181,4 +193,10 @@ describe('utility functions', () => {
expect(titleFirst('a')).toBe('A');
expect(titleFirst('abc')).toBe('Abc');
});
+
+ test('stringToInteger works with negative numbers', () => {
+ expect(stringToInteger('-3')).toBe(-3);
+ // Unicode minus
+ expect(stringToInteger('−3')).toBe(-3);
+ });
});
diff --git a/packages/loot-core/src/shared/util.ts b/packages/loot-core/src/shared/util.ts
index a2121c3085..6b09125069 100644
--- a/packages/loot-core/src/shared/util.ts
+++ b/packages/loot-core/src/shared/util.ts
@@ -461,6 +461,8 @@ export function amountToCurrencyNoDecimal(amount: Amount): CurrencyAmount {
}
export function currencyToAmount(currencyAmount: string): Amount | null {
+ currencyAmount = currencyAmount.replace(/\u2212/g, '-');
+
let integer, fraction;
// match the last dot or comma in the string
@@ -490,7 +492,9 @@ export function currencyToInteger(
}
export function stringToInteger(str: string): number | null {
- const amount = parseInt(str.replace(/[^-0-9.,]/g, ''));
+ const amount = parseInt(
+ str.replace(/\u2212/g, '-').replace(/[^-0-9.,]/g, ''),
+ );
if (!isNaN(amount)) {
return amount;
}
@@ -536,7 +540,12 @@ export function looselyParseAmount(amount: string) {
}
if (amount.startsWith('(') && amount.endsWith(')')) {
+ // Remove Unicode minus inside parentheses before converting to ASCII minus
+ amount = amount.replace(/\u2212/g, '');
amount = amount.replace('(', '-').replace(')', '');
+ } else {
+ // Replace Unicode minus with ASCII minus for non-parenthesized amounts
+ amount = amount.replace(/\u2212/g, '-');
}
// Look for a decimal marker, then look for either 1-2 or 4-9 decimal places.
diff --git a/upcoming-release-notes/6247.md b/upcoming-release-notes/6247.md
new file mode 100644
index 0000000000..2e2e0652f4
--- /dev/null
+++ b/upcoming-release-notes/6247.md
@@ -0,0 +1,7 @@
+---
+category: Bugfix
+authors: [MatissJanis]
+---
+
+Fix number parsing to recognize Unicode minus as a valid negative indicator.
+