Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/proc/tools/deps/proc.ts>Enumerable

A better way to work with processes in Deno.
Go to Latest
class Enumerable
implements AsyncIterable<T>
Re-export
import { Enumerable } from "https://deno.land/x/proc@0.20.35/tools/deps/proc.ts";

Enumerable wrapper for AsyncIterable.

Use the factory function enumerate to create new instances.

Constructors

new
Enumerable(iter: AsyncIterable<T>)

Construct a new enumerable wrapper.

Properties

readonly
first: Promise<T>

Take the head of the enumeration.

This operation is equivalent to take(1) and consumes the enumeration.

readonly
lines: Lines<T>

Convert the output to text lines.

Note that this should probably only be used with small data. Consider chunkedLines to improve performance with larger data.

Methods

collect(): Promise<T[]>

Collect the items in this iterator to an array.

concat(other: AsyncIterable<T>): Enumerable<T>

Drop the first n items, return the rest.

concurrentMap<U>(mapFn: (item: T) => Promise<U>, options?: ConcurrentOptions): Enumerable<U>

Map the sequence from one type to another, concurrently.

Results are returned in order.

concurrentUnorderedMap<U>(mapFn: (item: T) => Promise<U>, options?: ConcurrentOptions): Enumerable<U>

Map the sequence from one type to another, concurrently.

Items are iterated out of order. This allows maximum concurrency at all times, but the output order cannot be assumed to be the same as the input order.

count(filterFn?: (item: T) => boolean | Promise<boolean>): Promise<number>

Count the number of items; optionally with a filter.

drop<N extends number = 1>(n?: N): Enumerable<T>

Drop the first n items, return the rest.

filter(filterFn: (item: T) => boolean | Promise<boolean>): Enumerable<T>

Filter the sequence to contain just the items that pass a test.

filterNot(filterFn: (item: T) => boolean | Promise<boolean>): Enumerable<T>

Filter the sequence to exclude the items that pass a test. This returns the inverse of filter.

flatMap<U>(mapFn: (item: T) => U | Promise<U>): Enumerable<ElementType<U>>

Equivalent to calling map followed by flatten.

flatten(): Enumerable<ElementType<T>>

Flatten the iterable.

forEach(forEachFn: (item: T) => void | Promise<void>): Promise<void>

Perform an operation for each item in the sequence.

map<U>(mapFn: (item: T) => U | Promise<U>): Enumerable<U>

Map the iterator from one type to another.

reduce<U>(zero: U, reduceFn: (acc: U, item: T) => U | Promise<U>): Promise<U>

Reduce a sequence to a single value.

run<S>(options: ProcessOptions<S>, ...cmd: Cmd): Run<S, T>

Run a process.

run(...cmd: Cmd): Run<unknown, T>

Run a process.

take<N extends number = 1>(n?: N): Enumerable<T>

Take the first n items.

tee<N extends number = 2>(n?: N): Tuple<Enumerable<T>, N>

Split into 2 or more identical iterators.

Transform the iterable from one type to another with an opportunity to catch and handle errors.

unzip<A, B>(): Unzip<T>

Unzip a collection of [A, B] into Enumerable<A> and Enumerable<B>.

Note that this operations uses tee, so it will use memory during the iteration.

Example

const [a, b] = enumerate([[1, "A"], [2, "B"], [3, "C"]]).unzip();

// a is number[] -> [1, 2, 3]
// b is string[] -> ["A", "B", "C"]
writeTo(writer: Writable<T> | WritableStream<T>, options?: { noclose?: boolean; }): Promise<void>

Write all data to the writer.

Example

Write some numbers to stdout.

range({to: 99})
  .map(n => n.toString())
  .transform(toBytes)
  .writeTo(Deno.stdout.writable, {noclose: true});
zip<U>(other: AsyncIterable<U>): Enumerable<[T, U]>

Zip two Enumerables together. If collections are unequal length, the longer collection is truncated.

Example

const a = range({ from: 1, until: 3 });
const b = enumerate(["A", "B", "C"]);

const result = a.zip(b);

// [[1, "A"], [2, "B"], [3, "C"]]
[Symbol.asyncIterator](): AsyncGenerator<T, void, unknown>

Implement AsyncIterable<T>.