-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpick-omit.js
34 lines (33 loc) · 864 Bytes
/
pick-omit.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
function pick(obj, keys) {
const picked = {};
for (let key in obj) {
let regexp = new RegExp(`^${key}$`);
if (typeof keys === "string") {
if (keys.match(regexp)) {
picked[key] = obj[key];
}
} else {
if (keys.includes(key)) {
picked[key] = obj[key];
}
}
}
return picked;
}
function omit(obj, keys) {
const omitted = {};
for (let key in obj) {
if (obj.hasOwnProperty(key) === false) continue;
let regexp = new RegExp(`^${key}$`);
if (typeof keys === "string") {
if (!keys.match(regexp)) {
omitted[key] = obj[key];
}
} else {
if (!keys.includes(key)) {
omitted[key] = obj[key];
}
}
}
return omitted;
}