From 9e86efadbcc2a0720282bce10924e6d9ea155077 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 30 Aug 2015 23:40:34 -0700 Subject: [PATCH] Make the chain wrapper iterable. --- lodash.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index 81407ebdd..db3d289d5 100644 --- a/lodash.js +++ b/lodash.js @@ -5870,14 +5870,13 @@ }); /** - * Gets the next value on a wrapped object in ES2015 iterator - * format (). Useful for `for..of` loops and using with - * some ES2015 features. + * Gets the next value on a wrapped object following the + * [iterator protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterator). * * @name next * @memberOf _ * @category Chain - * @returns {*} Returns the next value. + * @returns {Object} Returns the next iterator value. * @example * * var wrapped = _([1, 2]); @@ -5901,6 +5900,27 @@ return { 'done': done, 'value': value }; } + /** + * Enables the wrapper to be iterable. + * + * @name next + * @memberOf _ + * @category Chain + * @returns {Object} Returns the wrapper object. + * @example + * + * var wrapped = _([1, 2]); + * + * wrapped[Symbol.iterator]() === wrapped; + * // => true + * + * Array.from(wrapped); + * // => [1, 2] + */ + function wrapperToIterator() { + return this; + } + /** * Creates a clone of the chained sequence planting `value` as the wrapped value. * @@ -11998,6 +12018,9 @@ lodash.prototype.toString = wrapperToString; lodash.prototype.run = lodash.prototype.toJSON = lodash.prototype.valueOf = lodash.prototype.value = wrapperValue; + if (iteratorSymbol) { + lodash.prototype[iteratorSymbol] = wrapperToIterator; + } return lodash; }