Skip to content

Commit cecd741

Browse files
committed
Add examples with formatted output
1 parent c6bbb93 commit cecd741

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

JavaScript/a-display.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
// Display
13+
14+
const max = (a, b) => (a > b ? a : b);
15+
16+
const render = meta => src => {
17+
const keys = meta.map(([name]) => name);
18+
const width = src.map(obj => keys.map(
19+
key => obj[key].toString().length
20+
));
21+
const maxWidth = keys.map(
22+
(key, i) => width.reduce((a, b) => max(a, b[i]), 0)
23+
);
24+
const dest = src.map(obj => keys.map(
25+
(key, i) => obj[key].toString().padEnd(maxWidth[i] + 4)
26+
));
27+
return dest.map(row => row.join('')).join('\n');
28+
};
29+
30+
// Dataset
31+
32+
const persons = [
33+
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
34+
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
35+
{ name: 'Ibn Arabi', city: 'Murcia', born: 1165 },
36+
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1893 },
37+
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 },
38+
];
39+
40+
// Metadata
41+
42+
const year = date => date.getFullYear();
43+
const diff = y => year(new Date()) - year(new Date(y + ''));
44+
const upper = s => s.toUpperCase();
45+
46+
const md = [
47+
['name'],
48+
['place', upper, 'city'],
49+
['age', diff, 'born'],
50+
];
51+
52+
// Usage
53+
54+
const pf = projection(md);
55+
const data = persons.map(pf);
56+
57+
const renderer = render(md);
58+
const res = renderer(data);
59+
console.log('\n' + res + '\n');

JavaScript/b-disp-opt.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// Display
13+
14+
const max = items => Math.max(...items);
15+
const maxProp = key => items => max(items.map(x => x[key]));
16+
const maxLength = maxProp('length');
17+
const col = (name, data) => data.map(obj => obj[name].toString());
18+
19+
const render = meta => src => {
20+
const keys = meta.map(([name]) => name);
21+
const maxWidth = keys.map(key => maxLength(col(key, src)));
22+
const dest = src.map(obj => maxWidth.map(
23+
(width, i) => obj[keys[i]].toString().padEnd(width + 4)
24+
));
25+
return dest.map(row => row.join('')).join('\n');
26+
};
27+
28+
// Dataset
29+
30+
const persons = [
31+
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
32+
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
33+
{ name: 'Ibn Arabi', city: 'Murcia', born: 1165 },
34+
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1893 },
35+
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 },
36+
];
37+
38+
// Metadata
39+
40+
const year = date => date.getFullYear();
41+
const diff = y => year(new Date()) - year(new Date(y + ''));
42+
const upper = s => s.toUpperCase();
43+
44+
const md = [
45+
['name'],
46+
['place', upper, 'city'],
47+
['age', diff, 'born'],
48+
];
49+
50+
// Usage
51+
52+
const pf = projection(md);
53+
const data = persons.map(pf);
54+
55+
const renderer = render(md);
56+
const res = renderer(data);
57+
console.log('\n' + res + '\n');

0 commit comments

Comments
 (0)