Allow inputting negative quantities

This commit is contained in:
RunasSudo 2025-08-14 21:50:47 +10:00
parent 3badc1e936
commit 4d9741525b
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -183,7 +183,7 @@ export function serialiseAmount(quantity: number, commodity: string): string {
function parseFloatStrict(quantity: string): number { function parseFloatStrict(quantity: string): number {
// Parses quantity as a float, throwing error on invalid input // Parses quantity as a float, throwing error on invalid input
if (!/^[0-9]+(\.[0-9]+)?$/.test(quantity)) { if (!/^-?[0-9]+(\.[0-9]+)?$/.test(quantity)) {
throw new DeserialiseAmountError('Invalid quantity: ' + quantity); throw new DeserialiseAmountError('Invalid quantity: ' + quantity);
} }
return parseFloat(quantity); return parseFloat(quantity);
@ -216,12 +216,21 @@ export function deserialiseAmount(amount: string): { quantity: number, commodity
throw new DeserialiseAmountError('Amount cannot be blank'); throw new DeserialiseAmountError('Amount cannot be blank');
} }
if (amount.charAt(0) === '-') {
// Handle negative amount
const amountAbs = deserialiseAmount(amount.substring(1));
return {
quantity: -amountAbs.quantity,
commodity: amountAbs.commodity
};
}
if (amount.charAt(0) < '0' || amount.charAt(0) > '9') { if (amount.charAt(0) < '0' || amount.charAt(0) > '9') {
// Check for single letter commodity // Check for single letter commodity
if (amount.length === 1) { if (amount.length === 1) {
throw new DeserialiseAmountError('Quantity cannot be blank (expected quantity after commodity symbol ' + amount + ')'); throw new DeserialiseAmountError('Quantity cannot be blank (expected quantity after commodity symbol ' + amount + ')');
} }
if (amount.charAt(1) < '0' || amount.charAt(1) > '9') { if ((amount.charAt(1) < '0' || amount.charAt(1) > '9') && amount.charAt(1) !== '-') {
throw new DeserialiseAmountError('Invalid quantity: ' + amount + ' (expected quantity after single-letter commodity symbol ' + amount.charAt(0) + ')'); throw new DeserialiseAmountError('Invalid quantity: ' + amount + ' (expected quantity after single-letter commodity symbol ' + amount.charAt(0) + ')');
} }