Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Underscore.js / Lodash Mixin to deep compact an object
/*The MIT License (MIT)
Copyright (c) 2014 Kurt Milam - http://xioup.com | Source: https://gist.github.com/kurtmilam/886b705fb0eb03001c77
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.
*/
// returns a copy of the original object from which all empty keys, objects and arrays have been deleted
// currently, it's only meant to work with objects that could have been created by JSON.parse\
// Thanks to ljharb ( https://github.com/ljharb ) for the tips
function deepCompact (object) {
var oType = typeof object
if (object !== null && oType != 'undefined') {
if ((object instanceof Object || Array.isArray(object))
&& !(object instanceof Date)) {
var ret
if (object instanceof Array) {
ret = object.map(function (val, key, col) {
return deepCompact(val)
})
if (ret.length > 0) {
return ret
}
} else {
var keys = Object.keys(object)
ret = keys.reduce(function (acc, val, key) {
var tmp = deepCompact(object[val])
if (typeof tmp != 'undefined') {
acc[val] = tmp
}
return acc
}, {})
if (typeof ret == 'object' && Object.keys(ret).length > 0) {
return ret
}
}
} else if (oType == 'string' || oType == 'boolean' || oType == 'number') {
return object
} else if (object instanceof Date) {
return String(object)
} else if (!(object instanceof Object) && !(object instanceof Array)) {
return typeof object
}
}
}
console.clear()
_.mixin({ deepCompact: deepCompact })
var input = {a:1,b:{c:[],d:{}},f:new Date(),g:function(){},h:[1]}
var result = deepCompact(input)
console.log(input)
//-> Object {a: 1, b: Object, f: Sat Aug 02 2014 22:23:41 GMT+0200 (W. Europe Daylight Time), g: function, h: Array[1]}
console.log(result)
//-> Object {a: 1, f: "Sat Aug 02 2014 22:23:41 GMT+0200 (W. Europe Daylight Time)", g: "function", h: Array[1]}
// See here for working fiddle: http://jsfiddle.net/texinwien/7qw4Z/ (all the action takes place in the console)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.