split-iterable

Split iterable into multiple chunks

Usage no npm install needed!

<script type="module">
  import splitIterable from 'https://cdn.skypack.dev/split-iterable';
</script>

README

Split Iterable

Split iterable into multiple chunks

Usage

splitIterable

const { splitIterable } = require('split-iterable')

expect(Array.from(
  splitIterable([0, 'x', 1, 2, 'x', 3, 4, 5])
)).toEqual([
  [0],
  [1, 2],
  [3, 4, 5]
])

splitAsyncIterable

const { splitAsyncIterable } = require('split-iterable')

const iterable = {
  async * [Symbol.asyncIterator] () {
    yield * [0, 'x', 1, 2, 'x', 3, 4, 5]
  }
}

async function toArray (iterable) {
  let array = []

  for await (const item of iterable) {
    array.push(item)
  }

  return array
}

expect(await toArray(
  splitAsyncIterable(iterable)
)).toEqual([
  [0],
  [1, 2],
  [3, 4, 5]
])

License

MIT © Hoàng Văn Khải