Operators are functions that return a new object instance of type IntermediateStage
.
import { LazyPipeline } from 'lazy-pipeline';
import { toArray } from 'lazy-pipeline/collectors';
import { limit } from 'lazy-pipeline/operators';
const source = [2, 4, 6, 8, 10];
const result = LazyPipeline.from(source)
.add(
/*
* `limit()` operator returns an intermediate stage instance that only allows the first n elements to pass through
* the pipeline, in this case, only 3 elements are let through.
*/
limit(3)
)
.collect(
/**
* `toArray()` collector accumulates all remaining pipeline elements into an array and returns the resulting array.
*/
toArray()
);
console.log(result); // Outputs `[2, 4, 6]`.
Operator | Description |
---|---|
distinct |
Return an intermediate stage that lets only unique elements pass through. |
distinctBy |
Return an intermediate stage that lets only unique elements pass through, uniqueness is decided by the provided mapper function. |
dropWhile |
Return an intermediate stage that discards elements from the pipeline as long as the provided predicate is true . As soon as the predicate returns false , this stage will no longer execute even if the predicate returns true for subsequent elements down the pipeline. |
filter |
Return an intermediate stage that filters out elements that do not match the provided predicate. |
flatMap |
Return an intermediate stage that flattens the inner array returned by the flatMapper argument into the pipeline so that each returned array's element will flow through the pipeline one by one. |
limit |
Return an intermediate stage that only allows a fixed number of elements through the pipeline. |
map |
Return an intermediate stage that transforms each element according the provided mapping function. |
skip |
Return an intermediate stage that skips over a fixed number of elements. As soon as the requested number of elements have been skipped over, the returned stage will no longer run. |
sorted |
Return an intermediate stage that sorts the elements in this pipeline. This stage will accumulate all the elements before performing the sort, after that, it will resume the pipeline by sending the sorted elements down the pipeline. If no comparator function is provided, then the sorting behavior is the same as that of Array.prototype.sort where elements are compared by their character code points, otherwise, sorting behavior is dictated by the provided comparator where a returned value of -1 indicates that the left element should come before the right element, 1 indicates that the left element should come after the right element, and 0 indicates equality. |
takeWhile |
Return an intermediate stage that consumes elements in the pipeline as long as the provided predicate is true . As soon as the predicate returns false , this stage will no longer execute even if the predicate returns true for subsequent elements down the pipeline. |