Skip to content

Commit 293392b

Browse files
committed
Code optimizations
1 parent b05d0f8 commit 293392b

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

JavaScript/6-details.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const projection = (meta) => {
2121
mapper.join = (key, projection) => {
2222
keys.push(key);
2323
meta[key] = [key, val => val.map(projection)];
24-
console.dir({ meta });
2524
return mapper;
2625
};
2726
return mapper;

JavaScript/8-syntax.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,38 @@
22

33
// Projection
44

5-
const transforms = {
6-
string: name => d => d[name],
7-
function: fn => d => fn(d),
8-
object: name => proj => d => {
9-
const data = d[name];
10-
if (!data) return data;
11-
return data.map(projection(proj));
12-
}
13-
};
14-
155
const projection = (metadata) => {
166
const meta = {};
17-
let item, key, type, transform;
7+
let item, key, type, cast;
188
for (item of metadata) {
199
type = typeof(item);
20-
transform = transforms[type];
10+
cast = projection[type];
2111
if (type === 'string') key = item;
22-
if (type === 'object') transform = transform(key);
23-
meta[key] = transform(item);
12+
if (type === 'object') cast = cast(key);
13+
meta[key] = cast(item);
2414
}
2515
const keys = Object.keys(meta);
2616
const mapper = obj => {
2717
const hash = {};
28-
let key, value, transform;
18+
let key, value, cast;
2919
for (key of keys) {
30-
transform = meta[key];
31-
value = transform(obj);
20+
cast = meta[key];
21+
value = cast(obj);
3222
if (value) hash[key] = value;
3323
}
3424
return hash;
3525
};
3626
return mapper;
3727
};
3828

29+
projection.string = name => d => d[name];
30+
projection.function = fn => d => fn(d);
31+
projection.object = name => proj => d => {
32+
const data = d[name];
33+
if (!data) return data;
34+
return data.map(projection(proj));
35+
};
36+
3937
// Dataset
4038

4139
const persons = [
@@ -64,16 +62,16 @@ const persons = [
6462

6563
const md = [
6664
'name',
67-
'places', [
68-
'address', d => (d.country.toUpperCase() + ', '+ d.name),
69-
'population'
70-
],
7165
'place', d => '<' + d.city.toUpperCase() + '>',
7266
'born',
7367
'age', d => (
7468
new Date().getFullYear() -
7569
new Date(d.born + '').getFullYear()
76-
)
70+
),
71+
'places', [
72+
'address', d => (d.country.toUpperCase() + ', ' + d.name),
73+
'population'
74+
]
7775
];
7876

7977
// Usage

0 commit comments

Comments
 (0)