Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

optimize `fn.find()`

  • Loading branch information...
commit 4eca863e0804b6f3d24f84c025469efe78d1b39d 1 parent 116c79b
@mislav mislav authored
Showing with 5 additions and 1 deletion.
  1. +5 −1 src/zepto.js
View
6 src/zepto.js
@@ -7,6 +7,7 @@ var Zepto = (function() {
function classRE(name){ return new RegExp("(^|\\s)" + name + "(\\s|$)") }
function compact(array){ return array.filter(function(item){ return item !== undefined && item !== null }) }
+ function flatten(array){ return array.reduce(function(a,b){ return a.concat(b) }, []) }

If you want a bit more performance, and don't mind cryptic JS, you can define flatten as:

function flatten(array){ return Array.prototype.concat.apply([], array); }

Note: both versions only flatten a 2d array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
function camelize(str){ return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' }) }
fragmentRE = /^\s*<.+>/;
@@ -64,7 +65,10 @@ var Zepto = (function() {
first: function(){ return $(this.get(0)) },
last: function(){ return $(this.get(this.length - 1)) },
find: function(selector){
- return $(this.dom.map(function(el){ return $$(el, selector) }).reduce(function(a,b){ return a.concat(b) }, []));
+ var result;
+ if (this.length == 1) result = $$(this.get(0), selector);
+ else result = flatten(this.dom.map(function(el){ return $$(el, selector) }));
+ return $(result);
},
closest: function(selector, context){
var node = this.dom[0], nodes = $$(context !== undefined ? context : document, selector);
Please sign in to comment.
Something went wrong with that request. Please try again.