Skip to content

Instantly share code, notes, and snippets.

@dinigo
Created May 19, 2017
Embed
What would you like to do?
function searchAndInsert(node, trigger, name) {
var results = [];
if (node == {})
return false;
for (child in node) {
for (t of trigger) {
console.log('child', child, 'trigger', t);
if (child == t && !child[name]) {
//console.log('node', node, 'child(trigger)', node[child],'name', child[name])
node[child][name] = {};
return true;
} else
results.push(searchAndInsert(node[child], t, name));
}
}
return results.some(el=>el);
}
//var a = {
// b: {
// c: {},
// d: {}
// },
// e: {
// f: {}
// }
//}
//searchAndInsert(a,['c','f'],'fu');
function master(root, trigger, name) {
var asigned = searchAndInsert(root, trigger, name);
if (!asigned)
root[trigger][name] = {};
}
var tree = [].concat(tagManagerSettings.events, tagManagerSettings.data).map(el=>{
console.log(el.trigger);
var t = el.trigger;
el.trigger = typeof t == 'object' ? t.map(el=>el.replace(/^dataFilled./, '')) : t;
return el;
}
).reduce((acum,curr)=>{
searchAndInsert(acum, curr.trigger, curr.name);
return acum;
}
, {
preloader: {},
ready: {},
formStep: {}
});
console.log(JSON.stringify(tree, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment