-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.js
53 lines (41 loc) · 887 Bytes
/
tools.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
var _ = require('lodash');
var path = require('path');
module.exports.$toArray = function(obj) {
var records = [];
_.forEach(obj, function(v, k) {
var record = {
id: k
};
if (typeof v == 'object') {
_.forEach(v, function(v, k) {
record[k] = v;
});
}
records.push(record);
});
return records;
};
module.exports.prepareCSV = function(obj) {
var first = true;
var headers = [];
var data = [];
_.forEach(obj, function(value, key) {
if (typeof value !== 'object') return;
var me = [];
_.forEach(value, function(data, header) {
if (first === true) {
headers.push(header);
}
me.push(data);
});
data.push(me);
first = false;
})
data.unshift(headers);
return data;
};
module.exports.resolvePath = function(string) {
if (string.substr(0,1) === '~')
string = process.env.HOME + string.substr(1)
return path.resolve(string)
}