/
props.js
56 lines (43 loc) · 1.25 KB
/
props.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// @flow
import { curry, memoize, omit, path, pluck, prop } from 'ramda';
export const keyExtractor = prop('id');
function normalizePath(edgesPath: string): Array<string> {
const accessor = edgesPath.split('.');
return accessor[accessor.length - 1] === 'edges'
? accessor
: [...accessor, 'edges'];
}
export const mapEdgesToProp = curry(
(edgePath: string, propName: string, dataObj: any) => {
// TODO: better check if we're passing a destructured data or not
const data = dataObj.data ? dataObj.data : dataObj;
const edges = path(normalizePath(edgePath), data);
let targetProp;
if (edges && edges.length) {
// $FlowFixMe$
targetProp = pluck('node', edges);
}
return {
data,
[propName]: targetProp,
};
},
);
export const flattenEdges = memoize((connection: ?Object) => {
if (!connection) {
return [];
}
const edges = prop('edges', connection);
if (!edges || !edges.length) {
return [];
}
return pluck('node', edges).map(node => {
Object.keys(node).forEach(key => {
if (path([key, 'edges'], node)) {
// eslint-disable-next-line no-param-reassign
node[key] = flattenEdges(prop(key, node));
}
});
return omit('edges', node);
});
});