|
| 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