Last active
December 22, 2015 04:48
-
-
Save digitalicarus/6419344 to your computer and use it in GitHub Desktop.
flatter
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 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