This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.js
82 lines (81 loc) · 2.78 KB
/
util.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
module.exports = {
loadSubmodules: function (path) {
var exp = {},
fs = require('fs');
fs.readdirSync(path).filter(function (f) {
return f.match(/^[a-zA-Z]/)
&& !f.match(/^index\.js$/)
&& (fs.statSync(path+'/'+f).isFile() ? f.match(/\.js$/) : true);
}).forEach(function (f) {
try {
exp[this.camelize(f).replace(/\..*/, '')] = require(path+'/'+f);
} catch (e) {}
}, this);
return exp;
},
camelize: function (x) {
return x.toLowerCase().replace(/_?([^\W_]+)/g, function(m, m1) {
return m1.charAt(0).toUpperCase() + m1.substr(1);
}).replace(/\w+/g, function (m, m1) {
return m.charAt(0).toUpperCase() + m.substr(1);
});
},
hasFields: function (o, fields) {
return o && fields.every(function (f) { return o[f] !== undefined });
},
getEnvConfig: function (fields, opts) {
var prefix = opts.prefix || '', conf = {};
fields.forEach(function (f) {
conf[f] = process.env[(prefix + f).toUpperCase()];
});
return conf
},
defProp: Object.defineProperty.bind(Object),
getOwnProp: Object.getOwnPropertyDescriptor.bind(Object),
clone: Object.create.bind(Object),
extend: function(obj, extra) {
var clone = this.clone(obj);
for (var k in extra)
if (extra.hasOwnProperty(k))
this.defProp(clone, k, this.getOwnProp(extra, k));
else
clone[k] = extra[k];
return clone;
},
shortcut: function(obj, key, other, other_key) {
if (typeof other === 'string') { // in-place shortcut
other_key = other;
other = obj;
}
other_key = other_key || key;
Object.defineProperty(obj, key, { get: function() { return other[key] }});
},
autoconfig: function (defs) {
var extend = this.extend.bind(this),
autoconfig = this.autoconfig.bind(this);
function conf(opts) {
return autoconfig(extend(conf, opts));
}
conf.__proto__ = extend(conf.__proto__, defs);
return conf;
},
objMap: function (old, mapper) {
var obj = {};
Object.keys(old).forEach(function(f) {
obj[f] = mapper(old[f], f);
});
return obj;
},
objFilter: function (old, filter) {
var obj = {};
Object.keys(old).filter(function(f) { return filter(old[f], f) })
.forEach(function(f) { obj[f] = old[f] });
return obj;
},
sans: function (A, B) {
return A.filter(function(x) { return B.indexOf(x) < 0 });
},
ifndef: function(x, d) {
return x === undefined ? d : x
}
}