Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement for...of iterator via Symbol.iterator #1197

Merged
merged 3 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/cheerio.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*global Symbol*/
/*
Module dependencies
*/
Expand Down Expand Up @@ -138,6 +139,11 @@ Cheerio.prototype.toArray = function() {
return this.get();
};

// Support for (const element of $(...)) iteration:
if (typeof Symbol !== 'undefined') {
Cheerio.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fit better in api/traversing.js

Copy link
Contributor Author

@papandreou papandreou May 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was what I tried first (so it could live near each), but it didn't work because the export of api/traversing.js is being mixed into Cheerio.prototype using _.extend, which does not include Symbol keys:

const mySymbol = Symbol('foo');
const a = { [mySymbol]: 123 };

console.log(a[mySymbol]); // 123
const b = require('lodash').extend({}, a);
console.log(b[mySymbol]); // undefined

Fixing that would involve iterating over Object.getOwnPropertySymbols(mod) as well, and that seemed way too invasive.

}

// Plug in the API
api.forEach(function(mod) {
_.extend(Cheerio.prototype, mod);
Expand Down
16 changes: 16 additions & 0 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*global Symbol*/
var expect = require('expect.js'),
cheerio = require('../..'),
food = require('../fixtures').food,
Expand Down Expand Up @@ -680,6 +681,21 @@ describe('$(...)', function() {
});
});

if (typeof Symbol !== 'undefined') {
describe('[Symbol.iterator]', function() {

it('should yield each element', function() {
// The equivalent of: for (const element of $('li')) ...
var $li = $('li'),
iterator = $li[Symbol.iterator]();
expect(iterator.next().value.attribs['class']).to.equal('apple');
expect(iterator.next().value.attribs['class']).to.equal('orange');
expect(iterator.next().value.attribs['class']).to.equal('pear');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also assert that iteration terminates as expected?

Copy link
Contributor Author

@papandreou papandreou May 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, fixed in c6bdc79

expect(iterator.next().done).to.equal(true);
});
});
}

describe('.map', function() {
it('(fn) : should be invoked with the correct arguments and context', function() {
var $fruits = $('li');
Expand Down