Skip to content

Conversation

@fl0w
Copy link
Collaborator

@fl0w 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
Copy link
Collaborator Author

fl0w commented Apr 27, 2018

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

Copy link
Member

@ricardograca ricardograca left a comment

Choose a reason for hiding this comment

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

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

@fl0w fl0w force-pushed the collection-iterator branch from cf6a56e to 6095874 Compare April 27, 2018 11:02
@fl0w
Copy link
Collaborator Author

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
@fl0w fl0w deleted the collection-iterator branch April 29, 2018 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants