Skip to content

Commit

Permalink
fix: Add length instance property to all Cheerio instances (#1681)
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv authored Jan 26, 2021
1 parent 3cfd691 commit b3010d7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var Cheerio = (module.exports = function (selector, context, root, options) {
return new Cheerio(selector, context, root, options);
}

this.length = 0;
this.options = Object.assign(
{},
defaultOptions,
Expand Down Expand Up @@ -106,7 +107,6 @@ Cheerio.prototype.cheerio = '[cheerio object]';
/*
* Make cheerio an array-like object
*/
Cheerio.prototype.length = 0;
Cheerio.prototype.splice = Array.prototype.splice;

/**
Expand Down
8 changes: 5 additions & 3 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ exports.merge = function (arr1, arr2) {
if (!isArrayLike(arr1) || !isArrayLike(arr2)) {
return;
}
var newLength = arr1.length + arr2.length;
for (var i = 0; i < arr2.length; i++) {
arr1[i + arr1.length] = arr2[i];
var newLength = arr1.length;
var len = +arr2.length;

for (var i = 0; i < len; i++) {
arr1[newLength++] = arr2[i];
}
arr1.length = newLength;
return arr1;
Expand Down
7 changes: 7 additions & 0 deletions test/api/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ describe('deprecated APIs', function () {
expect(typeof cheerio.merge).toBe('function');
});

// #1674 - merge, wont accept Cheerio object
it('should be a able merge array and cheerio object', function () {
var ret = cheerio.merge(new cheerio(), ['elem1', 'elem2']);
expect(typeof ret).toBe('object');
expect(ret).toHaveLength(2);
});

it('(arraylike, arraylike) : should return an array', function () {
var ret = cheerio.merge(arr1, arr2);
expect(typeof ret).toBe('object');
Expand Down

0 comments on commit b3010d7

Please sign in to comment.