Skip to content

Commit 4190ff4

Browse files
committed
Refactored JavaScript examples, added cached projection
1 parent fcd067b commit 4190ff4

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.log
3+
.DS_Store

JavaScript/3-cached.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
let persons = [
2+
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
3+
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
4+
{ name: 'Ibn Arabi', city: 'Murcia', born: 1165 },
5+
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1596 },
6+
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 }
7+
];
8+
9+
let md = {
10+
name: ['name'],
11+
place: ['city', upper, s => '<' + s + '>'],
12+
age: ['born', age]
13+
};
14+
15+
function projection(meta) {
16+
let keys = Object.keys(meta);
17+
return obj => keys.reduce((hash, key) => (
18+
hash[key] = meta[key].reduce(
19+
(val, fn, i) => i === 0 ? obj[fn] : fn(val), null
20+
), hash
21+
), {});
22+
};
23+
24+
let p1 = projection(md);
25+
let data = persons.map(p1);
26+
console.dir(data);
27+
28+
function capitalize(s) {
29+
return s.replace(/\w+/g, function(word) {
30+
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
31+
});
32+
}
33+
34+
function upper(s) {
35+
return typeof(s) === 'string' ? s.toUpperCase() : '';
36+
}
37+
38+
function age(year) {
39+
return new Date().getFullYear() - new Date(year + '').getFullYear();
40+
}
41+
42+
function inc(x) {
43+
return ++x;
44+
}

0 commit comments

Comments
 (0)