Skip to content
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

Fix Collection.closest #83

Closed
wants to merge 5 commits into from
Closed

Conversation

juliankrispel
Copy link

At the moment Collection.closest doesn't actually work with filters. If type.check(parent.value) in the while loop on line 80 returns true it will stop the while loop, skipping the last expression, which tests if the filter condition applies.

This pr fixes this and refactors the while loop into a recursive function which is imo more readable.

I also added some specs to prove that closest is working correctly.

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@fkling
Copy link
Contributor

fkling commented Jan 4, 2016

Thanks for noticing this! However, I'm not in favor of a recursive solution. It's unlikely that someone will run in to call stack issues, but still. I'm more comfortable doing this when we have TCO :)

It seems this could be easily fixed by changing the while condition to

while (
  parent &&
  !(
    type.check(parent.value) &&
    (!filter || matchNode(parent.value, filter))
  )
) {

(or something equivalent)

@juliankrispel
Copy link
Author

@fkling my pleasure, thanks for this awesome project! I'll change it to your liking, it's your project after all, all I want to do is contribute :)

Just as a side note: I'm not strongly opinionated about this, but it certainly helped to debug the thing, also three if conditions although more verbose seemed easier to reason about than one big condition, hence I thought it was reasonable.

@juliankrispel
Copy link
Author

@fkling changed it back to a while loop, the added tests cover the issue I noticed so it should be good to go :)

@fkling
Copy link
Contributor

fkling commented Jan 7, 2016

I squashed all the commits. This is now merged with faba497 and release as v0.3.12. Thank you again! :)

@fkling fkling closed this Jan 7, 2016
@juliankrispel
Copy link
Author

@fkling thanks :), here's a kitten

image

@juliankrispel juliankrispel deleted the closest branch January 8, 2016 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants