Skip to content

Instantly share code, notes, and snippets.

@dinigo
Created May 19, 2017 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinigo/5a359ec83332b2b6dad80566af70619c to your computer and use it in GitHub Desktop.
Save dinigo/5a359ec83332b2b6dad80566af70619c to your computer and use it in GitHub Desktop.
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