Repository navigation
Expand file tree
/
Copy pathobjects.js
More file actions
68 lines (59 loc) · 1.52 KB
/
Copy pathobjects.js
File metadata and controls
68 lines (59 loc) · 1.52 KB
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
// Complete the following underscore functions.
// Reference http://underscorejs.org/ for examples.
const keys = (obj) => {
const newArray = [];
Object.keys(obj).forEach((key, index) => {
newArray[index] = key;
});
return newArray;
};
const values = (obj) => {
return Object.keys(obj).map((key) => {
return obj[key];
});
};
const mapObject = (obj, cb) => {
const newObject = {};
Object.keys(obj).forEach((key) => {
newObject[key] = cb(obj[key]);
});
return newObject;
};
const pairs = (obj) => {
const newArray = [];
Object.keys(obj).forEach((key, index) => {
newArray[index] = [key, obj[key]];
});
return newArray;
};
/* STRETCH PROBLEMS */
const invert = (obj) => {
// Returns a copy of the object where the keys have become the values and the values the keys.
// Assume that all of the object's values will be unique and string serializable.
// http://underscorejs.org/#invert
Object.keys(obj).forEach((key) => {
const newKey = obj[key];
obj[newKey] = key;
delete obj[key];
});
return obj;
};
const defaults = (obj, defaultProps) => {
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// http://underscorejs.org/#defaults
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,
};