|
1 | | -// Complete the following underscore functions. |
2 | | -// Reference http://underscorejs.org/ for examples. |
3 | | - |
4 | | -const keys = (obj) => { |
5 | | - // Retrieve all the names of the object's properties. |
6 | | - // Return the keys as strings in an array. |
7 | | - // Based on http://underscorejs.org/#keys |
8 | | -}; |
9 | | - |
10 | | -const values = (obj) => { |
11 | | - // Return all of the values of the object's own properties. |
12 | | - // Ignore functions |
13 | | - // http://underscorejs.org/#values |
14 | | -}; |
15 | | - |
16 | | -const mapObject = (obj, cb) => { |
17 | | - // Like map for arrays, but for objects. Transform the value of each property in turn. |
18 | | - // http://underscorejs.org/#mapObject |
19 | | -}; |
20 | | - |
21 | | -const pairs = (obj) => { |
22 | | - // Convert an object into a list of [key, value] pairs. |
23 | | - // http://underscorejs.org/#pairs |
24 | | -}; |
25 | | - |
26 | | -const invert = (obj) => { |
27 | | - // Returns a copy of the object where the keys have become the values and the values the keys. |
28 | | - // Assume that all of the object's values will be unique and string serializable. |
29 | | - // http://underscorejs.org/#invert |
30 | | -}; |
31 | | - |
32 | | -const defaults = (obj, defaultProps) => { |
33 | | - // Fill in undefined properties that match properties on the `defaultProps` parameter object. |
34 | | - // Return `obj`. |
35 | | - // http://underscorejs.org/#defaults |
36 | | -}; |
37 | | - |
38 | | -/* eslint-enable no-unused-vars */ |
39 | | - |
40 | | -module.exports = { |
41 | | - keys, |
42 | | - values, |
43 | | - mapObject, |
44 | | - pairs, |
45 | | - invert, |
46 | | - defaults |
47 | | -}; |
| 1 | +// Complete the following underscore functions. |
| 2 | +// Reference http://underscorejs.org/ for examples. |
| 3 | + |
| 4 | +const keys = (obj) => { |
| 5 | + // Retrieve all the names of the object's properties. |
| 6 | + // Return the keys as strings in an array. |
| 7 | + // Based on http://underscorejs.org/#keys |
| 8 | + return Object.keys(obj); |
| 9 | +}; |
| 10 | + |
| 11 | +const values = (obj) => { |
| 12 | + // Return all of the values of the object's own properties. |
| 13 | + // Ignore functions |
| 14 | + // http://underscorejs.org/#values |
| 15 | + return Object.values(obj); |
| 16 | +}; |
| 17 | + |
| 18 | +const mapObject = (obj, cb) => { |
| 19 | + // Like map for arrays, but for objects. Transform the value of each property in turn. |
| 20 | + // http://underscorejs.org/#mapObject |
| 21 | + const keyArr = Object.keys(obj); |
| 22 | + const result = {}; |
| 23 | + |
| 24 | + keyArr.forEach((k) => { |
| 25 | + result[k] = cb(obj[k]); |
| 26 | + }); |
| 27 | + return result; |
| 28 | +}; |
| 29 | + |
| 30 | +const pairs = (obj) => { |
| 31 | + // Convert an object into a list of [key, value] pairs. |
| 32 | + // http://underscorejs.org/#pairs |
| 33 | + return Object.entries(obj); |
| 34 | +}; |
| 35 | + |
| 36 | +const invert = (obj) => { |
| 37 | + // Returns a copy of the object where the keys have become the values and the values the keys. |
| 38 | + // Assume that all of the object's values will be unique and string serializable. |
| 39 | + // http://underscorejs.org/#invert |
| 40 | + const keyArr = Object.keys(obj); |
| 41 | + const result = {}; |
| 42 | + |
| 43 | + keyArr.forEach(k => result[obj[k]] = k); |
| 44 | + return result; |
| 45 | +}; |
| 46 | + |
| 47 | +const defaults = (obj, defaultProps) => { |
| 48 | + // Fill in undefined properties that match properties on the `defaultProps` parameter object. |
| 49 | + // Return `obj`. |
| 50 | + // http://underscorejs.org/#defaults |
| 51 | + const keyArr = Object.keys(defaultProps); |
| 52 | + keyArr.forEach((k) => { |
| 53 | + if (!obj[k]) { |
| 54 | + obj[k] = defaultProps[k]; |
| 55 | + } |
| 56 | + }); |
| 57 | + return obj; |
| 58 | +}; |
| 59 | + |
| 60 | +/* eslint-enable no-unused-vars */ |
| 61 | + |
| 62 | +module.exports = { |
| 63 | + keys, |
| 64 | + values, |
| 65 | + mapObject, |
| 66 | + pairs, |
| 67 | + invert, |
| 68 | + defaults |
| 69 | +}; |
0 commit comments