CoffeeScript Blows. Minds.

Was looking for a simple way to write an alternative of PHP’s in_array() function in JavaScript, and since I’m using actually writing CoffeeScript, decided to look there as well. And sure enough. Mind Blown.

 

        for val,key in values
            if val in selected_values
                return "MIND BLOWN."

Turns into

var key, val, _i, _len,
  __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

for (key = _i = 0, _len = values.length; _i < _len; key = ++_i) {
  val = values[key];
  if (__indexOf.call(selected_values, val) >= 0) {
    return "MIND BLOWN.";
  }
}

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *