Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if an array has some items in coffeescript

Is there some method in coffeescript that returns true when an array has some items in it? Like method in ruby present?:

[].present? false
[1].present? true

According to http://arcturo.github.com/library/coffeescript/07_the_bad_parts.html an array emptiness in coffeescript is determined by its length

alert("Empty Array")  unless [].length

that seems so lame to me.

like image 267
mirelon Avatar asked Jan 11 '13 13:01

mirelon


2 Answers

I don't think there is but can be:

Array::present = ->
  @.length > 0

if [42].present()
  # why yes of course
else
  # oh noes

A very simple and incomplete implementation but it should give you some ideas. And for the record, there's no present? method in Ruby, the method is added by the active_support gem.

like image 61
Jiří Pospíšil Avatar answered Sep 20 '22 13:09

Jiří Pospíšil


Unfortunately, there isn't. The best way to do it is by comparing its length.

like image 35
Michal Miškerník Avatar answered Sep 19 '22 13:09

Michal Miškerník