-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.macro.js
More file actions
142 lines (126 loc) · 3.61 KB
/
match.macro.js
File metadata and controls
142 lines (126 loc) · 3.61 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
const { createMacro } = require('babel-plugin-macros');
module.exports = createMacro(match);
/**
* Construct a matcher function using MongoDB-like syntax
*
* Logical: $and, $or, $not, $nor
* Comparison: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin
* @example
* ```js
* import { filter } from 'data-manager';
* import match from 'data-manager/match.macro';
*
* filter(match({
* a: 10,
* b: { $or: { $lt: 0, $gt: 100 } },
* c: { $in: [1, 2, 3] }
* }));
*
* // transforms at build-time into:
*
* filter(function match(row) {
* return row.a === 10 && (row.b < 0 || row.b > 100) && [1, 2, 3].indexOf(row.c) >= 0
* })
* ```
* @param {object} query
* @returns {function}
*/
function match({ references, state, babel: { template, types: t } }) {
const paths = references.default;
if (!paths || !paths.length) return;
const buildMatch = template(`row => {
return LOGIC;
}`);
for (const identifier_path of paths) {
const section_path = identifier_path.parentPath;
const conditions = section_path.get('arguments.0');
const properties = conditions.node.properties;
// Create match logic
const LOGIC = properties.length
? logical.$and(undefined, properties, t)
: t.booleanLiteral(true);
const match = buildMatch({ LOGIC });
section_path.replaceWith(match);
}
}
function evaluate(context, property, t) {
const key = property.key.name || property.key.value;
if (key in logical) {
return logical[key](context, property.value.properties, t);
} else if (key in comparison) {
return comparison[key](context, property.value, t);
} else if (t.isObjectExpression(property.value)) {
return logical.$and(property.key, property.value.properties, t);
} else {
return comparison.$eq(property.key, property.value, t);
}
}
const logical = {
$and(context, properties, t) {
const conditions = properties.map(property =>
evaluate(context, property, t)
);
return joinLogic('&&', conditions, t);
},
$or(context, properties, t) {
const conditions = properties.map(property =>
evaluate(context, property, t)
);
return joinLogic('||', conditions, t);
},
$not(context, properties, t) {
return t.unaryExpression('!', logical.$and(context, properties, t));
},
$nor(context, properties, t) {
const conditions = properties.map(property =>
t.unaryExpression('!', evaluate(context, property, t))
);
return joinLogic('&&', conditions, t);
}
};
const comparison = {
$eq(key, value, t) {
return t.binaryExpression('===', row(key, t), value);
},
$ne(key, value, t) {
return t.binaryExpression('!==', row(key, t), value);
},
$gt(key, value, t) {
return t.binaryExpression('>', row(key, t), value);
},
$gte(key, value, t) {
return t.binaryExpression('>=', row(key, t), value);
},
$lt(key, value, t) {
return t.binaryExpression('<', row(key, t), value);
},
$lte(key, value, t) {
return t.binaryExpression('<=', row(key, t), value);
},
$in(key, value, t) {
return t.binaryExpression(
'>=',
t.callExpression(t.memberExpression(value, t.identifier('indexOf')), [
row(key, t)
]),
t.numericLiteral(0)
);
},
$nin(key, value, t) {
return t.unaryExpression('!', comparison.$in(key, value, t));
}
};
function joinLogic(operator, conditions, t) {
if (conditions.length === 1) {
return conditions[0];
}
return t.logicalExpression(
operator,
conditions[0],
joinLogic(operator, conditions.slice(1), t)
);
}
function row(key, t) {
const computed = !t.isIdentifier(key);
return t.memberExpression(t.identifier('row'), key, computed);
}