-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize.macro.js
More file actions
159 lines (141 loc) · 4.01 KB
/
normalize.macro.js
File metadata and controls
159 lines (141 loc) · 4.01 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const { createMacro, MacroError } = require('babel-plugin-macros');
module.exports = createMacro(normalize);
/**
* @example
* ```js
* import { flatMap } from 'data-manager';
* import normalize from 'data-manager/normalize.macro';
*
* flatMap(normalize({
* x: 'a',
* y: {
* columns: ['b', 'c'],
* category: 'type'
* }
* }));
*
* // transforms at build-time into:
*
* flatMap(row => {
* return [
* { x: row['a'], y: row['b'], type: 'b' },
* { x: row['a'], y: row['c'], type: 'c' }
* ];
* });
*
* flatMap(normalize({
* x: 'a',
* y: {
* columns: ['b', 'c'],
* categories: {
* b: { isB: true, isC: false },
* b: { isB: false, isC: true }
* }
* }
* }));
*
* // transforms at build-time into:
*
* flatMap(row => {
* return [
* { x: row['a'], y: row['b'], isB: true, isC: false },
* { x: row['a'], y: row['c'], isB: false, isC: true }
* ];
* });
* ```
* @param {object} mapping fields to normalize / select
* @param {...string} [passthrough]
* @returns {function}
*/
function normalize({ references, state, babel: { template, types: t } }) {
const paths = references.default;
if (!paths || !paths.length) return;
const buildNormalize = template(`row => {
return NORMALIZE;
}`);
for (const identifier_path of paths) {
const section_path = identifier_path.parentPath;
const mapping = section_path.get('arguments.0').node;
const passthrough = section_path.get('arguments.1');
const rows = mapping.properties.reduce(
(rows, property) => {
if (t.isObjectExpression(property.value)) {
const columns_property = findByKey(
property.value.properties,
'columns'
);
const category_property = findByKey(
property.value.properties,
'category'
);
const categories_property = findByKey(
property.value.properties,
'categories'
);
const normalized = columns_property.value.elements.map(element => {
let row = [
t.objectProperty(property.key, toRow(element), property.computed)
];
if (category_property) {
row.push(
t.objectProperty(
category_property.value,
element,
category_property.computed
)
);
} else if (categories_property) {
const categories = findByKey(
categories_property.value.properties,
toKey(element)
);
row = row.concat(categories ? categories.value.properties : []);
}
return row;
});
return rows.reduce((flattened, row) => {
return flattened.concat(
normalized.map(normal => row.concat(normal))
);
}, []);
} else {
return rows.map(row =>
row.concat(
t.objectProperty(
property.key,
toRow(property.value),
property.computed
)
)
);
}
},
[[]]
);
if (passthrough) {
const elements = t.isArrayExpression(passthrough.node)
? passthrough.node.elements
: section_path.node.arguments.slice(1);
elements.forEach(element => {
const computed = !t.isStringLiteral(element);
rows.forEach(row => {
row.push(t.objectProperty(element, toRow(element), computed));
});
});
}
const NORMALIZE = t.arrayExpression(
rows.map(row => t.objectExpression(row))
);
const normalize = buildNormalize({ NORMALIZE });
section_path.replaceWith(normalize);
}
function toRow(key) {
return t.memberExpression(t.identifier('row'), key, true);
}
function toKey(node) {
return node.name || node.value;
}
function findByKey(properties, key) {
return properties.find(property => toKey(property.key) === key);
}
}