Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Created January 17, 2017 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JDMcKinstry/de076d2ca99f086955f769c43982feb4 to your computer and use it in GitHub Desktop.
Save JDMcKinstry/de076d2ca99f086955f769c43982feb4 to your computer and use it in GitHub Desktop.
/** isEmpty(varried)
* Simple method for testing if item is "empty"
**/
;(function() {
/**
* !a Basic empty check. Takes care of 90% of items passed through.
* 0 >= a Catchs '0' strings and empty Arrays
*
* /\{\}/.test(JSON.stringify(a).replace(/"[^"]*":(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '').replace(/"[^"]*":\{\},?/g, '')) // to determine Blank Object
* /|\[(null(,)*)*\]/.test(JSON.stringify(a).replace(/(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '')) // to determine Blank Array
**/
function isEmpty(a) {
if (!a || 0 >= a) return !0;
if ("object" == typeof a) {
var b = JSON.stringify(a).replace(/"[^"]*":(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '').replace(/"[^"]*":\{\},?/g, '');
if ( /^$|\{\}|\[\]/.test(b) ) return !0;
else if (a instanceof Array) {
b = b.replace(/(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '');
if ( /^$|\{\}|\[\]/.test(b) ) return !0;
}
}
return false;
}
window.hasOwnProperty("empty")||(window.empty=isEmpty);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment