96 lines
2.1 KiB
JavaScript
96 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
function parseAccount(str) {
|
|
// str will look like: 'KONTO 1010 "Balanserade utgifter"'
|
|
const [, number, ...name] = str.split(/\s+/);
|
|
|
|
return {
|
|
number: parseInt(number, 10),
|
|
name: name.join(' '),
|
|
};
|
|
}
|
|
|
|
const accountType = {
|
|
T: 1,
|
|
S: 2,
|
|
I: 3,
|
|
K: 4,
|
|
};
|
|
|
|
function parseAccountType(str) {
|
|
const [, number, type] = str.split(/\s+/);
|
|
|
|
return {
|
|
number: parseInt(number, 10),
|
|
type: accountType[type],
|
|
};
|
|
}
|
|
|
|
function parseDate(str) {
|
|
const [, year, month, day] = /(\d\d\d\d)(\d\d)(\d\d)/.exec(str);
|
|
|
|
return new Date(`${year}-${month}-${day}T00:00:00.000Z`);
|
|
}
|
|
|
|
function parseTransaction(str) {
|
|
// str looks like: ' #TRANS 6310 {} 1541.00 20150824 '
|
|
const [,, account,, amount, date] = str.split(/\s+/);
|
|
|
|
return {
|
|
account: parseInt(account, 10),
|
|
amount: parseFloat(amount),
|
|
date: parseDate(date),
|
|
};
|
|
}
|
|
|
|
function parseTicket(str) {
|
|
const arr = str.split('\r\n');
|
|
// arr[0] will look like: 'VER 5101 147 20150917 "Binero" 20160224'
|
|
const [, type, number, date, ...rest] = arr[0].split(/\s+/);
|
|
const dateCreated = rest.pop();
|
|
const title = rest.join(' ');
|
|
|
|
console.log(type);
|
|
const transactions = arr.slice(2, -2).map(parseTransaction);
|
|
|
|
return {
|
|
type: parseInt(type, 10),
|
|
number: parseInt(number, 10),
|
|
date: parseDate(date),
|
|
dateCreated: parseDate(date),
|
|
title,
|
|
transactions,
|
|
}
|
|
}
|
|
|
|
|
|
// console.log(string.split('\n#').length)
|
|
|
|
module.exports = function parse(string) {
|
|
const accounts = [];
|
|
const tickets = [];
|
|
|
|
string.split(/\n#/g).forEach((str, i) => {
|
|
if (str.startsWith('KONTO')) {
|
|
accounts.push(parseAccount(str));
|
|
} else if (str.startsWith('KTYP')) {
|
|
const { number, type } = parseAccountType(str);
|
|
|
|
// const string = fs.readFileSync('./bitmill2015.se', { encoding: 'UTF8' });
|
|
const account = accounts.find((account) => account.number === number);
|
|
|
|
account.type = type;
|
|
} else if (str.startsWith('VER')) {
|
|
tickets.push(parseTicket(str));
|
|
}
|
|
});
|
|
|
|
return {
|
|
accounts,
|
|
tickets,
|
|
};
|
|
};
|
|
// console.log(tickets);
|
|
// process.exit(0);
|
|
|