Skip to content
Browse files

stop iteration in fn.each() when the callback returns false

This makes it compatible with jQuery and current Zepto documentation.
  • Loading branch information...
1 parent f5ef323 commit 4c67d178b5349636282bb29d0ad42c89f038940f @mitio mitio committed with mislav
Showing with 15 additions and 1 deletion.
  1. +3 −1 src/zepto.js
  2. +12 −0 test/zepto.html
View
4 src/zepto.js
@@ -350,7 +350,9 @@ 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
12 test/zepto.html
@@ -705,6 +705,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)

0 comments on commit 4c67d17

Please sign in to comment.
Something went wrong with that request. Please try again.