Comparing version 0.9.1 to 0.9.2
0.9.2 / 2012-08-10 | ||
================== | ||
* added $(...).map(fn) | ||
* manipulation: refactor `makeCheerioArray` | ||
* make .removeClass() remove *all* occurrences (#64) | ||
0.9.1 / 2012-08-03 | ||
@@ -3,0 +10,0 @@ ================== |
@@ -73,44 +73,27 @@ var _ = require('underscore'), | ||
var split = function(className) { | ||
return className.trim().split(/\s+/); | ||
}; | ||
// Handle if value is a function | ||
if (_.isFunction(value)) { | ||
this.each(function(j) { | ||
var $this = $(this), | ||
className = $this.attr('class') || ''; | ||
$this.removeClass(value.call(this, j, className)); | ||
return this.each(function(idx) { | ||
$this.removeClass(value.call(this, idx, $(this).attr('class') || '')); | ||
}); | ||
} | ||
// If value isnt undefined and also not a string | ||
if (value !== undefined && !_.isString(value)) return this; | ||
var classNames = (value || '').split(rspace), | ||
numClasses = classNames.length, | ||
className, | ||
$elem, | ||
ret; | ||
for (var i = 0, iLen = this.length; i < iLen; i++) { | ||
$elem = $(this[i]); | ||
className = this[i].attribs['class']; | ||
if (!$.isTag(this[i]) || !className) continue; | ||
else if (!value) { | ||
this[i].attribs['class'] = ''; | ||
continue; | ||
return this.each(function() { | ||
if ($.isTag(this)) { | ||
// If `value` is "" or undefined, set the class attribute to "". | ||
// Otherwise, split the class name into an array of class names, | ||
// discard occurrences of `value`, and join the remaining class | ||
// names to produce the updated attribute value. | ||
this.attribs['class'] = !value ? '' : _.reject( | ||
split(this.attribs['class']), | ||
function(name) { return _.contains(split(value), name); } | ||
).join(' '); | ||
} | ||
// Separate out the classes | ||
ret = (' ' + className + ' ').replace(rclass, ' '); | ||
for (var j = 0; j < numClasses; j++) { | ||
className = classNames[j]; | ||
ret = ret.replace(' ' + className + ' ', ' '); | ||
} | ||
this[i].attribs['class'] = ret.trim(); | ||
} | ||
return this; | ||
}); | ||
}; | ||
module.exports = $.fn.extend(exports); |
@@ -18,17 +18,5 @@ var _ = require('underscore'), | ||
var makeCheerioArray = function(elems) { | ||
var dom = [], | ||
len = elems.length, | ||
elem; | ||
for (var i = 0; i < len; i++) { | ||
elem = elems[i]; | ||
// If a cheerio object | ||
if (elem.cheerio) { | ||
dom = dom.concat(elem.toArray()); | ||
} else { | ||
dom = dom.concat($.parse.eval(elem)); | ||
} | ||
} | ||
return dom; | ||
return _.reduce(elems, function(dom, elem) { | ||
return dom.concat(elem.cheerio ? elem.toArray() : $.parse.eval(elem)); | ||
}, []); | ||
}; | ||
@@ -35,0 +23,0 @@ |
@@ -75,2 +75,9 @@ var _ = require('underscore'), | ||
var map = exports.map = function(fn, args) { | ||
var self = this; | ||
return self.toArray().map(function(el, i) { | ||
return fn.call(self[i], i, el); | ||
}); | ||
}; | ||
var first = exports.first = function() { | ||
@@ -77,0 +84,0 @@ return this[0] ? $(this[0]) : $(); |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["htmlparser", "jquery", "selector", "scraper"], | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
@@ -219,2 +219,11 @@ # cheerio [![Build Status](https://secure.travis-ci.org/MatthewMueller/cheerio.png?branch=master)](http://travis-ci.org/MatthewMueller/cheerio) | ||
#### .map( function(index, element) ) | ||
Iterates over a cheerio object, executing a function for each selected element. Map will return an `array` of return values from each of the functions it iterated over. The function is fired in the context of the DOM element, so `this` refers to the current element, which is equivalent to the function parameter `element`. | ||
$('li').map(function(i, el) { | ||
// this === el | ||
return $(this).attr('class'); | ||
}).join(', '); | ||
=> apple, orange, pear | ||
#### .first() | ||
@@ -221,0 +230,0 @@ Will select the first element of a cheerio object |
@@ -178,2 +178,7 @@ var expect = require('expect.js'); | ||
it('(class) : should remove all occurrences of a class name', function() { | ||
var $div = $('<div class="x x y x z"></div>'); | ||
expect($div.removeClass('x').hasClass('x')).to.be(false); | ||
}); | ||
it('(fn) : should remove classes returned from the function'); | ||
@@ -180,0 +185,0 @@ |
@@ -5,3 +5,2 @@ var expect = require('expect.js'), | ||
describe('$(...)', function() { | ||
@@ -99,2 +98,16 @@ | ||
describe('.map', function() { | ||
it('(fn) : should return an array of mapped items', function() { | ||
var classes = $('li', fruits).map(function(i, el) { | ||
expect(this).to.be(el); | ||
expect(el.name).to.be('li'); | ||
expect(i).to.be.a('number'); | ||
return $(this).attr('class'); | ||
}).join(', '); | ||
expect(classes).to.be('apple, orange, pear'); | ||
}); | ||
}); | ||
describe('.first', function() { | ||
@@ -101,0 +114,0 @@ |
92142
513