Skip to content

Commit

Permalink
change $.fn.map() to return a Zepto collection
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Oct 3, 2012
1 parent 17f69f4 commit 2f723fa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/zepto.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ var Zepto = (function() {
// `map` and `slice` in the jQuery API work differently
// from their array counterparts
map: function(fn){
return $.map(this, function(el, i){ return fn.call(el, i, el) })
return $($.map(this, function(el, i){ return fn.call(el, i, el) }))
},
slice: function(){
return $(slice.apply(this, arguments))
Expand Down Expand Up @@ -357,9 +357,9 @@ var Zepto = (function() {
},
find: function(selector){
var result
if (this.length == 1) result = zepto.qsa(this[0], selector)
if (this.length == 1) result = $(zepto.qsa(this[0], selector))
else result = this.map(function(){ return zepto.qsa(this, selector) })
return $(result)
return result
},
closest: function(selector, context){
var node = this[0]
Expand All @@ -385,7 +385,7 @@ var Zepto = (function() {
return filtered(this.map(function(){ return children(this) }), selector)
},
contents: function() {
return $(this.map(function() { return slice.call(this.childNodes) }))
return this.map(function() { return slice.call(this.childNodes) })
},
siblings: function(selector){
return filtered(this.map(function(i, el){
Expand All @@ -397,7 +397,7 @@ var Zepto = (function() {
},
// `pluck` is borrowed from Prototype.js
pluck: function(property){
return this.map(function(){ return this[property] })
return $.map(this, function(el){ return el[property] })
},
show: function(){
return this.each(function(){
Expand Down Expand Up @@ -447,7 +447,7 @@ var Zepto = (function() {
return this
},
clone: function(){
return $(this.map(function(){ return this.cloneNode(true) }))
return this.map(function(){ return this.cloneNode(true) })
},
hide: function(){
return this.css("display", "none")
Expand Down
6 changes: 3 additions & 3 deletions test/data.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ <h1>Zepto data() tests</h1>

items.data('each', 'mark')

var values = items.map(function(){ return $(this).data('each') })
var values = items.map(function(){ return $(this).data('each') }).get()
t.assertEqual('mark, mark', values.join(', '))
},

Expand Down Expand Up @@ -200,10 +200,10 @@ <h1>Zepto data() tests</h1>
'color': 'purple'
})

var values = items.map(function(){ return $(this).data('foo') })
var values = items.map(function(){ return $(this).data('foo') }).get()
t.assertEqual('bar, bar', values.join(', '))

var values2 = items.map(function(){ return $(this).data('answer') })
var values2 = items.map(function(){ return $(this).data('answer') }).get()
t.assertEqual('42, 42', values2.join(', '))
}

Expand Down
9 changes: 5 additions & 4 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,8 @@ <h1>Zepto DOM unit tests</h1>
t.assertIdentical(el, this)
return idx + ':' + this.nodeName.toUpperCase()
})
t.assertEqual('0:SPAN, 1:B, 2:BR', results.join(', '))
t.assertEqual(3, results.size())
t.assertEqual('0:SPAN, 1:B, 2:BR', results.get().join(', '))
},

testDollarMap: function(t){
Expand Down Expand Up @@ -1454,12 +1455,12 @@ <h1>Zepto DOM unit tests</h1>
testAttrOnTextInputField: function(t) {
var inputs = $('#attr_with_text_input input'), values

values = inputs.map(function(){ return $(this).attr('value') })
values = $.map(inputs, function(i){ return $(i).attr('value') })
t.assertEqual('Default input, Text input, Email input, Search input', values.join(', '))

inputs.val(function(i, value){ return value.replace('input', 'changed') })

values = inputs.map(function(){ return $(this).attr('value') })
values = $.map(inputs, function(i){ return $(i).attr('value') })
t.assertEqual('Default changed, Text changed, Email changed, Search changed', values.join(', '))
},

Expand Down Expand Up @@ -1677,7 +1678,7 @@ <h1>Zepto DOM unit tests</h1>
'<b>six</b>'
)
t.assertEqual('original one two three four five six',
el.children().map(function(){ return $(this).text() }).join(' '))
$.map(el.children(), function(c){ return $(c).text() }).join(' '))
},

testAppendToPrependTo: function(t){
Expand Down

0 comments on commit 2f723fa

Please sign in to comment.