previewable-iterator
v0.1.1
Published
Previewable Iterable/Iterator for JavaScript/TypeScript
Downloads
3
Readme
previewable-iterator
Previewable Iterable/Iterator for JavaScript/TypeScript
Install
npm install previewable-iterator
API
import {
PreviewableIterable,
previewable,
} from "previewable-iterator";
class PreviewableIterable
PreviewableIterable
implements IterableIterator
, which means it provides
proper interface for both Iterable and Iterator. About the interface, please
refer to the following MDN documentation.
constructor(iterator: Iterator)
It accepts an iterator and wraps it as PreviewableIterable
.
However, rather than directly using new PreviewableIterable(...)
, you may
prefer using the previewable(...)
helper function.
PreviewableIterable#preview()
It additionally provides a preview
to literally preview the result of the
iterable. In other words, it returns a previewed { value, done }
without
actually consuming an iteration step.
previewable(iterable: Iterable)
It is a helper function for PreviewableIterable
, accepts an iterable. It
creates an iterator from the iterable, and apply it to PreviewableIterable
.
// e.g.
const iter = previewable([1, 2, 3, 4]);
iter.next(); // { value: 1, done: false }
iter.next(); // { value: 2, done: false }
iter.preview(); // { value: 3, done: false }
iter.preview(); // { value: 3, done: false }
iter.next(); // { value: 3, done: false }
Development
# formatting scripts (using Prettier)
npm run check-format
npm run format
# build TypeScript
npm run build
# test
npm run test
The formatting scripts are automatically executed before commit or push.