Display single-letter commodity before quantity

This commit is contained in:
RunasSudo 2025-06-08 21:22:57 +10:00
parent 6174a7ce57
commit 5439632fc6
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 13 additions and 5 deletions

View File

@ -177,7 +177,7 @@ function parseFloatStrict(quantity: string): number {
return parseFloat(quantity);
}
function validateCommodity(commodity: string) {
export function validateCommodity(commodity: string) {
// Validate that the commodity is correctly formed
const commodityParts = commodity.split(' ');
if (commodityParts.length > 2) {

View File

@ -1,6 +1,6 @@
/*
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222025 Lee Yingtong Li (RunasSudo)
Copyright (C) 2022-2025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
@ -16,7 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { db } from './db.ts';
import { db, validateCommodity } from './db.ts';
export function pp(quantity: number): string {
// Pretty print the quantity
@ -33,8 +33,16 @@ export function pp(quantity: number): string {
export function ppWithCommodity(quantity: number, commodity: string): string {
// Pretty print the amount including commodity
if (commodity.length === 1) {
return commodity + pp(quantity);
validateCommodity(commodity);
const commodityParts = commodity.split(' ');
if (commodityParts[0].length === 1) {
if (commodityParts.length === 1) {
return commodityParts[0] + pp(quantity);
} else {
return commodityParts[0] + pp(quantity) + ' ' + commodityParts[1];
}
} else {
return pp(quantity) + ' ' + commodity;
}