检查 string 是空还是空的最简单方法

我得到了这个代码,它检查空字符串或空字符串。它在测试中工作。

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}"

我想知道是否有比 not email? or email is ''更好的方法。我是否可以在 CoffeeScript 中使用一个调用来实现类似于 C # string.IsNullOrEmpty(arg)的功能?我总是可以为它定义一个函数(就像我做的那样) ,但是我想知道在这门语言中是否有我缺少的东西。

63777 次浏览

Yup:

passwordNotEmpty = not not password

or shorter:

passwordNotEmpty = !!password

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)

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

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"

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 :)

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

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.

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

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

You can use the coffeescript or= operation

s = ''
s or= null

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.