Last active
August 29, 2015 14:16
-
-
Save laser/0523969397e7e7623e9a to your computer and use it in GitHub Desktop.
What do I name these things
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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': {} | |
} | |
} | |
} | |
*/ |
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