UNPKG

743 BJavaScriptView Raw
1/**
2 * Traverse a moddle tree, depth first from top to bottom
3 * and call the passed visitor fn.
4 *
5 * @param {ModdleElement} element
6 * @param {Function} fn
7 */
8module.exports = function traverse(element, fn) {
9 fn(element);
10
11 var descriptor = element.$descriptor;
12
13 if (descriptor.isGeneric) {
14 return;
15 }
16
17 var containedProperties = descriptor.properties.filter(p => {
18 return !p.isAttr && !p.isReference && p.type !== 'String';
19 });
20
21 containedProperties.forEach(p => {
22 if (p.name in element) {
23 const propertyValue = element[p.name];
24
25 if (p.isMany) {
26 propertyValue.forEach(child => {
27 traverse(child, fn);
28 });
29 } else {
30 traverse(propertyValue, fn);
31 }
32 }
33 });
34};