Skip to content

Commit

Permalink
Merge pull request #121 from davidchambers/iterators
Browse files Browse the repository at this point in the history
use `_.each` and `_.map` to simplify cheerio namesakes
  • Loading branch information
matthewmueller committed Nov 17, 2012
2 parents 8b3ac38 + 0b70293 commit 1715a34
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,16 @@ var children = exports.children = function(selector) {
};

var each = exports.each = function(fn) {
var len = this.length,
el,
$;

for(var i = 0; i < len; i++) {
el = this[i];
$ = this.make(el);
fn.call($, i, el);
}

_.each(this, function(el, i) {
fn.call(this.make(el), i, el);
}, this);
return this;
};

var map = exports.map = function(fn) {
var len = this.length,
ret = [],
el,
$;

for(var i = 0; i < len; i++) {
el = this[i];
$ = this.make(el);
ret[ret.length] = fn.call($, i, el);
}

return ret;
return _.map(this, function(el, i) {
return fn.call(this.make(el), i, el);
}, this);
};

var filter = exports.filter = function(match) {
Expand All @@ -107,11 +91,11 @@ var filter = exports.filter = function(match) {
};

var first = exports.first = function() {
return this[0] ? this.make(this[0]) : this;
return this[0] ? this.make(this[0]) : this;
};

var last = exports.last = function() {
return this[0] ? this.make(this[this.length - 1]) : this;
return this[0] ? this.make(this[this.length - 1]) : this;
};

// Reduce the set of matched elements to the one at the specified index.
Expand Down

0 comments on commit 1715a34

Please sign in to comment.