Skip to content

Commit

Permalink
Deprecate $.fn.toArray
Browse files Browse the repository at this point in the history
Because `$.fn.get` is more powerful than `toArray` and because `toArray`
is not implemented by jQuery, the `toArray` method is no longer
necessary. Deprecate it so it may be more safely removed in some future
version of this library.
  • Loading branch information
jugglinmike committed Mar 13, 2014
1 parent 0ff539e commit f0dc4d2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 23 deletions.
8 changes: 0 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,14 +624,6 @@ $.xml()
### Miscellaneous
DOM element methods that don't fit anywhere else

#### .toArray()
Retrieve all the DOM elements contained in the jQuery set, as an array.

```js
$('li').toArray()
//=> [ {...}, {...}, {...} ]
```

#### .clone() ####
Clone the cheerio object.

Expand Down
4 changes: 2 additions & 2 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var makeDomArray = function(elem) {
if (elem == null) {
return [];
} else if (elem.cheerio) {
return elem.toArray();
return elem.get();
} else if (_.isArray(elem)) {
return _.flatten(elem.map(makeDomArray));
} else if (_.isString(elem)) {
Expand Down Expand Up @@ -195,7 +195,7 @@ var html = exports.html = function(str) {
return $.html(this[0].children);
}

str = str.cheerio ? str.toArray() : evaluate(str);
str = str.cheerio ? str.get() : evaluate(str);

domEach(this, function(i, el) {
el.children = str;
Expand Down
10 changes: 5 additions & 5 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var parents = exports.parents = function(selector) {
// When multiple DOM elements are in the original set, the resulting set will
// be in *reverse* order of the original elements as well, with duplicates
// removed.
this.toArray().reverse().forEach(function(elem) {
this.get().reverse().forEach(function(elem) {
traverseParents(this, elem.parent, selector, Infinity)
.forEach(function(node) {
if (parentNodes.indexOf(node) === -1) {
Expand Down Expand Up @@ -109,9 +109,9 @@ var nextUntil = exports.nextUntil = function(selector, filterSelector) {
var elems = [], untilNode, untilNodes;

if (typeof selector === 'string') {
untilNode = select(selector, this.nextAll().toArray())[0];
untilNode = select(selector, this.nextAll().get())[0];
} else if (selector && selector.cheerio) {
untilNodes = selector.toArray();
untilNodes = selector.get();
} else if (selector) {
untilNode = selector;
}
Expand Down Expand Up @@ -171,9 +171,9 @@ var prevUntil = exports.prevUntil = function(selector, filterSelector) {
var elems = [], untilNode, untilNodes;

if (typeof selector === 'string') {
untilNode = select(selector, this.prevAll().toArray())[0];
untilNode = select(selector, this.prevAll().get())[0];
} else if (selector && selector.cheerio) {
untilNodes = selector.toArray();
untilNodes = selector.get();
} else if (selector) {
untilNode = selector;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ Cheerio.prototype._make = function(dom) {

/**
* Turn a cheerio object into an array
*
* @deprecated
*/

Cheerio.prototype.toArray = function() {
Expand Down
12 changes: 6 additions & 6 deletions test/api.manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('$(...)', function() {
it('(Array) : should append all elements in the array', function() {
var $fruits = $(fruits);
var more = $('<li class="plum">Plum</li><li class="grape">Grape</li>')
.toArray();
.get();
$fruits.append(more);
expect($fruits.children(3).hasClass('plum')).to.be.ok();
expect($fruits.children(4).hasClass('grape')).to.be.ok();
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('$(...)', function() {
it('(Array) : should add all elements in the array as inital children', function() {
var $fruits = $(fruits);
var more = $('<li class="plum">Plum</li><li class="grape">Grape</li>')
.toArray();
.get();
$fruits.prepend(more);
expect($fruits.children(0).hasClass('plum')).to.be.ok();
expect($fruits.children(1).hasClass('grape')).to.be.ok();
Expand Down Expand Up @@ -296,7 +296,7 @@ describe('$(...)', function() {
it('(Array) : should add all elements in the array as next sibling', function() {
var $fruits = $(fruits);
var more = $('<li class="plum">Plum</li><li class="grape">Grape</li>')
.toArray();
.get();
$('.apple', $fruits).after(more);
expect($fruits.children(1).hasClass('plum')).to.be.ok();
expect($fruits.children(2).hasClass('grape')).to.be.ok();
Expand Down Expand Up @@ -438,7 +438,7 @@ describe('$(...)', function() {
it('(Array) : should add all elements in the array as previous sibling', function() {
var $fruits = $(fruits);
var more = $('<li class="plum">Plum</li><li class="grape">Grape</li>')
.toArray();
.get();
$('.apple', $fruits).before(more);
expect($fruits.children(0).hasClass('plum')).to.be.ok();
expect($fruits.children(1).hasClass('grape')).to.be.ok();
Expand Down Expand Up @@ -541,7 +541,7 @@ describe('$(...)', function() {
it('(Array) : should replace one <li> tag with the elements in the array', function() {
var $fruits = $(fruits);
var more = $('<li class="plum">Plum</li><li class="grape">Grape</li>')
.toArray();
.get();
$('.pear', $fruits).replaceWith(more);

expect($fruits.children(2).hasClass('plum')).to.be.ok();
Expand Down Expand Up @@ -592,7 +592,7 @@ describe('$(...)', function() {

it('(fn) : should invoke the callback with the correct argument and context', function() {
var $fruits = $(fruits);
var origChildren = $fruits.children().toArray();
var origChildren = $fruits.children().get();
var args = [];
var thisValues = [];

Expand Down
4 changes: 2 additions & 2 deletions test/api.traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ describe('$(...)', function() {
return [1, [3, 4]];
});

expect($mapped.toArray()).to.eql([
expect($mapped.get()).to.eql([
1, [3, 4],
1, [3, 4],
1, [3, 4]
Expand All @@ -593,7 +593,7 @@ describe('$(...)', function() {
return [null, undefined];
});

expect($mapped.toArray()).to.eql([
expect($mapped.get()).to.eql([
null, undefined,
null, undefined,
null, undefined,
Expand Down

0 comments on commit f0dc4d2

Please sign in to comment.