Allow inputting negative quantities
This commit is contained in:
parent
3badc1e936
commit
4d9741525b
13
src/db.ts
13
src/db.ts
@ -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) + ')');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user