Skip to content

Instantly share code, notes, and snippets.

@Dafrok
Created January 25, 2016 05:15
Show Gist options
  • Save Dafrok/5992c33fbe5a1c256dcf to your computer and use it in GitHub Desktop.
Save Dafrok/5992c33fbe5a1c256dcf to your computer and use it in GitHub Desktop.
Check an object if all of its childrens are empty.
/*
* @file Check an object if all of its childrens are empty.
* @author Dafrok
* @param {object} obj
* @return {boolean} If all of the childrens of the target object are empty, it will be true, else, false.
*/
;var empty = (function () {
var isEmpty
function type (obj) {
return Object.prototype.toString.call(obj).split(/ |\[|\]/g)[2]
}
function walkObject (obj) {
switch (type(obj)) {
case 'Object':
for (var key in obj) {
walkObject(obj[key])
}
break
case 'Array':
for (var i = 0; i < obj.length; i++) {
walkObject(obj[i])
}
break
case 'String':
if (!obj) {
isEmpty = false
} else {
break
}
case 'Undefined':
case 'Null':
break
default:
isEmpty = false
}
}
function check (obj) {
isEmpty = true
walkObject(obj)
return isEmpty
}
return check
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment