coffeescript-cookbook/coffeescript-cookbook.github.io

Unique recipe fails on empty arrays

inanna-malick opened this issue · 2 comments

As defined here: http://coffeescriptcookbook.com/chapters/arrays/removing-duplicate-elements-from-arrays

Array::unique = ->
  output = {}
  output[@[key]] = @[key] for key in [0...@length]
  value for key, value of output

[].unique() evaluates to [null] in the latest version of Chrome.

This can be fixed by using the 'own' keyword to only iterate over output's own properties.

Array::unique = ->
  output = {}
  output[@[key]] = @[key] for key in [0...@length]
  value for own key, value of output

[].unique() evaluates to [] in the latest version of Chrome.

I'm not sure why null is a property of the empty array, but javascript can be weird like that.

you wanna PR this change?

Sorry, I'll do that.

PS: thanks for the great resource