From 0b7029312116214007cd9aabba779d21a0125095 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Sat, 17 Nov 2012 11:39:19 -0800 Subject: [PATCH] use `_.each` and `_.map` to simplify cheerio namesakes --- lib/api/traversing.js | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/lib/api/traversing.js b/lib/api/traversing.js index 60880a37b9..50c672093d 100644 --- a/lib/api/traversing.js +++ b/lib/api/traversing.js @@ -70,40 +70,24 @@ 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 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.