vizwiz/server/api/parseLine.js
2017-03-15 18:03:15 +01:00

138 lines
2.9 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(' ').slice(1, -1),
};
}
const accountType = {
T: 1,
S: 2,
I: 3,
K: 4,
};
function parseCompanyName(str) {
const [, name] = str.split(/\s+/);
return name.slice(1, -1);
}
// str should be `ORGNR "556930-1673"`
function parseOrganizationNumber(str) {
const [, number] = str.split(/\s+/);
return number.slice(1, -1);
}
function parseYearDates(str) {
const [, current, start, end] = str.split(/\s+/);
return {
start: parseDate(start),
end: parseDate(end),
current: current === '0',
};
}
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 parseSRU(str) {
const [, number, sru] = str.split(/\s+/);
return {
number: parseInt(number, 10),
sru: parseInt(sru, 10),
};
}
function parseTransaction(str) {
// str looks like: ' #TRANS 6310 {} 1541.00 20150824 '
const [, accountNumber,, amount] = str.split(/\s+/);
return {
accountNumber: parseInt(accountNumber, 10),
amount: parseFloat(amount),
};
}
function parseTicket(str) {
const [, type, number, date, ...rest] = str.split(/\s+/);
const dateCreated = rest.pop();
const title = rest.join(' ');
return {
type: parseInt(type, 10),
number: parseInt(number, 10),
date: parseDate(date),
date_created: parseDate(dateCreated),
title: title.slice(1, -1),
};
}
module.exports = {
parseAccountType,
parseAccount,
parseCompanyName,
parseOrganizationNumber,
parseSRU,
parseTransaction,
parseTicket,
parseYearDates,
};
// console.log(string.split('\n#').length)
// module.exports = function parseLine(line) {
// line = line.slice(1);
// if (line.startsWith('KONTO')) {
// const account = parseAccount(line);
// } else if (line.startWith('KTYP') {
// const accountType = parseAccountType(line);
// }
// 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);