;
/**
Retrieves the property with the given name from the iterator.
If no callback is passed, it returns the value of the property
or `undefined` if the property is not set.
If a callback is passed, it returns `undefined`
and calls the callback with the property the moment it is set.
@param {string} propertyName The name of the property to retrieve
@param {Function?} [callback] A one-argument callback to receive the property value
@returns {object?} The value of the property (if set and no callback is given)
*/
getProperty(propertyName: string, callback?: (value: P) => void): P | undefined;
/**
Sets the property with the given name to the value.
@param {string} propertyName The name of the property to set
@param {object?} value The new value of the property
*/
setProperty
(propertyName: string, value: P): void;
/**
Retrieves all properties of the iterator.
@returns {object} An object with property names as keys.
*/
getProperties(): {
[name: string]: any;
};
/**
Sets all of the given properties.
@param {object} properties Key/value pairs of properties to set
*/
setProperties(properties: {
[name: string]: any;
}): void;
/**
Copies the given properties from the source iterator.
@param {module:asynciterator.AsyncIterator} source The iterator to copy from
@param {Array} propertyNames List of property names to copy
*/
copyProperties(source: AsyncIterator, propertyNames: string[]): void;
/**
Transforms items from this iterator.
After this operation, only read the returned iterator instead of the current one.
@param {object|Function} [options] Settings of the iterator, or the transformation function
@param {integer} [options.maxbufferSize=4] The maximum number of items to keep in the buffer
@param {boolean} [options.autoStart=true] Whether buffering starts directly after construction
@param {integer} [options.offset] The number of items to skip
@param {integer} [options.limit] The maximum number of items
@param {Function} [options.filter] A function to synchronously filter items from the source
@param {Function} [options.map] A function to synchronously transform items from the source
@param {Function} [options.transform] A function to asynchronously transform items from the source
@param {boolean} [options.optional=false] If transforming is optional, the original item is pushed when its mapping yields `null` or its transformation yields no items
@param {Array|module:asynciterator.AsyncIterator} [options.prepend] Items to insert before the source items
@param {Array|module:asynciterator.AsyncIterator} [options.append] Items to insert after the source items
@returns {module:asynciterator.AsyncIterator} A new iterator that maps the items from this iterator
*/
transform(options: TransformOptions): AsyncIterator;
/**
Maps items from this iterator using the given function.
After this operation, only read the returned iterator instead of the current one.
@param {Function} map A mapping function to call on this iterator's (remaining) items
@param {object?} self The `this` pointer for the mapping function
@returns {module:asynciterator.AsyncIterator} A new iterator that maps the items from this iterator
*/
map(map: MapFunction, self?: any): AsyncIterator;
/**
Return items from this iterator that match the filter.
After this operation, only read the returned iterator instead of the current one.
@param {Function} filter A filter function to call on this iterator's (remaining) items
@param {object?} self The `this` pointer for the filter function
@returns {module:asynciterator.AsyncIterator} A new iterator that filters items from this iterator
*/
filter(filter: (item: T) => item is K, self?: any): AsyncIterator;
filter(filter: (item: T) => boolean, self?: any): AsyncIterator;
/**
* Returns a new iterator containing all of the unique items in the original iterator.
* @param by - The derived value by which to determine uniqueness (e.g., stringification).
Defaults to the identity function.
* @returns An iterator with duplicates filtered out.
*/
uniq(by?: (item: T) => any): AsyncIterator;
/**
Prepends the items after those of the current iterator.
After this operation, only read the returned iterator instead of the current one.
@param {Array|module:asynciterator.AsyncIterator} items Items to insert before this iterator's (remaining) items
@returns {module:asynciterator.AsyncIterator} A new iterator that prepends items to this iterator
*/
prepend(items: T[] | AsyncIterator): AsyncIterator;
/**
Appends the items after those of the current iterator.
After this operation, only read the returned iterator instead of the current one.
@param {Array|module:asynciterator.AsyncIterator} items Items to insert after this iterator's (remaining) items
@returns {module:asynciterator.AsyncIterator} A new iterator that appends items to this iterator
*/
append(items: T[] | AsyncIterator): AsyncIterator;
/**
Surrounds items of the current iterator with the given items.
After this operation, only read the returned iterator instead of the current one.
@param {Array|module:asynciterator.AsyncIterator} prepend Items to insert before this iterator's (remaining) items
@param {Array|module:asynciterator.AsyncIterator} append Items to insert after this iterator's (remaining) items
@returns {module:asynciterator.AsyncIterator} A new iterator that appends and prepends items to this iterator
*/
surround(prepend: AsyncIteratorOrArray, append: AsyncIteratorOrArray): AsyncIterator;
/**
Skips the given number of items from the current iterator.
The current iterator may not be read anymore until the returned iterator ends.
@param {integer} offset The number of items to skip
@returns {module:asynciterator.AsyncIterator} A new iterator that skips the given number of items
*/
skip(offset: number): AsyncIterator;
/**
Limits the current iterator to the given number of items.
The current iterator may not be read anymore until the returned iterator ends.
@param {integer} limit The maximum number of items
@returns {module:asynciterator.AsyncIterator} A new iterator with at most the given number of items
*/
take(limit: number): AsyncIterator;
/**
Limits the current iterator to the given range.
The current iterator may not be read anymore until the returned iterator ends.
@param {integer} start Index of the first item to return
@param {integer} end Index of the last item to return
@returns {module:asynciterator.AsyncIterator} A new iterator with items in the given range
*/
range(start: number, end: number): AsyncIterator;
/**
Creates a copy of the current iterator,
containing all items emitted from this point onward.
Further copies can be created; they will all start from this same point.
After this operation, only read the returned copies instead of the original iterator.
@returns {module:asynciterator.AsyncIterator} A new iterator that contains all future items of this iterator
*/
clone(): ClonedIterator;
/**
* An AsyncIterator is async iterable.
* This allows iterators to be used via the for-await syntax.
*
* In cases where the returned EcmaScript AsyncIterator will not be fully consumed,
* it is recommended to manually listen for error events on the main AsyncIterator
* to avoid uncaught error messages.
*
* @returns {ESAsyncIterator} An EcmaScript AsyncIterator
*/
[Symbol.asyncIterator](): ESAsyncIterator;
}
/**
An iterator that doesn't emit any items.
@extends module:asynciterator.AsyncIterator
*/
export declare class EmptyIterator extends AsyncIterator {
/** Creates a new `EmptyIterator`. */
constructor();
}
/**
An iterator that emits a single item.
@extends module:asynciterator.AsyncIterator
*/
export declare class SingletonIterator extends AsyncIterator {
private _item;
/**
Creates a new `SingletonIterator`.
@param {object} item The item that will be emitted.
*/
constructor(item: T);
read(): T | null;
protected _toStringDetails(): string;
}
/**
An iterator that emits the items of a given array.
@extends module:asynciterator.AsyncIterator
*/
export declare class ArrayIterator extends AsyncIterator {
private _buffer?;
protected _index: number;
protected _sourceStarted: boolean;
protected _truncateThreshold: number;
/**
Creates a new `ArrayIterator`.
@param {Array} items The items that will be emitted.
@param {boolean} [options.autoStart=true] Whether buffering starts directly after construction
@param {boolean} [options.preserve=true] If false, the passed array can be safely modified
*/
constructor(items?: Iterable, { autoStart, preserve }?: {
autoStart?: boolean | undefined;
preserve?: boolean | undefined;
});
read(): T | null;
protected _toStringDetails(): string;
protected _destroy(cause: Error | undefined, callback: (error?: Error) => void): void;
/**
Consume all remaining items of the iterator into an array that will be returned asynchronously.
@param {object} [options] Settings for array creation
@param {integer} [options.limit] The maximum number of items to place in the array.
*/
toArray(options?: {
limit?: number;
}): Promise;
}
/**
An iterator that enumerates integers in a certain range.
@extends module:asynciterator.AsyncIterator
*/
export declare class IntegerIterator extends AsyncIterator {
private _next;
private _step;
private _last;
/**
Creates a new `IntegerIterator`.
@param {object} [options] Settings of the iterator
@param {integer} [options.start=0] The first number to emit
@param {integer} [options.end=Infinity] The last number to emit
@param {integer} [options.step=1] The increment between two numbers
*/
constructor({ start, step, end }?: {
start?: number;
step?: number;
end?: number;
});
read(): number | null;
protected _toStringDetails(): string;
}
/**
* A synchronous mapping function from one element to another.
* A return value of `null` means that nothing should be emitted for a particular item.
*/
export declare type MapFunction = (item: S) => D | null;
/** Function that maps an element to itself. */
export declare function identity(item: S): typeof item;
/** Key indicating the current consumer of a source. */
export declare const DESTINATION: unique symbol;
/**
An iterator that synchronously transforms every item from its source
by applying a mapping function.
@extends module:asynciterator.AsyncIterator
*/
export declare class MappingIterator extends AsyncIterator {
protected readonly _map: MapFunction;
protected readonly _source: InternalSource;
protected readonly _destroySource: boolean;
/**
* Applies the given mapping to the source iterator.
*/
constructor(source: AsyncIterator, map?: MapFunction, options?: SourcedIteratorOptions);
read(): D | null;
protected _end(destroy: boolean): void;
}
/**
An iterator that maintains an internal buffer of items.
This class serves as a base class for other iterators
with a typically complex item generation process.
@extends module:asynciterator.AsyncIterator
*/
export declare class BufferedIterator extends AsyncIterator {
private _buffer;
private _maxBufferSize;
protected _reading: boolean;
protected _pushedCount: number;
protected _sourceStarted: boolean;
/**
Creates a new `BufferedIterator`.
@param {object} [options] Settings of the iterator
@param {integer} [options.maxBufferSize=4] The number of items to preload in the internal buffer
@param {boolean} [options.autoStart=true] Whether buffering starts directly after construction
*/
constructor({ maxBufferSize, autoStart }?: BufferedIteratorOptions);
/**
The maximum number of items to preload in the internal buffer.
A `BufferedIterator` tries to fill its buffer as far as possible.
Set to `Infinity` to fully drain the source.
@type number
*/
get maxBufferSize(): number;
set maxBufferSize(maxBufferSize: number);
/**
Initializing the iterator by calling {@link BufferedIterator#_begin}
and changing state from INIT to OPEN.
@protected
@param {boolean} autoStart Whether reading of items should immediately start after OPEN.
*/
protected _init(autoStart: boolean): void;
/**
Writes beginning items and opens iterator resources.
Should never be called before {@link BufferedIterator#_init};
typically, `_init` is responsible for calling `_begin`.
@protected
@param {function} done To be called when initialization is complete
*/
protected _begin(done: () => void): void;
/**
Tries to read the next item from the iterator.
If the buffer is empty,
this method calls {@link BufferedIterator#_read} to fetch items.
@returns {object?} The next item, or `null` if none is available
*/
read(): T | null;
/**
Tries to generate the given number of items.
Implementers should add `count` items through {@link BufferedIterator#_push}.
@protected
@param {integer} count The number of items to generate
@param {function} done To be called when reading is complete
*/
protected _read(count: number, done: () => void): void;
/**
Adds an item to the internal buffer.
@protected
@param {object} item The item to add
@emits module:asynciterator.AsyncIterator.readable
*/
protected _push(item: T): void;
/**
Fills the internal buffer until `this._maxBufferSize` items are present.
This method calls {@link BufferedIterator#_read} to fetch items.
@protected
@emits module:asynciterator.AsyncIterator.readable
*/
protected _fillBuffer(): void;
/**
Schedules `_fillBuffer` asynchronously.
*/
protected _fillBufferAsync(): void;
/**
Stops the iterator from generating new items
after a possible pending read operation has finished.
Already generated, pending, or terminating items can still be emitted.
After this, the iterator will end asynchronously.
@emits module:asynciterator.AsyncIterator.end
*/
close(): void;
/**
Stops the iterator from generating new items,
switching from `CLOSING` state into `CLOSED` state.
@protected
@emits module:asynciterator.AsyncIterator.end
*/
protected _completeClose(): void;
protected _destroy(cause: Error | undefined, callback: (error?: Error) => void): void;
/**
Writes terminating items and closes iterator resources.
Should never be called before {@link BufferedIterator#close};
typically, `close` is responsible for calling `_flush`.
@protected
@param {function} done To be called when termination is complete
*/
protected _flush(done: () => void): void;
/**
Generates details for a textual representation of the iterator.
@protected
*/
protected _toStringDetails(): string;
}
/**
An iterator that generates items based on a source iterator.
This class serves as a base class for other iterators.
@extends module:asynciterator.BufferedIterator
*/
export declare class TransformIterator extends BufferedIterator {
protected _source?: InternalSource;
protected _createSource?: (() => MaybePromise>) | null;
protected _destroySource: boolean;
protected _optional: boolean;
protected _boundPush: (item: D) => void;
/**
Creates a new `TransformIterator`.
@param {module:asynciterator.AsyncIterator|Readable} [source] The source this iterator generates items from
@param {object} [options] Settings of the iterator
@param {integer} [options.maxBufferSize=4] The maximum number of items to keep in the buffer
@param {boolean} [options.autoStart=true] Whether buffering starts directly after construction
@param {boolean} [options.optional=false] If transforming is optional, the original item is pushed when its transformation yields no items
@param {boolean} [options.destroySource=true] Whether the source should be destroyed when this transformed iterator is closed or destroyed
@param {module:asynciterator.AsyncIterator} [options.source] The source this iterator generates items from
*/
constructor(source?: SourceExpression, options?: TransformIteratorOptions);
/**
The source this iterator generates items from.
@type module:asynciterator.AsyncIterator
*/
get source(): AsyncIterator | undefined;
set source(value: AsyncIterator | undefined);
/**
Initializes a source that was set through a promise
@protected
*/
protected _loadSourceAsync(): void;
/**
Validates whether the given iterator can be used as a source.
@protected
@param {object} source The source to validate
@param {boolean} allowDestination Whether the source can already have a destination
*/
protected _validateSource(source?: AsyncIterator, allowDestination?: boolean): InternalSource;
/**
Tries to read transformed items.
*/
protected _read(count: number, done: () => void): void;
/**
Reads a transforms an item
*/
protected _readAndTransform(next: () => void, done: () => void): void;
/**
Tries to transform the item;
if the transformation yields no items, pushes the original item.
*/
protected _optionalTransform(item: S, done: () => void): void;
/**
Generates items based on the item from the source.
Implementers should add items through {@link BufferedIterator#_push}.
The default implementation pushes the source item as-is.
@protected
@param {object} item The last read item from the source
@param {function} done To be called when reading is complete
@param {function} push A callback to push zero or more transformation results.
*/
protected _transform(item: S, done: () => void, push: (i: D) => void): void;
/**
Closes the iterator when pending items are transformed.
@protected
*/
protected _closeWhenDone(): void;
protected _end(destroy: boolean): void;
}
/**
An iterator that generates items based on a source iterator
and simple transformation steps passed as arguments.
@extends module:asynciterator.TransformIterator
*/
export declare class SimpleTransformIterator extends TransformIterator {
private _offset;
private _limit;
private _prepender?;
private _appender?;
private _filter;
private _map?;
/**
Creates a new `SimpleTransformIterator`.
@param {module:asynciterator.AsyncIterator|Readable} [source] The source this iterator generates items from
@param {object|Function} [options] Settings of the iterator, or the transformation function
@param {integer} [options.maxbufferSize=4] The maximum number of items to keep in the buffer
@param {boolean} [options.autoStart=true] Whether buffering starts directly after construction
@param {module:asynciterator.AsyncIterator} [options.source] The source this iterator generates items from
@param {integer} [options.offset] The number of items to skip
@param {integer} [options.limit] The maximum number of items
@param {Function} [options.filter] A function to synchronously filter items from the source
@param {Function} [options.map] A function to synchronously transform items from the source
@param {Function} [options.transform] A function to asynchronously transform items from the source
@param {boolean} [options.optional=false] If transforming is optional, the original item is pushed when its mapping yields `null` or its transformation yields no items
@param {Array|module:asynciterator.AsyncIterator} [options.prepend] Items to insert before the source items
@param {Array|module:asynciterator.AsyncIterator} [options.append] Items to insert after the source items
*/
constructor(source?: SourceExpression, options?: TransformOptions | (TransformOptions & ((item: S, done: () => void) => void)));
protected _read(count: number, done: () => void): void;
protected _readAndTransformSimple(count: number, next: () => void, done: () => void): void;
protected _begin(done: () => void): void;
protected _flush(done: () => void): void;
protected _insert(inserter: AsyncIterator | undefined, done: () => void): void;
}
/**
An iterator that generates items by transforming each item of a source
with a different iterator.
@extends module:asynciterator.TransformIterator
*/
export declare class MultiTransformIterator extends TransformIterator {
private _transformerQueue;
/**
Creates a new `MultiTransformIterator`.
@param {module:asynciterator.AsyncIterator|Readable} [source] The source this iterator generates items from
@param {object|Function} [options] Settings of the iterator, or the transformation function
@param {integer} [options.maxbufferSize=4] The maximum number of items to keep in the buffer
@param {boolean} [options.autoStart=true] Whether buffering starts directly after construction
@param {module:asynciterator.AsyncIterator} [options.source] The source this iterator generates items from
@param {integer} [options.offset] The number of items to skip
@param {integer} [options.limit] The maximum number of items
@param {Function} [options.filter] A function to synchronously filter items from the source
@param {Function} [options.map] A function to synchronously transform items from the source
@param {Function} [options.transform] A function to asynchronously transform items from the source
@param {boolean} [options.optional=false] If transforming is optional, the original item is pushed when its mapping yields `null` or its transformation yields no items
@param {Function} [options.multiTransform] A function to asynchronously transform items to iterators from the source
@param {Array|module:asynciterator.AsyncIterator} [options.prepend] Items to insert before the source items
@param {Array|module:asynciterator.AsyncIterator} [options.append] Items to insert after the source items
*/
constructor(source: AsyncIterator, options?: MultiTransformOptions | (MultiTransformOptions & ((item: S) => AsyncIterator)));
protected _read(count: number, done: () => void): void;
/**
Creates a transformer for the given item.
@param {object} item The last read item from the source
@returns {module:asynciterator.AsyncIterator} An iterator that transforms the given item
*/
protected _createTransformer(item: S): AsyncIterator;
protected _closeWhenDone(): void;
protected _end(destroy: boolean): void;
}
/**
An iterator that generates items by reading from multiple other iterators.
@extends module:asynciterator.BufferedIterator
*/
export declare class UnionIterator extends BufferedIterator {
private _sources;
private _pending?;
private _currentSource;
protected _destroySources: boolean;
/**
Creates a new `UnionIterator`.
@param {module:asynciterator.AsyncIterator|Array} [sources] The sources to read from
@param {object} [options] Settings of the iterator
@param {boolean} [options.destroySource=true] Whether the sources should be destroyed when transformed iterator is closed or destroyed
*/
constructor(sources: AsyncIteratorOrArray> | AsyncIteratorOrArray>> | AsyncIteratorOrArray>>, options?: BufferedIteratorOptions & {
destroySources?: boolean;
});
protected _loadSources(): void;
protected _addSource(source: MaybePromise>): void;
protected _removeEmptySources(): void;
protected _read(count: number, done: () => void): void;
protected _end(destroy?: boolean): void;
}
/**
An iterator that copies items from another iterator.
@extends module:asynciterator.TransformIterator
*/
export declare class ClonedIterator extends TransformIterator {
private _readPosition;
/**
Creates a new `ClonedIterator`.
@param {module:asynciterator.AsyncIterator|Readable} [source] The source this iterator copies items from
*/
constructor(source: AsyncIterator);
protected _init(): void;
close(): void;
get source(): AsyncIterator | undefined;
set source(value: AsyncIterator | undefined);
/**
Validates whether the given iterator can be used as a source.
@protected
@param {object} source The source to validate
@param {boolean} allowDestination Whether the source can already have a destination
*/
protected _validateSource(source?: AsyncIterator, allowDestination?: boolean): InternalSource;
getProperty(propertyName: string, callback?: (value: P) => void): P | undefined;
protected _getSourceProperty
(propertyName: string, callback: (value: P) => void): void;
getProperties(): {
[name: string]: any;
};
protected _toStringDetails(): string;
read(): T | null;
protected _end(destroy: boolean): void;
}
/**
* An iterator that takes a variety of iterable objects as a source.
*/
export declare class WrappingIterator extends AsyncIterator {
protected _source: InternalSource | null;
protected _destroySource: boolean;
constructor(source?: MaybePromise>, opts?: SourcedIteratorOptions);
set source(value: IterableSource);
read(): T | null;
protected _end(destroy?: boolean): void;
}
/**
Creates an iterator that wraps around a given iterator or readable stream.
Use this to convert an iterator-like object into a full-featured AsyncIterator.
After this operation, only read the returned iterator instead of the given one.
@function
@param [source] The source this iterator generates items from
@param {object} [options] Settings of the iterator
@returns {module:asynciterator.AsyncIterator} A new iterator with the items from the given iterator
*/
export declare function wrap(source?: MaybePromise> | null, options?: TransformIteratorOptions): AsyncIterator;
/**
Creates an empty iterator.
*/
export declare function empty(): AsyncIterator;
/**
Creates an iterator with a single item.
@param {object} item the item
*/
export declare function single(item: T): AsyncIterator;
/**
Creates an iterator for the given array.
@param {Array} items the items
*/
export declare function fromArray(items: Iterable): AsyncIterator;
/**
Creates an iterator for the given Iterator.
@param {Iterable} source the iterator
*/
export declare function fromIterator(source: Iterable | Iterator): AsyncIterator;
/**
Creates an iterator for the given Iterable.
@param {Iterable} source the iterable
*/
export declare function fromIterable(source: Iterable | Iterator): AsyncIterator;
/**
Creates an iterator containing all items from the given iterators.
@param {Array} items the items
*/
export declare function union(sources: AsyncIteratorOrArray> | AsyncIteratorOrArray>> | AsyncIteratorOrArray>>): UnionIterator;
/**
Creates an iterator of integers for the given numeric range.
@param {Array} items the items
*/
export declare function range(start: number, end: number, step?: number): IntegerIterator;
export declare type IterableSource = T[] | AsyncIterator | EventEmitter | Iterator | Iterable;
export interface SourcedIteratorOptions {
destroySource?: boolean;
}
export interface BufferedIteratorOptions {
maxBufferSize?: number;
autoStart?: boolean;
}
export interface TransformIteratorOptions extends SourcedIteratorOptions, BufferedIteratorOptions {
source?: SourceExpression;
optional?: boolean;
}
export interface TransformOptions extends TransformIteratorOptions {
offset?: number;
limit?: number;
prepend?: AsyncIteratorOrArray;
append?: AsyncIteratorOrArray;
filter?: (item: S) => boolean;
map?: (item: S) => D;
transform?: (item: S, done: () => void, push: (i: D) => void) => void;
}
export interface MultiTransformOptions extends TransformOptions {
multiTransform?: (item: S) => AsyncIterator;
}
/**
* Copy of the EcmaScript AsyncIterator interface, which we can not use directly due to the name conflict.
*/
interface ESAsyncIterator {
next(value?: any): Promise>;
}
declare type MaybePromise = T | Promise;
declare type AsyncIteratorOrArray = T[] | AsyncIterator;
declare type SourceExpression = MaybePromise> | (() => MaybePromise>);
declare type InternalSource = AsyncIterator & {
[DESTINATION]?: AsyncIterator;
};
export declare function isFunction(object: any): object is Function;
export declare function isEventEmitter(object: any): object is EventEmitter;
export declare function isPromise(object: any): object is Promise;
export declare function isSourceExpression(object: any): object is SourceExpression;
export declare function isIterable(object: {
[key: string]: any;
}): object is Iterable;
export declare function isIterator(object: {
[key: string]: any;
}): object is Iterator;