-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
45 lines (37 loc) · 1007 Bytes
/
index.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
'use strict';
const _ = require('lodash'),
{ getComponentName } = require('clayutils');
/**
* @param {object} obj
* @param {Function} [filter=_.identity] Optional filter
* @returns {array}
*/
function listDeepObjects(obj, filter) {
let cursor, items,
list = [],
queue = [obj];
while (queue.length) {
cursor = queue.pop();
items = _.filter(cursor, _.isObject);
list = list.concat(_.filter(items, filter));
queue = queue.concat(items);
}
return list;
}
/**
* Loop through the provided object and find all
* references to other components.
*
* @param {string} [ref]
* @param {object} data
* @returns {{refs: object, components: Array}}
*/
function getIndices(ref, data) {
let components,
refs = {};
refs[ref] = data;
_.assign(refs, _.keyBy(listDeepObjects(data, '_ref'), '_ref'));
components = _.uniq(_.filter(_.map(Object.keys(refs), (ref) => getComponentName(ref)), _.identity));
return { refs, components };
}
module.exports = getIndices;