Check if it is empty regardless of type, javascript

4

I'm trying to create a function that checks for something blank, empty, undefined, null etc.

But my difficulty is that I do not know all the forms of a string being "blank"

I created this function

function empty() {
   var args = [].slice.call(arguments);

    args.forEach(function(argument) {
        if(!argument || 0 === argument.length || !argument || /^\s*$/.test(argument) || argument.length === 0 || !argument.trim()) {
            return false;
        } 
    });
}

The intention is to return false so that I can simply check if my variables have some blank in this way if(!empty(var1, var2...)) {

But there are several ways, as I said, for example in my doubts:

If it is an array, how do you know if it is empty?

If it's a JSON how do you know if it's empty?

If it's only numbers, how can I tell if it's empty?

There are so many ways you have to check that I get lost ..

When I say empty I mean undefined, null, filled with only empty spaces etc.

    
asked by anonymous 03.10.2015 / 15:43

2 answers

3

There is no "empty" value for numbers and booleans, unless you define some. This "empty" value in these cases would be null , or undefined .

I'm assuming that:

  • null is empty;
  • undefined is empty;
  • objects without fields are empty (including inherited);
  • Arrays without elements are empty;
  • strings of size 0 are empty;
  • strings consisting of blank characters are empty;
  • There is no specific numeric or boolean value to represent empty (could be 0 or false , maybe).

So it could be written like this:

function empty() {
    var args = Array.prototype.slice.call(arguments);

    return args.some(function(argument) {
        return argument === null
            || typeof argument === "undefined"
            || (Array.isArray(argument) && argument.length === 0)
            || (typeof argument === "object" && emptyObject(argument))
            || (typeof argument === "string" && (argument.length === 0 || argument.trim().length === 0));
    });
}

function emptyObject(arg) {
    var count = 0;
    for ( var i in arg ) {
        count++;
        break;
    }
    return count === 0;
}

The emptyObject method checks how many fields an object has. I do not know any other way to do that.

The method call Array.some is used so that when a single empty item is found, the method returns, not checking all other items.

No tested, so there may be some errors; D

Improvements are welcome!

    
03.10.2015 / 16:34
2

In Javascript, the fastest way to validate the result of an expression including the most commonly used types of values representing a non-value is double negation . More details on this answer .

The possible values that can be typecast to true / false are:

  • false
  • NaN
  • undefined
  • null
  • "" (empty string)
  • 0

Some examples of double negation validation (taken from this answer ):

          !!false === false
           !!true === true

              !!0 === false
!!parseInt("foo") === false // NaN
              !!1 === true
             !!-1 === true  // -1 é verdadeiro

             !!"" === false // string vazia é 'falsa'
          !!"foo" === true  // string não-vazia é 'verdadeira'
        !!"false" === true  // ...mesmo se conter o valor "false"

     !!window.foo === false // undefined é falso
           !!null === false // null também

             !!{} === true  // um objeto vazio é 'verdadeiro';
             !![] === true  // um array vazio também.
    
04.10.2015 / 03:29