Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating a non-empty String in Lo-Dash [duplicate]

I saw this question, but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for ""?

like image 562
casademora Avatar asked Sep 30 '08 17:09

casademora


People also ask

How do I check if a Lodash string is empty?

The Lodash _. isEmpty() Method Checks if the value is an empty object, collection, map, or set. Objects are considered empty if they have no own enumerable string keyed properties. Collections are considered empty if they have a 0 length.

How do you check if a string is empty or not?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

Does Lodash isEmpty check for NULL?

Return value This method returns true if the value is empty, and vice-versa. If the value is a boolean or null , it will always return true . If the value is a collection , this method will evaluate its length to determine if it's empty.

How do you check if a string is not empty in C#?

In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.


3 Answers

If you just want to check whether there's a truthy value, you can do:

if (strValue) {
    //do something
}

If you need to check specifically for an empty string over null, I would think checking against "" is your best bet, using the === operator (so that you know that it is, in fact, a string you're comparing against).

if (strValue === "") {
    //...
}
like image 81
bdukes Avatar answered Sep 30 '22 15:09

bdukes


For checking if a variable is falsey or if it has length attribute equal to zero (which for a string, means it is empty), I use:

function isEmpty(str) {
    return (!str || str.length === 0 );
}

(Note that strings aren't the only variables with a length attribute, arrays have them as well, for example.)

For checking if a variable is falsey or if the string only contains whitespace or is empty, I use:

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}

If you want, you can monkey-patch the String prototype like this:

String.prototype.isEmpty = function() {
    // This doesn't work the same way as the isEmpty function used 
    // in the first example, it will return true for strings containing only whitespace
    return (this.length === 0 || !this.trim());
};
console.log("example".isEmpty());

Note that monkey-patching built-in types is controversial, as it can break code that depends on the existing structure of built-in types, for whatever reason.

like image 39
Jano González Avatar answered Sep 30 '22 16:09

Jano González


All the previous answers are good, but this will be even better. Use dual NOT operators (!!):

if (!!str) {
    // Some code here
}

Or use type casting:

if (Boolean(str)) {
    // Code here
}

Both do the same function. Typecast the variable to Boolean, where str is a variable.

  • It returns false for null, undefined, 0, 000, "", false.

  • It returns true for all string values other than the empty string (including strings like "0" and " ")

like image 22
karthick.sk Avatar answered Sep 30 '22 17:09

karthick.sk