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

Make collection iterable using for of loops #1830

Merged
merged 1 commit into from Apr 27, 2018

Conversation

Projects
2 participants
@fl0w
Copy link
Contributor

fl0w commented Apr 27, 2018

Introduction

Make collections iterable using for of loops.

const models = await bookshelf.model('Model').fetchAll()
// previously
for (const model of models.models) {}

// now
for (const model of models) {}

Motivation

It's nice to be able to use default ECMA functionality when they're non-breaking.

Proposed solution

Define CollectionBase.prototype[Symbol.iterator] and yield to this.models (which always defaults to an array)

Current PR Issues

None that I am aware of

Alternatives considered

None that I am aware of

@fl0w fl0w changed the title make collection iterable using for of loops Make collection iterable using for of loops Apr 27, 2018

@fl0w

This comment has been minimized.

Copy link
Contributor

fl0w commented Apr 27, 2018

My god, I completely missed #1817, still think my solution is cleaner. Apologies :)

@ricardograca ricardograca added this to To Do in Version 0.14.0 via automation Apr 27, 2018

@ricardograca
Copy link
Member

ricardograca left a comment

This needs to have accompanying tests to ensure the correct behavior.

@fl0w fl0w force-pushed the fl0w:collection-iterator branch from cf6a56e to 6095874 Apr 27, 2018

@fl0w

This comment has been minimized.

Copy link
Contributor

fl0w commented Apr 27, 2018

Added a simple test, if it's not enough pointers are appreciated and I'll add tests accordantly.

@ricardograca ricardograca merged commit cbf9675 into bookshelf:master Apr 27, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

Version 0.14.0 automation moved this from To Do to Done Apr 27, 2018

@fl0w fl0w deleted the fl0w:collection-iterator branch Apr 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment