Skip to content

Commit 5e23e25

Browse files
committed
updated objects.js
1 parent 72b0507 commit 5e23e25

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/objects.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,61 @@ const keys = (obj) => {
55
// Retrieve all the names of the object's properties.
66
// Return the keys as strings in an array.
77
// Based on http://underscorejs.org/#keys
8+
return Object.keys(obj);
89
};
910

1011
const values = (obj) => {
1112
// Return all of the values of the object's own properties.
1213
// Ignore functions
1314
// http://underscorejs.org/#values
15+
const vals = Object.keys(obj).map((key) => {
16+
return obj[key];
17+
});
18+
return vals;
1419
};
1520

1621
const mapObject = (obj, cb) => {
1722
// Like map for arrays, but for objects. Transform the value of each property in turn.
1823
// http://underscorejs.org/#mapObject
24+
Object.keys(obj).forEach(key => (obj[key] = cb(obj[key])));
25+
return obj;
1926
};
2027

2128
const pairs = (obj) => {
2229
// Convert an object into a list of [key, value] pairs.
2330
// http://underscorejs.org/#pairs
31+
const keyValue = (Object.keys(obj).map(key => ([key, obj[key]])));
32+
return keyValue;
2433
};
25-
2634
const invert = (obj) => {
2735
// Returns a copy of the object where the keys have become the values and the values the keys.
2836
// Assume that all of the object's values will be unique and string serializable.
2937
// http://underscorejs.org/#invert
38+
// Object.keys(obj).forEach((key) => {
39+
// const newKey = obj[key];
40+
// obj[newKey] = key;
41+
// delete obj[key];
42+
// });
43+
// return obj;
44+
Object.keys(obj).forEach((key) => {
45+
const newKey = obj[key];
46+
obj[newKey] = key;
47+
delete obj[key];
48+
});
49+
return obj;
3050
};
3151

3252
const defaults = (obj, defaultProps) => {
3353
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
3454
// Return `obj`.
3555
// http://underscorejs.org/#defaults
56+
Object.keys(defaultProps).forEach((key) => {
57+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
58+
return;
59+
}
60+
obj[key] = defaultProps[key];
61+
});
62+
return obj;
3663
};
3764

3865
/* eslint-enable no-unused-vars */

0 commit comments

Comments
 (0)