Repository navigation
Expand file tree
/
Copy pathobjects.js
More file actions
69 lines (59 loc) · 2.01 KB
/
Copy pathobjects.js
File metadata and controls
69 lines (59 loc) · 2.01 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
69
// Complete the following underscore functions.
// Reference http://underscorejs.org/ for examples.
/* ************ NOTE TO EVALUATORS *******************
Given time, I'd like to find a way to use Object.entries
and iterate over the resulting
2-D Array to find keys, values
I like objects as much as Ryan likes arrays :D
See: https://repl.it/@wj68yz/Troy-Bradley-ScratchPad-for-Lambda-JS-I-Objects
and https://repl.it/@wj68yz/Troy-Bradley-More-Scratching-at-forEach-in-JS-I-Objects
*/
const keys = (obj) => {
// Retrieve all the names of the object's properties.
// Return the keys as strings in an array.
// Based on http://underscorejs.org/#keys
return Object.getOwnPropertyNames(obj);
};
const values = (obj) => {
// Return all of the values of the object's own properties.
// Ignore functions
// http://underscorejs.org/#values
return Object.values(obj);
};
const mapObject = (obj, cb) => {
// Like map for arrays, but for objects. Transform the value of each property in turn.
// http://underscorejs.org/#mapObject
Object.keys(obj).forEach(key => (obj[key] = cb(obj[key])));
return obj;
};
const pairs = (obj) => {
// Convert an object into a list of [key, value] pairs.
// http://underscorejs.org/#pairs
// flatten (Object.entries(obj)); ???
return Object.entries(obj);
};
/* 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
// const flip = {};
// for (const key in obj) { // try to find a way to use forEach
// flip[obj[key]] = key;
// }
// return flip;
};
const defaults = (obj, defaultProps) => {
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
// Return `obj`.
// http://underscorejs.org/#defaults
};
/* eslint-enable no-unused-vars */
module.exports = {
keys,
values,
mapObject,
pairs,
invert,
defaults,
};