Skip to content

Commit f0d43fb

Browse files
committed
Extract mapping example to separate file
1 parent 3a30d10 commit f0d43fb

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

JavaScript/5-instance.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,3 @@ const person = (name, born) => () => `${name} was born in ${born} ${era(born)}`;
3434

3535
const marcus2 = person('Marcus Aurelius', 121, 'Roma');
3636
console.log(marcus2());
37-
38-
// Object reflection
39-
40-
const omap = (obj, fn) => Object.keys(obj)
41-
.reduce((res, key) => {
42-
const [prop, val] = fn(key, obj[key]);
43-
res[prop] = val;
44-
return res;
45-
}, {});
46-
47-
const marcus3 = {
48-
firstName: 'Marcus',
49-
middleName: 'Aurelius',
50-
lastName: 'Antoninus',
51-
};
52-
53-
const marcus4 = omap(marcus3, (key, val) => {
54-
const prop = key.toLowerCase().replace('name', '');
55-
const value = val.toUpperCase();
56-
return [prop, value];
57-
});
58-
console.log(marcus4);

JavaScript/6-mapping.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
// Map object instead of key iteration
4+
5+
// Imperative
6+
7+
const marcus1 = {
8+
firstName: 'Marcus',
9+
middleName: 'Aurelius',
10+
lastName: 'Antoninus',
11+
};
12+
13+
const marcus2 = {};
14+
15+
for (const key in marcus1) {
16+
const prop = key.toLowerCase().replace('name', '');
17+
const value = marcus1[key].toUpperCase();
18+
marcus2[prop] = value;
19+
}
20+
console.log(marcus2);
21+
22+
// Functional
23+
24+
const omap = (obj, fn) => Object.keys(obj)
25+
.reduce((res, key) => {
26+
const [prop, val] = fn(key, obj[key]);
27+
res[prop] = val;
28+
return res;
29+
}, {});
30+
31+
const marcus3 = {
32+
firstName: 'Marcus',
33+
middleName: 'Aurelius',
34+
lastName: 'Antoninus',
35+
};
36+
37+
const marcus4 = omap(marcus3, (key, val) => {
38+
const prop = key.toLowerCase().replace('name', '');
39+
const value = val.toUpperCase();
40+
return [prop, value];
41+
});
42+
console.log(marcus4);

0 commit comments

Comments
 (0)