Files: 7cc4808dbfe9bd370a1f0df3485981c9a7b048e4 / 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