Last active
July 31, 2018 19:17
-
-
Save kurtmilam/7006dc1c2123787f9679 to your computer and use it in GitHub Desktop.
Underscore/Lodash slenderize / flatten object mixin
This file contains 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
/* Copyright (C) 2014 Kurt Milam - http://xioup.com | Source: https://gist.github.com/kurtmilam/7006dc1c2123787f9679 | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
**/ | |
// This function takes an arbitrarily deeply nested object and returns a flat 'copy', | |
// an object with no nesting, just properties, where each property's identifier is a | |
// dot notation string representation of the path to the property on the original | |
// object of which it's a copy (see the working sample code at the end) | |
// Working Fiddle here: http://jsfiddle.net/texinwien/XAf7P/4/ | |
// Scroll to the bottom to see the working sample code | |
function slenderizeObject (fatObject) { | |
var _propertyIdentifiers = [] | |
var _slenderObject = {} | |
function processNode (theNode, _propertyIdentifiers, _slenderObject) { | |
theNode = theNode || {} | |
_propertyIdentifiers = _propertyIdentifiers || [] | |
var ret = _(theNode) | |
.map( | |
function (value, key) { | |
var myKeys = _.clone(_propertyIdentifiers), | |
ret = {} | |
myKeys.push(key) | |
// if value is a string, number or boolean | |
if (_.isString(value) || _.isNumber(value) || _.isBoolean(value)) { | |
// build a keyString to use as a property identifier | |
var keyString = myKeys.join('.') | |
// add a property with that identifier and childNode as the value to our return object | |
ret[keyString] = _slenderObject[keyString] = value | |
} else { | |
// Call processNode recursively if value isn't a leaf node type (string, number or boolean) | |
processNode(value, myKeys, _slenderObject) | |
ret = value | |
} | |
return ret; | |
}, this | |
) | |
.value() | |
return ret | |
} | |
processNode(fatObject, _propertyIdentifiers, _slenderObject) | |
return _slenderObject | |
} | |
_.mixin({ 'slenderizeObject': slenderizeObject }) | |
//Sample code starts here: | |
var fat = { | |
name: 'fat object', | |
food: [ | |
{ | |
cookie: { | |
calories: 500, | |
healthy: false, | |
remark: 'yummy!' | |
} | |
}, | |
{ | |
celery: | |
{ | |
calories: 50, | |
healthy: true, | |
remark: 'crunchy' | |
} | |
} | |
] | |
} | |
var slender = _.slenderizeObject(fat) | |
console.log(slender) | |
/* | |
outputs: | |
{ | |
"food.0.cookie.calories": 500, | |
"food.0.cookie.healthy": false, | |
"food.0.cookie.remark": "yummy!", | |
"food.1.celery.calories": 50, | |
"food.1.celery.healthy": true, | |
"food.1.celery.remark": "crunchy", | |
"name": "fat object" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment