Skip to content

Commit

Permalink
Support Symbol.iterator only if available in the runtime environment.
Browse files Browse the repository at this point in the history
Closes #124
  • Loading branch information
kofrasa committed Mar 8, 2020
1 parent a2853e8 commit 166bd95
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.5.0 / 2020-03-08

- Optionally add `Symbol.iterator` support if available in runtime environment. Closes [#124](https://github.com/kofrasa/mingo/issues/124)
- Allow matching with nested fields in $lookup. Fixes [#123](https://github.com/kofrasa/mingo/issues/123)
- Add $round operator and support 'place' argument for `$trunc`. Fixes [#121](https://github.com/kofrasa/mingo/issues/121)
- Support new array fields. Fixes [#119](https://github.com/kofrasa/mingo/issues/119)
Expand Down
34 changes: 17 additions & 17 deletions dist/mingo.js
Original file line number Diff line number Diff line change
Expand Up @@ -1536,11 +1536,6 @@ var Iterator = function () {
}

createClass(Iterator, [{
key: Symbol.iterator,
value: function value() {
return this;
}
}, {
key: '_validate',
value: function _validate() {
if (this.__first) throw new Error("Cannot add iteratee/transform after `first()`");
Expand Down Expand Up @@ -1718,6 +1713,12 @@ var Iterator = function () {
return Iterator;
}();

if (typeof Symbol === 'function') {
Iterator.prototype[Symbol.iterator] = function () {
return this;
};
}

/**
* Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries.
* https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/
Expand Down Expand Up @@ -2774,22 +2775,21 @@ var Cursor = function () {
value: function forEach(callback) {
this._fetch().each(callback);
}

/**
* Applies an [ES2015 Iteration protocol][] compatible implementation
* [ES2015 Iteration protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
* @returns {Object}
*/

}, {
key: Symbol.iterator,
value: function value() {
return this._fetch();
}
}]);
return Cursor;
}();

if (typeof Symbol === 'function') {
/**
* Applies an [ES2015 Iteration protocol][] compatible implementation
* [ES2015 Iteration protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
* @returns {Object}
*/
Cursor.prototype[Symbol.iterator] = function () {
return this._fetch();
};
}

/**
* Query object to test collection elements with
* @param criteria the pass criteria for the query
Expand Down
7 changes: 4 additions & 3 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ export class Cursor {
forEach (callback) {
this._fetch().each(callback)
}
}

if (typeof Symbol === 'function') {
/**
* Applies an [ES2015 Iteration protocol][] compatible implementation
* [ES2015 Iteration protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
* @returns {Object}
*/
[Symbol.iterator] () {
Cursor.prototype[Symbol.iterator] = function () {
return this._fetch()
}
}

}
10 changes: 6 additions & 4 deletions lib/lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ class Iterator {
this.next = baseIterator(source, this.__iteratees, this.__buf)
}

[Symbol.iterator] () {
return this
}

_validate () {
if (this.__first) throw new Error("Cannot add iteratee/transform after `first()`")
}
Expand Down Expand Up @@ -284,3 +280,9 @@ class Iterator {
return this.reduce((acc,n) => ++acc, 0)
}
}

if (typeof Symbol === 'function') {
Iterator.prototype[Symbol.iterator] = function() {
return this
}
}
1 change: 0 additions & 1 deletion test/cursor_tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var test = require('tape')
var Backbone = require('backbone')
var mingo = require('../dist/mingo')
var samples = require('./support')
var _ = mingo._internal()
Expand Down

0 comments on commit 166bd95

Please sign in to comment.