-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.macro.js
More file actions
77 lines (69 loc) · 1.96 KB
/
Copy pathselect.macro.js
File metadata and controls
77 lines (69 loc) · 1.96 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
const { createMacro } = require('babel-plugin-macros');
module.exports = createMacro(select);
/**
* @example
* ```js
* import { map } from 'data-manager';
* import select from 'data-manager/select.macro';
*
* map(select('Year', 'Population'));
*
* // transforms at build-time into:
*
* map(row => {
* return { 'Year': row['Year'], 'Population': row['Population'] };
* });
*
* const name = 'Population';
*
* map(select({
* year: 'Year',
* population: name
* }));
*
* // transforms at build-time into:
*
* map(row => {
* return { year: row['Year'], population: row[name] };
* });
* ```
* @param {string[] | object} mapping fields names or mapping to field name
* @returns {function}
*/
function select({ references, state, babel: { template, types: t } }) {
const paths = references.default;
if (!paths || !paths.length) return;
const buildSelect = template(`row => {
return SELECT;
}`);
for (const identifier_path of paths) {
const section_path = identifier_path.parentPath;
const mapping = section_path.get('arguments.0').node;
let properties;
if (t.isArrayExpression(mapping)) {
properties = mapping.elements.map(element => {
const computed = !t.isStringLiteral(element);
return t.objectProperty(element, row(element), computed);
});
} else if (t.isObjectExpression(mapping)) {
properties = mapping.properties.map(property => {
return t.objectProperty(
property.key,
row(property.value),
property.computed
);
});
} else {
properties = section_path.node.arguments.map(arg => {
const computed = !t.isStringLiteral(arg);
return t.objectProperty(arg, row(arg), computed);
});
}
const SELECT = t.objectExpression(properties);
const select = buildSelect({ SELECT });
section_path.replaceWith(select);
}
function row(key) {
return t.memberExpression(t.identifier('row'), key, true);
}
}