-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
49 lines (40 loc) · 1001 Bytes
/
objects.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
// Complete the following underscore functions.
// Reference http://underscorejs.org/ for examples.
/* eslint-disable no-unused-vars, arrow-body-style, arrow-parens */
const keys = (obj) => {
return Object.keys(obj);
};
const values = (obj) => {
return Object.keys(obj).map((key) => {
return obj[key];
});
};
const mapObject = (obj, cb) => {
Object.keys(obj).forEach((key) => (obj[key] = cb(obj[key])));
return obj;
};
const pairs = (obj) => Object.keys(obj).map((key) => [key, obj[key]]);
const invert = (obj) => {
Object.keys(obj).forEach((key) => {
const newKey = obj[key];
obj[newKey] = key;
delete obj[key];
});
return obj;
};
const defaults = (obj, defaultProps) => {
Object.keys(defaultProps).forEach((key) => {
if (Object.prototype.hasOwnProperty.call(obj, key)) return;
obj[key] = defaultProps[key];
});
return obj;
};
/* eslint-enable no-unused-vars */
module.exports = {
keys,
values,
mapObject,
pairs,
invert,
defaults
};