-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
48 lines (41 loc) · 1.15 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
module.exports = (function () {
'use strict';
var fs = require('fs');
function writeField(code, value) {
var result = '';
if (value) {
result += code;
result += value;
result += '\r\n';
}
return result;
}
var write = function (transactions) {
var result;
if (transactions && transactions.cash) {
result = '!Type:Cash\r\n';
for (var i = 0; i < transactions.cash.length; i++) {
var transaction = transactions.cash[i];
result += writeField('D', transaction.date);
result += writeField('T', transaction.amount);
result += writeField('P', transaction.payee);
result += writeField('N', transaction.checknumber);
result += writeField('L', transaction.category);
result += writeField('M', transaction.memo);
result += '^\r\n';
};
}
return result;
};
var writeToFile = function (transactions, file, callback) {
var data = write(transactions);
fs.writeFile(file, data, function (err) {
if (err) throw err;
callback(undefined, data);
});
};
return {
write: write,
writeToFile: writeToFile
};
})();