Skip to content

Instantly share code, notes, and snippets.

@digitalicarus
Last active December 22, 2015 04:48
Show Gist options
  • Select an option

  • Save digitalicarus/6419344 to your computer and use it in GitHub Desktop.

Select an option

Save digitalicarus/6419344 to your computer and use it in GitHub Desktop.
flatter
function flatter (obj, match, replaceWhat, replaceWith) {
var replace = (replaceWhat && replaceWhat instanceof RegExp && typeof replaceWith === 'string')
, tmp = null // description - a tmp variable
;
function getJuicyObject (obj) {
var children = {}
, juicyChildren = {}
;
// recurse over all JavaScripts children, but honor those who conform to the mighty regex
for (i in obj) {
if (obj.hasOwnProperty(i)) {
if (typeof obj[i] === 'object') {
children[i] = obj[i];
}
if (match.test(i)) {
tmp = (replace) ? i.replace(replaceWhat, replaceWith) : i;
juicyChildren[tmp] = obj[i];
}
}
}
// no more children, just return the juicy ones
if (isEmptyObj(children)) {
return juicyChildren;
}
// merge any subsequent juicy children with current
for (i in children) {
juicyChildren = aug(juicyChildren, getJuicyObject(children[i]));
}
return juicyChildren;
}
return getJuicyObject(obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment