File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ) ;
You can’t perform that action at this time.
0 commit comments