All files / src/modules/implementations/commands/list-definitions/reports aggregate.js

100% Statements 45/45
93.1% Branches 27/29
100% Functions 4/4
100% Lines 45/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4640x 40x 40x 768x 23x 768x 768x 8x 8x 8x 8x 17x 17x 17x 17x 8x 768x 768x 23x 23x 10x 10x 25x 25x 25x 25x 25x 25x 25x 10x 10x 13x 13x 21x 34x 34x 34x 34x 34x 34x 34x 34x 23x 768x 40x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ numeric: { decimal: { formatDecimalFixed, parseDecimal } } }) => ({
    loadFormatData(journal) {
        return journal.commodities({ usage: 'all' });
    },
    formatCsv(rows, { denominate, groupBy }, format) {
        const { formatCsvTable } = format;
        const showAccounts = groupBy !== 'commodity';
        const headings = showAccounts ? ['account', 'amount', 'commodity'] : ['amount', 'commodity'];
        return formatCsvTable(headings, rows.map((row) => {
            const quantity = denominate ? formatDecimalFixed(parseDecimal(row.quantity), 2) : row.quantity;
            return showAccounts
                ? [row.account, quantity, row.commodity]
                : [quantity, row.commodity];
        }));
    },
    formatText(rows, { denominate, groupBy }, format, descriptions) {
        const { commodityFormats, displayQuantity, formatAlignedAmounts, parseCommodityFormat, } = format;
        if (!denominate || groupBy === 'commodity') {
            const formats = commodityFormats(descriptions);
            return formatAlignedAmounts(rows.map((row) => ({
                quantity: displayQuantity(row.quantity, row.commodity, formats, denominate ? 2 : null),
                commodity: row.commodity,
                label: row.account,
                isTotal: row.isTotal,
                decimalSeparator: formats.has(row.commodity)
                    ? parseCommodityFormat(formats.get(row.commodity))?.decimalSeparator
                    : '.',
            })), groupBy !== 'commodity');
        }
        const formats = commodityFormats(descriptions);
        const lines = [];
        for (const row of rows) {
            const quantity = displayQuantity(row.quantity, row.commodity, formats, 2);
            const amount = parseDecimal(quantity.replaceAll(',', '')).coefficient === 0n
                ? '0'
                : `${quantity} ${row.commodity}`;
            if (row.isTotal)
                lines.push('-'.repeat(20));
            lines.push(row.isTotal ? amount.padStart(20) : `${amount.padStart(20)}  ${row.account}`);
        }
        return lines.length === 0 ? '' : `${lines.join('\n')}\n`;
    },
});