-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
287 lines (222 loc) · 8.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"use strict";
const fs = require('fs');
const moment = require('moment');
const sources = require('./sources');
const path = require('path');
const YNABHeadings = ['Date', 'Payee', 'Category', 'Memo', 'Outflow', 'Inflow'];
//Default optionss
let options;
let sourceConfig;
//TODO: Be able to provide string of data instead of file
function generate(file, opts){
if(typeof opts === 'undefined') opts = {};
options = {
source: 'nordea',
delimitor: ';',
dateformat: 'DD/MM/YYYY',
payees: [],
path: '.',
output: 'ynab',
csvstring: false,
write: true
};
return new Promise( (resolve, reject ) => {
validateOptions(opts)
.then( (opts) => {
options = util.extend(options, opts);
sourceConfig = sources[options.source];
//Check if the source provides a delimitor and not overwritten by a provided option
if(!opts.delimitor && sourceConfig.delimitor){
options.delimitor = sourceConfig.delimitor;
}
})
.then( () => loadFile(file) )
.then( getCSVRows )
.then( generateCSV )
.then( writeCSV )
.then( resolve )
.catch( reject );
});
}
function validateOptions(opts){
return new Promise((resolve, reject) => {
//Validate source
if(opts.source && !sources.hasOwnProperty(opts.source)){
reject(Error(`Source ${opts.source} is not valid. List of valid sources: [ ${Object.keys(sources)} ]`)); return;
}
//Validate dateformat
let allowedDateFormats = ["DD/MM/YYYY", "YYYY/MM/DD", "YYYY-MM-DD", "DD-MM-YYYY", "DD.MM.YYYY", "MM/DD/YYYY", "YYYY.MM.DD"];
if(opts.dateformat && allowedDateFormats.indexOf(opts.dateformat) === -1){
reject(Error(`Date format ${opts.dateformat} is not valid. List of valid dateformats: [ ${allowedDateFormats} ]`)); return;
}
//Validate last date
let dateformat = opts.dateformat || options.dateformat;
if(opts.lastdate && !moment(opts.lastdate, dateformat, true).isValid() ){
reject(Error(`${opts.lastdate} is not a valid date for ${dateformat} date format`)); return;
}
//Validate output
if(opts.output){
opts.output = opts.output.replace(/\.csv$/i, '');
//Directory
try{
let stats = fs.lstatSync(opts.output);
if (stats.isDirectory()) {
opts.path = opts.output;
delete opts.output;
}
} catch(e){
//Files should be ignored
}
}
resolve(opts);
});
}
function loadFile(file){
return new Promise((resolve, reject) => {
//Check if we have a csv string provided
if(options.csvstring && !file){
reject(Error('A valid csv string needs to be provided')); return;
}
//Check if we have a csv string provided
if(options.csvstring && file){
resolve(file); return;
}
//Check if file provided
if(!file){
reject(Error('A valid .csv file needs to be provided')); return;
}
//Validate csv file
if(!/.*\.csv$/i.test(file)){
reject(Error('File provided is does not have a .csv extension')); return;
}
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
reject(err); return;
}
resolve(data);
});
});
}
function getCSVRows(data){
return new Promise((resolve, reject) => {
//Check if any data
if(!data.toString()){
reject(Error('CSV file is empty')); return;
}
var rows = data.toString().replace(/\r/g, '').split('\n').filter( row => !!row );
var headerCells = rows.shift().split( options.delimitor ).filter( cell => !!cell );
//Check if any data rows
if(!rows.length && headerCells.length){
reject(Error('CSV file only contains the header row')); return;
}
//Check if the csv heading cells are the same as the source config
if( (sourceConfig.headers.length !== headerCells.length) || !sourceConfig.headers.every((h,i)=> h == headerCells[i]) ) {
reject(Error(`CSV headers are not the same as the source config headers. Expected header rows: [ ${sourceConfig.headers.join(options.delimitor)} ]`)); return;
}
resolve(rows);
});
}
function generateCSV(rows){
return new Promise((resolve, reject) => {
var newData = YNABHeadings.join(';') + '\n';
rows.forEach( (row, y) => {
let cells = row.split(options.delimitor).filter( c => !!c );
//Check if we're exceeding the last date
var date = moment(cells[sourceConfig.map.date], sourceConfig.dateformat);
var lastDate = options.lastdate ? moment(options.lastdate, options.dateformat) : false;
var renderRow = !options.lastdate || date.isSameOrBefore(lastDate);
if(renderRow){
YNABHeadings.forEach( (h, i) => {
var heading = h.toLowerCase();
newData += createField[heading]( cells[ sourceConfig.map[heading] ], cells );
if(i !== YNABHeadings.length -1){
newData += ';';
}
});
if(y !== rows.length -1){
newData += '\n';
}
}
});
resolve(newData);
});
}
function writeCSV(data){
return new Promise((resolve, reject) => {
//If no write, output file
if(!options.write){
resolve(data); return;
}
let filename = path.join(options.path, options.output + '.csv');
fs.writeFile(filename, data, function (err) {
if (err){
reject(err); return;
}
resolve(`File ${filename} written successfully!`);
});
});
}
class createField{
static date(val){
if(sourceConfig.map.date === null) return '';
var date = moment(val, sourceConfig.dateformat).format(options.dateformat);
//For invalid date return today's date
if(date == 'Invalid date'){
date = moment().format(options.dateformat);
}
return date;
}
static payee(val, cells){
if(sourceConfig.map.payee == null && options.payees.length) {
var payee = '';
for(var i = 0; i < options.payees.length; i++){
if(payee) break;
var regexp = new RegExp(options.payees[i], 'i');
if(regexp.test( cells[sourceConfig.map.memo] ) ){
payee = options.payees[i];
}
}
return payee;
} else {
if(sourceConfig.map.payee == null) return '';
return val;
}
}
static category(val){
if(sourceConfig.map.category == null) return '';
return val;
}
static memo(val){
if(sourceConfig.map.memo == null) return '';
return val.replace(/\s{2,100}/g, ' ');
}
static inflow(val){
if(sourceConfig.map.inflow == null) return '';
val = val.replace(',', '.');
if(sourceConfig.map.outflow == sourceConfig.map.inflow && val.indexOf('-') == 0){
return '';
}
return Math.abs(parseFloat(val));
}
static outflow(val){
if(sourceConfig.map.outflow == null) return '';
val = val.replace(',', '.');
if(sourceConfig.map.outflow == sourceConfig.map.inflow && val.indexOf('-') == -1){
return '';
}
return Math.abs(parseFloat(val));
}
}
let util = {
extend: function(obj1, obj2){
var obj3 = {};
Object.keys(obj1).forEach( key => {
obj3[key] = obj1[key];
});
Object.keys(obj2).forEach( key => {
obj3[key] = obj2[key];
});
return obj3;
}
}
module.exports = generate;