-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.js
58 lines (47 loc) · 1.25 KB
/
helpers.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
const moment = require('moment');
require('moment-duration-format');
const stereotype = require('stereotype');
module.exports = {
parseList,
parseUpsVars,
getCurrentTimestamp,
getProjectRoot,
time
};
function parseList(list) {
return Object.keys(list).map(val => ({ name: val, description: list[val] }), []);
}
function getCurrentTimestamp() {
return moment().format('YYYY-MM-DD-HH-mm-ss');
}
function getProjectRoot() {
const directory = __dirname.split('/');
return directory.join('/');
}
function time(startDateTime, endDateTime) {
const duration = moment.duration(endDateTime.diff(startDateTime));
if (duration < 1000) {
return duration.format('S [ms]');
}
return duration.format('h [hours] m [minutes] s [seconds]');
}
// http://jsfiddle.net/brandonscript/tfjH3/
function parseUpsVars(obj) {
Object.keys(obj).forEach(key => {
if (key.indexOf('.') !== -1) parseDotNotation(key, obj[key], obj);
});
return obj;
}
function parseDotNotation(str, val, obj) {
const keys = str.split('.');
let currentObj = obj;
let key;
let i = 0;
for (i; i < Math.max(1, keys.length - 1); ++i) {
key = keys[i];
currentObj[key] = currentObj[key] || {};
currentObj = currentObj[key];
}
currentObj[keys[i]] = stereotype(val.trim());
delete obj[str];
}