Skip to content

Commit cfae217

Browse files
committed
Code style
1 parent 6c6f840 commit cfae217

File tree

5 files changed

+90
-63
lines changed

5 files changed

+90
-63
lines changed

JavaScript/1-simple.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const projection = (fields, obj) => (
88
.reduce((hash, key) => (hash[key] = obj[key], hash), {})
99
);
1010

11+
// Dataset
12+
1113
const persons = [
1214
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
1315
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
@@ -16,6 +18,8 @@ const persons = [
1618
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 }
1719
];
1820

21+
// Usage
22+
1923
const p1 = curry(projection, ['name', 'born']);
2024
const p2 = curry(projection, ['name']);
2125

JavaScript/2-extended.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
const curry = (fn, x) => (...args) => fn(x, ...args);
44

5+
const projection = (meta, obj) => (
6+
Object.keys(meta).reduce((hash, key) => (
7+
hash[key] = meta[key].reduce(
8+
(val, fn, i) => (i === 0 ? obj[fn] : fn(val)), null
9+
), hash
10+
), {})
11+
);
12+
13+
const upper = (s) => (
14+
typeof(s) === 'string' ? s.toUpperCase() : ''
15+
);
16+
17+
const age = (year) => (
18+
new Date().getFullYear() - new Date(year + '').getFullYear()
19+
);
20+
21+
// Dataset
22+
523
const persons = [
624
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
725
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
@@ -10,28 +28,15 @@ const persons = [
1028
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 }
1129
];
1230

31+
// Metadata
32+
1333
const md = {
1434
name: ['name'],
1535
place: ['city', upper, s => '<' + s + '>'],
1636
age: ['born', age]
1737
};
1838

19-
const projection = (meta, obj) => (
20-
Object.keys(meta).reduce((hash, key) => (
21-
hash[key] = meta[key].reduce(
22-
(val, fn, i) => (i === 0 ? obj[fn] : fn(val)), null
23-
), hash
24-
), {})
25-
);
2639

2740
const p1 = curry(projection, md);
2841
const data = persons.map(p1);
2942
console.dir(data);
30-
31-
function upper(s) {
32-
return typeof(s) === 'string' ? s.toUpperCase() : '';
33-
}
34-
35-
function age(year) {
36-
return new Date().getFullYear() - new Date(year + '').getFullYear();
37-
}

JavaScript/3-cached.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
'use strict';
22

3+
const projection = (meta) => {
4+
const keys = Object.keys(meta);
5+
return obj => keys.reduce((hash, key) => (
6+
hash[key] = meta[key].reduce(
7+
(val, fn, i) => (i === 0 ? obj[fn] : fn(val)), null
8+
), hash
9+
), {});
10+
};
11+
12+
const upper = (s) => (
13+
typeof(s) === 'string' ? s.toUpperCase() : ''
14+
);
15+
16+
const age = (year) => (
17+
new Date().getFullYear() - new Date(year + '').getFullYear()
18+
);
19+
20+
// Dataset
21+
322
const persons = [
423
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
524
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
@@ -8,29 +27,16 @@ const persons = [
827
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 }
928
];
1029

30+
// Metadata
31+
1132
const md = {
1233
name: ['name'],
1334
place: ['city', upper, s => '<' + s + '>'],
1435
age: ['born', age]
1536
};
1637

17-
const projection = (meta) => {
18-
const keys = Object.keys(meta);
19-
return obj => keys.reduce((hash, key) => (
20-
hash[key] = meta[key].reduce(
21-
(val, fn, i) => (i === 0 ? obj[fn] : fn(val)), null
22-
), hash
23-
), {});
24-
};
38+
// Usage
2539

2640
const p1 = projection(md);
2741
const data = persons.map(p1);
2842
console.dir(data);
29-
30-
function upper(s) {
31-
return typeof(s) === 'string' ? s.toUpperCase() : '';
32-
}
33-
34-
function age(year) {
35-
return new Date().getFullYear() - new Date(year + '').getFullYear();
36-
}

JavaScript/4-imperative.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
'use strict';
22

3+
const projection = (meta) => {
4+
const keys = Object.keys(meta);
5+
return obj => {
6+
const hash = {};
7+
keys.forEach(key => {
8+
const def = meta[key];
9+
let val = obj[def[0]];
10+
if (def.length > 1) val = def[1](val);
11+
hash[key] = val;
12+
});
13+
return hash;
14+
};
15+
};
16+
17+
const age = (year) => (
18+
new Date().getFullYear() - new Date(year + '').getFullYear()
19+
);
20+
21+
// Dataset
22+
323
const persons = [
424
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
525
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
@@ -8,30 +28,16 @@ const persons = [
828
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 }
929
];
1030

31+
// Metadata
32+
1133
const md = {
1234
name: ['name'],
1335
place: ['city', s => '<' + s.toUpperCase() + '>'],
1436
born: ['born'],
1537
age: ['born', age]
1638
};
1739

18-
function age(year) {
19-
return new Date().getFullYear() - new Date(year + '').getFullYear();
20-
}
21-
22-
const projection = (meta) => {
23-
const keys = Object.keys(meta);
24-
return obj => {
25-
const hash = {};
26-
keys.forEach(key => {
27-
const def = meta[key];
28-
let val = obj[def[0]];
29-
if (def.length > 1) val = def[1](val);
30-
hash[key] = val;
31-
});
32-
return hash;
33-
};
34-
};
40+
// Usage
3541

3642
const p1 = projection(md);
3743
const data = persons.map(p1);

JavaScript/5-optimized.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
'use strict';
22

3+
const projection = (meta) => {
4+
const keys = Object.keys(meta);
5+
return obj => {
6+
const hash = {};
7+
let name, key, def, val, fn;
8+
for (key of keys) {
9+
def = meta[key];
10+
[name, fn] = def;
11+
val = obj[name];
12+
if (fn) val = fn(val);
13+
hash[key] = val;
14+
}
15+
return hash;
16+
};
17+
};
18+
19+
// Dataset
20+
321
const persons = [
422
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
523
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
@@ -8,6 +26,8 @@ const persons = [
826
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 }
927
];
1028

29+
// Metadata
30+
1131
const md = {
1232
name: ['name'],
1333
place: ['city', s => '<' + s.toUpperCase() + '>'],
@@ -19,21 +39,7 @@ const md = {
1939
]
2040
};
2141

22-
const projection = (meta) => {
23-
const keys = Object.keys(meta);
24-
return obj => {
25-
const hash = {};
26-
let name, key, def, val, fn;
27-
for (key of keys) {
28-
def = meta[key];
29-
[name, fn] = def;
30-
val = obj[name];
31-
if (fn) val = fn(val);
32-
hash[key] = val;
33-
}
34-
return hash;
35-
};
36-
};
42+
// Usage
3743

3844
const p1 = projection(md);
3945
const data = persons.map(p1);

0 commit comments

Comments
 (0)