Skip to content

Instantly share code, notes, and snippets.

@laser
Last active August 29, 2015 14:16
Show Gist options
  • Save laser/0523969397e7e7623e9a to your computer and use it in GitHub Desktop.
Save laser/0523969397e7e7623e9a to your computer and use it in GitHub Desktop.
What do I name these things
function derp(expansions) {
var o = {};
// parse a period-delimited string
// and use the resulting array to
// add keys to an object.
function f1(acc, item) {
f2(acc, item.split('.'));
return acc;
}
// drills into an object, adding properties
// along the way. mutates the original object.
function f2(o, xs) {
if (xs.length === 0) {
return;
}
else {
var x = xs.shift();
o[x] = o[x] || {};
f2(o[x], xs);
}
}
return _.reduce(expansions.split(','), f1, o);
};
derp('Foo');
/*
{
'Foo': {}
}
*/
derp('Foo.Bar.Baz,Foo,Qux,Ding.Dong')
/*
{
'Qux': {},
'Ding': {
'Dong': {}
},
'Foo': {
'Bar': {
'Baz': {}
}
}
}
*/
@laser
Copy link
Author

laser commented Mar 5, 2015

  • reconstitute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment