Created
January 25, 2016 05:15
-
-
Save Dafrok/5992c33fbe5a1c256dcf to your computer and use it in GitHub Desktop.
Check an object if all of its childrens are empty.
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
/* | |
* @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