-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path2-extended.js
More file actions
35 lines (26 loc) · 910 Bytes
/
2-extended.js
File metadata and controls
35 lines (26 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
const partial = (fn, ...args) => (...rest) => fn(...args, ...rest);
// Projection
const projection = (meta, obj) => Object.keys(meta)
.reduce((hash, key) => (hash[key] = meta[key]
.reduce((val, fn, i) => (i ? fn(val) : obj[fn]), null), hash), {});
// Dataset
const persons = [
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
{ name: 'Ibn Arabi', city: 'Murcia', born: 1165 },
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1893 },
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 },
];
// Metadata
const md = {
name: ['name'],
place: ['city', (s) => '<' + s.toUpperCase() + '>'],
age: ['born', (year) => (
new Date().getFullYear() - new Date(year.toString()).getFullYear()
)]
};
// Usage
const p1 = partial(projection, md);
const data = persons.map(p1);
console.dir(data);