const { readBlock, writeBlock } = require('./domain/block'); /* * data {string} - Data to be parsed */ module.exports = function process(lines) { const groupsArr = readBlock(lines); // Calculate total of each group const groupsWithTotals = groupsArr.map(g => ({ ...g, total: g.records.reduce((total, record) => total + record.value, g.total), })); // Calculate balance const balance = groupsWithTotals.reduce((balance, g) => { if (g.ignoreInBalance) return balance; return balance + g.total; }, 0); return writeBlock(balance, groupsWithTotals); };