|
2 | 2 | // Reference http://underscorejs.org/ for examples. |
3 | 3 |
|
4 | 4 | const keys = (obj) => { |
| 5 | + return Object.getOwnPropertyNames(obj); |
5 | 6 | // Retrieve all the names of the object's properties. |
6 | 7 | // Return the keys as strings in an array. |
7 | 8 | // Based on http://underscorejs.org/#keys |
8 | 9 | }; |
9 | 10 |
|
10 | 11 | const values = (obj) => { |
| 12 | + const tvalues = []; |
| 13 | + const thekeys = keys(obj); |
| 14 | + for (let i = 0; i < thekeys.length; i++) { |
| 15 | + if (typeof (obj[thekeys[i]]) === 'function'); |
| 16 | + else { |
| 17 | + tvalues.push(obj[thekeys[i]]); |
| 18 | + } |
| 19 | + } |
| 20 | + return tvalues; |
11 | 21 | // Return all of the values of the object's own properties. |
12 | 22 | // Ignore functions |
13 | 23 | // http://underscorejs.org/#values |
14 | 24 | }; |
15 | 25 |
|
16 | 26 | const mapObject = (obj, cb) => { |
| 27 | + const mappedobj = {}; |
| 28 | + const thekeys = keys(obj); |
| 29 | + const thevalues = values(obj); |
| 30 | + for (let i = 0; i < thekeys.length; i++) { |
| 31 | + mappedobj[thekeys[i]] = cb(thevalues[i]); |
| 32 | + } |
| 33 | + return mappedobj; |
17 | 34 | // Like map for arrays, but for objects. Transform the value of each property in turn. |
18 | 35 | // http://underscorejs.org/#mapObject |
19 | 36 | }; |
20 | 37 |
|
21 | 38 | const pairs = (obj) => { |
| 39 | + const thekeys = keys(obj); |
| 40 | + const thevalues = values(obj); |
| 41 | + const pairarray = []; |
| 42 | + for (let i = 0; i < thekeys.length; i++) { |
| 43 | + pairarray.push([thekeys[i], thevalues[i]]); |
| 44 | + } |
| 45 | + return pairarray; |
22 | 46 | // Convert an object into a list of [key, value] pairs. |
23 | 47 | // http://underscorejs.org/#pairs |
24 | 48 | }; |
25 | 49 |
|
26 | 50 | const invert = (obj) => { |
| 51 | + const newobj = {}; |
| 52 | + const thekeys = keys(obj); |
| 53 | + const thevalues = values(obj); |
| 54 | + for (let i = 0; i < thekeys.length; i++) { |
| 55 | + newobj[thevalues[i]] = thekeys[i]; |
| 56 | + } |
| 57 | + return newobj; |
27 | 58 | // Returns a copy of the object where the keys have become the values and the values the keys. |
28 | 59 | // Assume that all of the object's values will be unique and string serializable. |
29 | 60 | // http://underscorejs.org/#invert |
30 | 61 | }; |
31 | 62 |
|
32 | 63 | const defaults = (obj, defaultProps) => { |
| 64 | + const defaultkeys = keys(defaultProps); |
| 65 | + const defaultvalues = values(defaultProps); |
| 66 | + const thekeys = keys(obj); |
| 67 | + for (let i = 0; i < defaultkeys.length; i++) { |
| 68 | + if (Object.prototype.hasOwnProperty.call(obj, defaultkeys[i])); |
| 69 | + else { |
| 70 | + obj[defaultkeys[i]] = defaultvalues[i]; |
| 71 | + } |
| 72 | + } |
| 73 | + return obj; |
33 | 74 | // Fill in undefined properties that match properties on the `defaultProps` parameter object. |
34 | 75 | // Return `obj`. |
35 | 76 | // http://underscorejs.org/#defaults |
|
0 commit comments