Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / exotic   js

Repository URL to install this package:

Version: 2.0.8 

/ src / types / array / cast / fromIteratorToArray.ts

import { preAllocate } from '../../../deps'

/**
 * @desc convert an iterator into an array using
 * @since 5.0.0-beta.6
 * @memberOf cast
 *
 * @name iteratorToArray
 * @alias arrayFromIterator
 *
 * @param ter iterator
 * @return iterator values
 *
 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators mozilla-iterators-and-generators}
 * @see {@link mozilla-iterators-and-generators}
 * @see symbols/iterator
 * @see array/preAllocate
 *
 * @example
 *
 *    const map = new Map(Object.entries({eh: true}))
 *
 *    iteratorToArray(map.keys())
 *    //=> ['eh']
 *
 *    iteratorToArray(new Set([0, 1]).keys())
 *    //=> [0, 1]
 *
 */
function iteratorToArray<Values>(iter: Iterator<Values>): Values[] {
  const list = preAllocate(iter)
  let next
  let index = 0

  while (!(next = iter.next()).done && index++) {
    list[index] = next.value
  }

  return list
}

export { iteratorToArray }
export default iteratorToArray