diff --git a/CHANGELOG.md b/CHANGELOG.md index f601df95d..e9b1dd49b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/dist/mingo.js b/dist/mingo.js index 93fb6f3bf..955476c31 100644 --- a/dist/mingo.js +++ b/dist/mingo.js @@ -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()`"); @@ -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/ @@ -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 diff --git a/lib/cursor.js b/lib/cursor.js index 4ed5d8164..42c27728c 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -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() } -} - +} \ No newline at end of file diff --git a/lib/lazy.js b/lib/lazy.js index 00b758146..df9180ed8 100644 --- a/lib/lazy.js +++ b/lib/lazy.js @@ -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()`") } @@ -284,3 +280,9 @@ class Iterator { return this.reduce((acc,n) => ++acc, 0) } } + +if (typeof Symbol === 'function') { + Iterator.prototype[Symbol.iterator] = function() { + return this + } +} \ No newline at end of file diff --git a/test/cursor_tests.js b/test/cursor_tests.js index ac1aad690..294304a4b 100644 --- a/test/cursor_tests.js +++ b/test/cursor_tests.js @@ -1,5 +1,4 @@ var test = require('tape') -var Backbone = require('backbone') var mingo = require('../dist/mingo') var samples = require('./support') var _ = mingo._internal()