diff --git a/modules/isIterable.js b/modules/isIterable.js new file mode 100644 index 000000000..b229eecf7 --- /dev/null +++ b/modules/isIterable.js @@ -0,0 +1,7 @@ +function isIterable(obj) { + // checks for null and undefined + if (obj == null) { + return false; + } + return typeof obj[Symbol.iterator] === 'function'; + } \ No newline at end of file diff --git a/modules/iteratorToArray.js b/modules/iteratorToArray.js new file mode 100644 index 000000000..5daababcd --- /dev/null +++ b/modules/iteratorToArray.js @@ -0,0 +1,9 @@ +export default function iteratorToArray(iterator) { + var data, + result = []; + + while (!(data = iterator.next()).done) { + result.push(data.value); + } + return result; +} \ No newline at end of file diff --git a/modules/toArray.js b/modules/toArray.js index 00730e61e..28e88b14a 100644 --- a/modules/toArray.js +++ b/modules/toArray.js @@ -5,7 +5,8 @@ import isArrayLike from './_isArrayLike.js'; import map from './map.js'; import identity from './identity.js'; import values from './values.js'; - +import isIterable from './isIterable.js' +import iteratorToArray from './iteratorToArray' // Safely create a real, live array from anything iterable. var reStrSymbol = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g; export default function toArray(obj) { @@ -16,5 +17,6 @@ export default function toArray(obj) { return obj.match(reStrSymbol); } if (isArrayLike(obj)) return map(obj, identity); + else if (isIterable(obj)) return iteratorToArray(obj[Symbol.iterator]) return values(obj); }