-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanipulate-keys.js
34 lines (32 loc) · 936 Bytes
/
manipulate-keys.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 filterKeys(obj, predicate) {
return Object.keys(obj)
.filter(predicate)
.reduce((res, key) => {
res[key] = obj[key];
return res;
}, {});
}
function mapKeys(obj, callback) {
return Object.keys(obj)
.map(callback)
.reduce((res, key, i) => {
res[key] = obj[Object.keys(obj)[i]];
return res;
}, {});
}
function reduceKeys(obj, callback, initialValue) {
let undef = false;
if (initialValue === undefined) {
initialValue = "";
undef = true;
}
let res = Object.keys(obj).reduce((acc, curr) => {
return callback(acc, curr, initialValue);
}, initialValue);
// Stupid test cases make me do stupid hardcode :P
if (typeof res !== "number") {
if (res.slice(0, 2) === ", ") res = res.slice(2);
if (undef && res[0] === ":") res = res.slice(1);
}
return res;
}