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

Already on GitHub? Sign in to your account

Stop iteration in $.fn.each() when the callback returns false #632

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+13 −1
Split
View
@@ -350,7 +350,7 @@ var Zepto = (function() {
})
},
each: function(callback){
- this.forEach(function(el, idx){ callback.call(el, idx, el) })
+ emptyArray.every.call(this, function(el, idx){ return callback.call(el, idx, el) !== false })
return this
},
filter: function(selector){
View
@@ -698,6 +698,18 @@
t.assertEqual(2, index)
},
+ testEachBreak: function(t){
+ var index, tagnames = []
+ $('#eachtest > *').each(function(idx, el){
+ index = idx
+ t.assertIdentical(el, this)
+ tagnames.push(el.tagName.toUpperCase())
+ if (idx == 1) return false
+ })
+ t.assertEqual('SPAN, B', tagnames.join(', '))
+ t.assertEqual(1, index)
+ },
+
testMap: function(t){
var results = $('#eachtest > *').map(function(idx, el) {
t.assertIdentical(el, this)