-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
40 lines (33 loc) · 1.01 KB
/
utils.js
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
const mapValues = require('lodash/mapValues');
const set = require('lodash/set');
const get = require('lodash/get');
const isObject = require('lodash/isObject');
const mapObject = (obj, fn) =>
mapValues(obj, (v, k) => (isObject(v) ? mapObject(v, fn) : fn(v, k, obj)));
const genConfigFromDoc = tree =>
tree.reduce((acc, field) => {
if (field.kind === 'Field' && field.selectionSet) {
acc[field.name.value] = genConfigFromDoc(field.selectionSet.selections);
}
if (field.directives && field.directives.length > 0) {
const directive = field.directives
.find(d => d.name.value === 'computed')
.arguments.find(arg => arg.name.value === 'value');
acc[field.name.value] = directive.value.value;
}
return acc;
}, {});
const setComputedProperty = (val, key, obj, data) => {
set(
obj,
key,
val.replace(/\$(\w+\.)+\w+/g, match =>
get(data, `data.${match.substring(1)}`)
)
);
};
module.exports = {
mapObject,
genConfigFromDoc,
setComputedProperty,
};