Files: ecf6563965cf3a734a59dfba74cd29977efbe9d6 / src / domain / record.js
722 bytesRaw
1 | const { blockTypes, blockIdentifiers } = require('./types'); |
2 | const { writeNumber } = require('./number'); |
3 | |
4 | module.exports.readRecord = function readRecord(line) { |
5 | const [symbol, value, ...nameArr] = line.split(' '); |
6 | |
7 | if (![blockIdentifiers.EARNING, blockIdentifiers.EXPENSE].includes(symbol)) { |
8 | throw Error(`Record not in right format, expected first chat to be '+' or '-': ${line}`); |
9 | } |
10 | |
11 | if (isNaN(value)) { |
12 | throw Error('Wrong monetary value to record: ' + line); |
13 | } |
14 | |
15 | const isEarning = symbol === '+'; |
16 | const num = Number(value); |
17 | const name = nameArr.join(' '); |
18 | |
19 | return { |
20 | name, |
21 | symbol, |
22 | value: isEarning ? num : -num, |
23 | toString: () => `${symbol} ${writeNumber(num)} ${name}`, |
24 | }; |
25 | }; |
26 |
Built with git-ssb-web