Skip to content

Correct unique for empty arrays #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Correct unique for empty arrays #107

wants to merge 1 commit into from

Conversation

inanna-malick
Copy link

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.

An alternative fix would be to just return an empty array if @Length == 0

Only return properties belonging to output object. Fixes bug where [].unique() evaluates to [null]
@inanna-malick
Copy link
Author

This is a bit too hacky. I'm going to resubmit a better fix

@sukima
Copy link
Contributor

sukima commented Apr 23, 2014

I think the return [] if @length == 0 is a good option, Underscore has checks like these.

@sukima sukima mentioned this pull request Apr 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants