检查字符串是否为空或空的最简单方法

发布于 2024-12-15 15:21:31 字数 608 浏览 2 评论 0原文

我有这段代码可以检查空字符串或空字符串。它正在测试中。

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

我想知道是否有比不发送电子邮件更好的方法?或者电子邮件是“”。我可以通过一次调用在 CoffeeScript 中执行与 C# string.IsNullOrEmpty(arg) 相同的操作吗?我总是可以为它定义一个函数(就像我所做的那样),但我想知道语言中是否缺少一些东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

叹沉浮 2024-12-22 15:21:31

是的:

passwordNotEmpty = not not password

或更短:

passwordNotEmpty = !!password

Yup:

passwordNotEmpty = not not password

or shorter:

passwordNotEmpty = !!password
鼻尖触碰 2024-12-22 15:21:31

它并不完全等效,但仅当 email 为非空且 .length 为非零且 .length 时,email?.length 才会为真> 财产。如果您此值,则结果对于字符串和数组都应按照您的预期运行。

如果 emailnull 或没有 .length,则 email?.length 将计算为 < code>null,这是错误的。如果它确实有.length,那么该值将计算其长度,如果它为空,则该值将为 false。

您的函数可以实现为:

eitherStringEmpty = (email, password) ->
  not (email?.length and password?.length)

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)
楠木可依 2024-12-22 15:21:31

在这种情况下,“真实”就派上用场了。您甚至不需要为此定义一个函数:

test1 = not (email and password)

它为什么有效?

'0'       // true
'123abc'  // true
''        // false
null      // false
undefined // false

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
飘过的浮云 2024-12-22 15:21:31
unless email? and email
  console.log 'email is undefined, null or ""'

首先使用存在运算符检查 email 是否未定义且不为 null,然后如果您知道它存在,则仅当电子邮件字符串为空时 and email 部分才会返回 false。

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.

dawn曙光 2024-12-22 15:21:31

您可以使用coffeescript或=操作

s = ''    
s or= null

You can use the coffeescript or= operation

s = ''    
s or= null
岁吢 2024-12-22 15:21:31

如果需要检查内容是否是字符串、不是 null 也不是数组,请使用简单的 typeof 比较:

 if typeof email isnt "string"

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"
耳根太软 2024-12-22 15:21:31

这是 jsfiddle 演示了一种非常简单的方法。

基本上你只需这样做就是 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"

希望这对某人有帮助:)

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

淡写薰衣草的香 2024-12-22 15:21:31

我认为问号是在事物存在的情况下调用该事物的函数的最简单方法。

例如,

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

您想获取颜色,但前提是汽车存在...

CoffeeScript:

 car?.color

翻译为 javascript:

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

它称为存在运算符 http://coffeescript.org/documentation/docs/grammar.html#section-63

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

甜心 2024-12-22 15:21:31

我很确定 @thejh 的答案足以检查空字符串但是,
我认为我们经常需要检查“它是否存在?”然后我们需要检查“它是空的吗?”包括字符串、数组和对象'

这是 CoffeeScript 执行此操作的简化方法。

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

如果我们保留这个问题的顺序,就会按这个顺序检查
1. 存在吗?
2.不是空字符串?
3.不是空对象?

所以即使在不存在的情况下,所有变量也没有任何问题。

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.

〆一缕阳光ご 2024-12-22 15:21:31

基于这个答案关于检查变量是否具有truthy值,你只需要一行:

result = !email or !password

&你可以在这个在线 Coffeescript 控制台上亲自尝试一下

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

为人所爱 2024-12-22 15:21:31

您可以使用代替接受的答案 passwordNotEmpty = !!password

passwordNotEmpty = if password then true else false

它给出相同的结果(仅在语法上有所不同)。

第一列是一个值,第二列是 if value 的结果:

0 - false
5 - true
'string' - true
'' - false
[1, 2, 3] - true
[] - true
true - true
false - false
null - false
undefined - false

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文