Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Add CollectionUtils.some() to allow breaking out of iteration #3117

Merged
merged 6 commits into from
Apr 18, 2013
Merged

Add CollectionUtils.some() to allow breaking out of iteration #3117

merged 6 commits into from
Apr 18, 2013

Conversation

TomMalbran
Copy link
Contributor

Since we will be using this function a lot more, this change would make it really useful to do a search inside an object and stop when finding the result by making the callback return a value that will be false once the search is done. This is the same way that jQuery uses to break the .each loops.

@ghost ghost assigned peterflynn Mar 13, 2013
@njx
Copy link
Contributor

njx commented Mar 13, 2013

To @peterflynn

@TomMalbran
Copy link
Contributor Author

An alternative to this could be a new CollectionUtils.some function that could call Object.keys().some() or use a for loop and break it if the callback return true, to make a function to loop over Objects similar to Array.some. Reference: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/some

@TomMalbran
Copy link
Contributor Author

I've been using Array.some a lot more lately and it seems useful to have a return value, so I removed the break in forEach and added a new some function, which also makes it more consistence with the Array functions.

@@ -62,7 +62,29 @@ define(function (require, exports, module) {
}
}

/**
* Iterates over all the properties in an object or elements in an array. If a callback returns a
* truthly value then it will inmediatelly return true, if not, it will return false. Differs from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "inmediatelly" -> "immediately"

@peterflynn
Copy link
Member

@TomMalbran done reviewing. Two comment nits plus this bigger question about whether we want to rename the API. We should probably wait a day or so to see if others chime in on that.

@TomMalbran
Copy link
Contributor Author

It seems like I missed several cases when switching $.each with forEach where the loop broke. This fixes one of those cases, since it needs CollectionUtils.some to work.

@TomMalbran
Copy link
Contributor Author

@peterflynn It's been a few days, and nothing happened, should we just leave like it is. The names make sense since the functions work for any collection, and JavaScript already implements different APIs with the same names.

peterflynn added a commit that referenced this pull request Apr 18, 2013
Add CollectionUtils.some() to allow breaking out of iteration
@peterflynn peterflynn merged commit 0efe674 into adobe:master Apr 18, 2013
@peterflynn
Copy link
Member

Clarifying title (was: "Break the CollectionUtils.forEach loop if the callback returns false")

@TomMalbran TomMalbran deleted the tom/break-forEach branch April 18, 2013 03:47
@TomMalbran
Copy link
Contributor Author

@peterflynn Great, I am glad that this is finally merged :) I know it shouldn't be used for arrays, but it is possible. It would work great when the object could be either an array or an object. Thanks for changing the name, this pull went from breaking the loop to creating a new method.

@peterflynn
Copy link
Member

Yeah -- thanks for generalizing it that way btw. We will definitely have more places to use this :-)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants