All files / src/modules/implementations/accounting journal-valuation-commodity.js

100% Statements 28/28
82.35% Branches 14/17
100% Functions 2/2
100% Lines 28/28

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 2963x 63x 798x 444x 444x 444x 444x 112915x 112915x 112892x 416x 2x 2x 2x 2x 2x 2x 414x 414x 414x 1000x 444x 30x 30x 444x 444x 798x 63x  
'use strict';
module.exports = ({ journal: { ingestionWarning: { createWarning, warningCodes }, }, }) => {
    function valuationCommodityFromJournal(journal, suppliedWarnings, ignoredProperties) {
        const warnings = suppliedWarnings || [];
        let valuationCommodity = null;
        let defaultLocation = null;
        for (const entry of journal.entries) {
            if (entry.type !== 'commodity')
                continue;
            for (const property of entry.properties.filter(({ name }) => name === 'default')) {
                if (defaultLocation) {
                    warnings.push(createWarning(warningCodes.MULTIPLE_DEFAULT_COMMODITIES, 'Multiple commodity declarations are marked default; using the first at ' +
                        `${defaultLocation.source}:${defaultLocation.line}:${defaultLocation.column}`, property.location));
                    if (ignoredProperties)
                        ignoredProperties.add(property);
                    continue;
                }
                valuationCommodity = entry.symbol;
                defaultLocation = property.location;
            }
        }
        if (!valuationCommodity) {
            warnings.push(createWarning(warningCodes.MISSING_DEFAULT_COMMODITY, 'Journal must declare one default commodity', { source: journal.journalPath || journal.source || '<input>', line: 1, column: 1 }));
        }
        return valuationCommodity;
    }
    return { valuationCommodityFromJournal };
};