mirror of
https://github.com/actualbudget/actual.git
synced 2026-03-09 06:03:01 -05:00
fix(csv-import): trim whitespace from amount strings before parsing (#7149)
* fix(csv-import): trim whitespace from amount strings before parsing looselyParseAmount relies on a regex anchored at $ to detect decimal markers. Trailing whitespace (e.g. from Excel-saved CSVs) shifts the pattern match so a thousands separator is misidentified as a decimal point, producing wildly wrong values. Adding trim() at the top of the function eliminates trailing/leading whitespace before any regex logic runs. Fixes actualbudget/actual#7121 * chore: add release notes for #7149
This commit is contained in:
@@ -72,6 +72,15 @@ describe('utility functions', () => {
|
||||
expect(looselyParseAmount('(1 500.99)')).toBe(-1500.99);
|
||||
});
|
||||
|
||||
test('looseParseAmount handles trailing whitespace', () => {
|
||||
expect(looselyParseAmount('1055 ')).toBe(1055);
|
||||
expect(looselyParseAmount('$1,055 ')).toBe(1055);
|
||||
expect(looselyParseAmount('$1,055.00 ')).toBe(1055);
|
||||
expect(looselyParseAmount(' $1,055 ')).toBe(1055);
|
||||
expect(looselyParseAmount('3.45 ')).toBe(3.45);
|
||||
expect(looselyParseAmount(' 3.45 ')).toBe(3.45);
|
||||
});
|
||||
|
||||
test('number formatting works with comma-dot format', () => {
|
||||
setNumberFormat({ format: 'comma-dot', hideFraction: false });
|
||||
let formatter = getNumberFormat().formatter;
|
||||
|
||||
@@ -550,6 +550,8 @@ export function looselyParseAmount(amount: string) {
|
||||
return v.replace(/[^0-9-]/g, '');
|
||||
}
|
||||
|
||||
amount = amount.trim();
|
||||
|
||||
if (amount.startsWith('(') && amount.endsWith(')')) {
|
||||
// Remove Unicode minus inside parentheses before converting to ASCII minus
|
||||
amount = amount.replace(/\u2212/g, '');
|
||||
|
||||
6
upcoming-release-notes/7149.md
Normal file
6
upcoming-release-notes/7149.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
category: Bugfixes
|
||||
authors: [mibragimov]
|
||||
---
|
||||
|
||||
Fix CSV import incorrectly parsing transaction amounts that contain trailing whitespace (e.g. amounts from Excel-saved CSV files).
|
||||
Reference in New Issue
Block a user