Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using indexOf in CoffeeScript

I'm using the following code in CoffeeScript:

if elem in my_array
  do_something()

Which compiles to this javascript:

if (__indexOf.call(my_array, elem) < 0) {
  my_array.push(elem);
}

I can see it's using the function __indexOf which is defined at the top of the script.

My question is regarding this use case: I want to remove an element from an array, and I want to support IE8. I can do that easily with indexOf and splice in browsers who support indexOf on an array object. However, in IE8 this doesn't work:

if (attr_index = my_array.indexOf(elem)) > -1
  my_array.splice(attr_index, 1)

I tried using the __indexOf function defined by CoffeScript but I get a reserved word error in the compiler.

if (attr_index = __indexOf.call(my_array, elem) > -1
  my_array.splice(attr_index, 1)

So how can I use CoffeScript or is there a more unobtrusive method for calling indexOf? It seems weird to define the same function twice, just because CoffeeScript won't let me use theirs...

like image 579
Cristian Avatar asked Jun 21 '12 14:06

Cristian


2 Answers

No, CoffeeScript precludes you from using its helpers directly, since that would break down the distinction between the language and the implementation. To support IE8, I would add a shim like

Array::indexOf or= (item) ->
  for x, i in this
    return i if x is item
  return -1

or use a library like Underscore.js for array manipulation.

like image 70
Trevor Burnham Avatar answered Sep 29 '22 07:09

Trevor Burnham


CoffeeScript adds the following to the top of file scope:

var __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;
};

If we attempted to utilise this by doing:

indexOf = __indexOf

This would produce a compiler error: RESERVED WORD "__INDEXOF"

The solution is to subvert the compiler using backticks:

indexOf = `__indexOf`

Then use it with

indexOf.call([1,2,3,4], 3) //2

Or we could reduce the duplicate code from @Trevor Burnham's answer:

Array::indexOf or= `__indexOf`

However, you need to be sure CoffeeScript will be adding this definition by making use of the in operator as boolean expression (and with a dynamic length array on the right hand side). At the end of the day, it might just be easier for some just to redefine it :)

like image 23
jpillora Avatar answered Sep 29 '22 05:09

jpillora