Skip to content

Commit bc3c98d

Browse files
committed
Add optimized example
1 parent 26b07b8 commit bc3c98d

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

JavaScript/9-optimized.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
// Projection
4+
5+
const id = x => x;
6+
7+
const projection = meta => src => meta.reduce(
8+
(dest, [name, fn = id, field = name]) =>
9+
(dest[name] = fn(src[field]), dest), {}
10+
);
11+
12+
// Dataset
13+
14+
const persons = [
15+
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
16+
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
17+
{ name: 'Ibn Arabi', city: 'Murcia', born: 1165 },
18+
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1893 },
19+
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 },
20+
];
21+
22+
// Metadata
23+
24+
const year = date => date.getFullYear();
25+
const diff = y => year(new Date()) - year(new Date(y + ''));
26+
const upper = s => s.toUpperCase();
27+
28+
const md = [
29+
['name'],
30+
['place', upper, 'city'],
31+
['age', diff, 'born'],
32+
];
33+
34+
// Usage
35+
36+
const p1 = projection(md);
37+
const data = persons.map(p1);
38+
console.dir(data);

0 commit comments

Comments
 (0)