extra-generator

npm install --save extra-generator

Usage no npm install needed!

<script type="module">
  import extraGenerator from 'https://cdn.skypack.dev/extra-generator';
</script>

README

extra-generator

Install

npm install --save extra-generator
# or
yarn add extra-generator

API

of

function of<T>(val: T): Iterable<T>
of(1) // [1]

repeat

function repeat<T>(val: T, times: number = Infinity): Iterable<T>
repeat(1) // [1, 1, 1, ...]
repeat(1, 3) // [1, 1, 1]
repeat(1, 0) // []

countdown

function countdown(begin: number, end: number): Iterable<number>
countdown(2, -2) // [2, 1, 0, -1, -2]
countdown(1, 1) // [1]
countdown(0, 1) // []

countup

function countup(begin: number, end: number): Iterable<number>
countup(-2, 2) // [-2, -1, 0, 1, 2]
countup(1, 1) // [1]
countup(1, 0) // []

range

function range(start: number, end: number, step: number = 1): Iterable<number>
// assert(step > 0)
range(1, 1) // []
range(-2, 2) // [-2, -1, 0, 1]
range(2, -2) // [2, 1, 0, -1]
range(1, -1, 0.5) // [1, 0.5, 0, -0.5]
range(2, -2, 0) // throw Error
range(2, -2, -0.5) // throw Error

stringifyJSONStream

function stringifyJSONStream<T>(iterable: Iterable<T>): Iterable<string>

stringifyJSONStreamAsync

function stringifyNDJSONStreamAsync<T>(iterable: AsyncIterable<T>): AsyncIterable<string>

stringifyNDJSONStream

function stringifyNDJSONStream<T>(iterable: Iterable<T>): Iterable<string>

stringifyNDJSONStreamAsync

function stringifyNDJSONStreamAsync<T>(iterable: AsyncIterable<T>): AsyncIterable<string>

sse

interface IMessage {
  event?: string
  data: string
  id?: string
  retry?: number
}

function sse(message: IMessage): Iterable<string>

ReusableIterable

class ReusableIterable<T> implements IReusableIterable<T> {
  get done(): boolean | undefined

  constructor(iterable: Iterable<T>)

  close(): void
}

ReusableAsyncIterable

class ReusableAsyncIterable<T> implements IReusableAsyncIterable<T> {
  get done(): boolean | undefined

  constructor(iterable: AsyncIterable<T>)

  close(): Promise<void>
}