90

I've got this code that checks for the empty or null string. It's working in testing.

eitherStringEmpty= (email, password) ->
  emailEmpty = not email? or email is ''
  passwordEmpty = not password? or password is ''
  eitherEmpty = emailEmpty || passwordEmpty         

test1 = eitherStringEmpty "A", "B" # expect false
test2 = eitherStringEmpty "", "b" # expect true
test3 = eitherStringEmpty "", "" # expect true
alert "test1: #{test1} test2: #{test2} test3: #{test3}"

What I'm wondering is if there's a better way than not email? or email is ''. Can I do the equivalent of C# string.IsNullOrEmpty(arg) in CoffeeScript with a single call? I could always define a function for it (like I did) but I'm wondering if there's something in the language that I'm missing.

jcollum
  • 43,623
  • 55
  • 191
  • 321

11 Answers11

122

Yup:

passwordNotEmpty = not not password

or shorter:

passwordNotEmpty = !!password
thejh
  • 44,854
  • 16
  • 96
  • 107
  • 1
    This is the more "javascript-y" of the two answers. Particularly if you use the `!!` version, which is a common way to essentially cast to boolean. If it matters, this is almost certainly faster than defining a function as Jeremy suggested. – Aaron Dufour Nov 14 '11 at 22:53
  • They all work, but this one got the most upvotes. I like http://stackoverflow.com/q/8127920/30946 for readability tho. – jcollum Nov 22 '11 at 20:10
  • 3
    It fails for string with all spaces...so doesn't work for blank – Arkhitech Feb 28 '14 at 23:01
  • 1
    @Arkhitech blank is not the same thing as empty, so it works. – jcollum Apr 14 '14 at 16:50
  • 2
    Too "clever" for my taste in readability; don't think I'll use it, but good work though – PandaWood Oct 14 '14 at 02:17
  • Cool, JS goes the way of Python. Readability and Efficience Boost Squared. – Stephan Kristyn Mar 31 '15 at 08:34
37

It isn't entirely equivalent, but email?.length will only be truthy if email is non-null and has a non-zero .length property. If you not this value the result should behave as you want for both strings and arrays.

If email is null or doesn't have a .length, then email?.length will evaluate to null, which is falsey. If it does have a .length then this value will evaluate to its length, which will be falsey if it's empty.

Your function could be implemented as:

eitherStringEmpty = (email, password) ->
  not (email?.length and password?.length)
Jeremy
  • 1
  • 85
  • 340
  • 366
  • 1
    @pyrotechnick The question didn't ask about distinguishing Arrays from Strings. It asked about distinguishing null values and empty strings from non-empty strings. My point was that the code could also distinguish null values and empty arrays from non-empty arrays, but that was beyond the scope of the question. – Jeremy May 10 '12 at 04:18
  • Nevertheless, "eitherStringEmpty" is an incorrect name for the method you provided. – pyrotechnick May 10 '12 at 11:00
14

This is a case where "truthiness" comes in handy. You don't even need to define a function for that:

test1 = not (email and password)

Why does it work?

'0'       // true
'123abc'  // true
''        // false
null      // false
undefined // false
Ricardo Tomasi
  • 34,573
  • 2
  • 55
  • 66
  • 1
    I assume you have already called `.trim()` on the value as part of your validation logic. The answer is more about CS syntax. – Ricardo Tomasi Jul 01 '15 at 09:14
5
unless email? and email
  console.log 'email is undefined, null or ""'

First check if email is not undefined and not null with the existential operator, then if you know it exists the and email part will only return false if the email string is empty.

cutemachine
  • 5,520
  • 2
  • 33
  • 30
3

You can use the coffeescript or= operation

s = ''    
s or= null
Duane Fields
  • 1,331
  • 12
  • 20
1

If you need to check that the content is a string, not null and not an array, use a simple typeof comparison:

 if typeof email isnt "string"
Kevin Lamping
  • 2,292
  • 16
  • 20
1

Here is a jsfiddle demonstrating a very easy way of doing this.

Basicly you simply do this is javascript:

var email="oranste";
var password="i";

if(!(email && password)){
    alert("One or both not set");        
}
else{
    alert("Both set");   
}

In coffescript:

email = "oranste"
password = "i"
unless email and password
  alert "One or both not set"
else
  alert "Both set"

Hope this helps someone :)

Automatico
  • 12,420
  • 9
  • 82
  • 110
1

I think the question mark is the easiest way to call a function on a thing if the thing exists.

for example

car = {
  tires: 4,
  color: 'blue' 
}

you want to get the color, but only if the car exists...

coffeescript:

 car?.color

translates to javascript:

if (car != null) {
  car.color;
}

it is called the existential operator http://coffeescript.org/documentation/docs/grammar.html#section-63

Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
1

I'm pretty sure @thejh 's answer was good enough to check empty string BUT, I think we frequently need to check that 'Does it exist?' and then we need to check 'Is it empty? include string, array and object'

This is the shorten way for CoffeeScript to do this.

tmp? and !!tmp and !!Object.keys(tmp).length

If we keep this question order, that would be checked by this order 1. does it exist? 2. not empty string? 3. not empty object?

so there wasn't any problems for all variable even in the case of not existed.

kakadais
  • 441
  • 1
  • 4
  • 17
0

Based on this answer about checking if a variable has a truthy value or not , you just need one line:

result = !email or !password

& you can try it for yourself on this online Coffeescript console

Community
  • 1
  • 1
AbdelHady
  • 9,334
  • 8
  • 56
  • 83
0

Instead of the accepted answer passwordNotEmpty = !!password you can use

passwordNotEmpty = if password then true else false

It gives the same result (the difference only in syntax).

In the first column is a value, in the second is the result of if value:

0 - false
5 - true
'string' - true
'' - false
[1, 2, 3] - true
[] - true
true - true
false - false
null - false
undefined - false
Evmorov
  • 1,134
  • 22
  • 28