const { blockTypes, blockIdentifiers } = require('./types'); const { writeNumber } = require('./number'); module.exports.readRecord = function readRecord(line) { const [symbol, value, ...nameArr] = line.split(' '); if (![blockIdentifiers.EARNING, blockIdentifiers.EXPENSE].includes(symbol)) { throw Error(`Record not in right format, expected first chat to be '+' or '-': ${line}`); } if (isNaN(value)) { throw Error('Wrong monetary value to record: ' + line); } const isEarning = symbol === '+'; const num = Number(value); const name = nameArr.join(' '); return { name, symbol, value: isEarning ? num : -num, toString: () => `${symbol} ${writeNumber(num)} ${name}`, }; };