All files / src/modules/implementations/core report-totals.js

100% Statements 30/30
100% Branches 10/10
100% Functions 2/2
100% Lines 30/30

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 3171x 71x 811x 30x 30x 65x 65x 36x 36x 36x 65x 65x 4x 4x 65x 30x 36x 36x 36x 36x 36x 36x 36x 3x 36x 36x 30x 30x 811x 71x  
'use strict';
module.exports = ({ numeric: { decimal: { addDecimals, formatDecimal, parseDecimal } } }) => {
    function appendTotal(rows) {
        const totals = new Map();
        for (const row of rows) {
            let total = totals.get(row.commodity);
            if (!total) {
                total = { quantity: parseDecimal('0') };
                totals.set(row.commodity, total);
            }
            total.quantity = addDecimals(total.quantity, parseDecimal(row.quantity));
            if (row.valuationValue !== undefined) {
                total.valuationValue = addDecimals(total.valuationValue ?? parseDecimal('0'), parseDecimal(row.valuationValue));
            }
        }
        return [...rows, ...[...totals.keys()].sort().map((commodity) => {
                const total = totals.get(commodity);
                return {
                    account: 'Total',
                    commodity,
                    isTotal: true,
                    quantity: formatDecimal(total.quantity),
                    ...(total.valuationValue === undefined ? {} : {
                        valuationValue: formatDecimal(total.valuationValue),
                    }),
                };
            })];
    }
    return { appendTotal };
};