Skip to content

Commit 1a511d4

Browse files
committed
Improved JavaScript examples
1 parent 4190ff4 commit 1a511d4

5 files changed

Lines changed: 65 additions & 3 deletions

File tree

.jshintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"esversion": 6,
3+
"node": true
4+
}

JavaScript/1-simple.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
Function.prototype.curry = function(...args) {
24
return this.bind(null, ...args);
35
};

JavaScript/2-extended.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strinct';
2+
13
Function.prototype.curry = function(...args) {
24
return this.bind(null, ...args);
35
};
@@ -6,7 +8,7 @@ let persons = [
68
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
79
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
810
{ name: 'Ibn Arabi', city: 'Murcia', born: 1165 },
9-
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1596 },
11+
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1893 },
1012
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 }
1113
];
1214

JavaScript/3-cached.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
'use strinct';
2+
13
let persons = [
24
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
35
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
46
{ name: 'Ibn Arabi', city: 'Murcia', born: 1165 },
5-
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1596 },
7+
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1893 },
68
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 }
79
];
810

@@ -19,7 +21,7 @@ function projection(meta) {
1921
(val, fn, i) => i === 0 ? obj[fn] : fn(val), null
2022
), hash
2123
), {});
22-
};
24+
}
2325

2426
let p1 = projection(md);
2527
let data = persons.map(p1);

JavaScript/4-optimized.js

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

0 commit comments

Comments
 (0)