Skip to content
Snippets Groups Projects
Unverified Commit 28c68940 authored by Aidan Harbison's avatar Aidan Harbison Committed by GitHub
Browse files

transaction-import: treat (amount) as -amount (#808)

- When parsing an amount string, consider surrounding parentheses to
mean the amount is negative.
- Ensures all input to `parseFloat()` is sanitized.

Closes: #807 
parent e71d4dc6
No related branches found
No related tags found
No related merge requests found
......@@ -377,15 +377,23 @@ export function looselyParseAmount(amount) {
return isNaN(v) ? null : v;
}
function extractNumbers(v) {
return v.replace(/[^0-9-]/g, '');
}
if (amount.startsWith('(') && amount.endsWith(')')) {
amount = amount.replace('(', '-').replace(')', '');
}
let m = amount.match(/[.,][^.,]*$/);
if (!m || m.index === 0) {
return safeNumber(parseFloat(amount));
return safeNumber(parseFloat(extractNumbers(amount)));
}
let left = amount.slice(0, m.index);
let right = amount.slice(m.index + 1);
let left = extractNumbers(amount.slice(0, m.index));
let right = extractNumbers(amount.slice(m.index + 1));
return safeNumber(parseFloat(left.replace(/[^0-9-]/g, '') + '.' + right));
return safeNumber(parseFloat(left + '.' + right));
}
export function semverToNumber(str) {
......
......@@ -22,6 +22,11 @@ describe('utility functions', () => {
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);
});
test('looseParseAmount ignores non-numeric characters', () => {
// This is strange behavior because it does not work for just
// `3_45_23` (it needs a decimal amount). This function should be
......
---
category: Enhancements
authors: [aharbis]
---
Import transactions with negative amounts represented as `(amount)`
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