forked from ostrojs/support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.js
32 lines (29 loc) · 761 Bytes
/
object.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
const _ = require('lodash')
Object.filter = function(obj, cb) {
let keys = Object.keys(obj)
for (let key of keys) {
let data = (typeof cb == 'function' ? cb(obj[key]) : obj[key])
if (!data) {
delete obj[key]
}
}
return obj
}
Object.except = function(obj, keys) {
var target = {};
for (var i in obj) {
if (keys.indexOf(i) >= 0) continue;
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
target[i] = obj[i];
}
return target;
}
Object.flip = function flip(obj) {
return _.transform(obj, function(res, val, key) {
if (_.isPlainObject(val)) {
res[key] = deepInvert(val);
} else {
res[val] = key;
}
});
}