🚧 Currently Under Development: Please note that this library is a work-in-progress. Features and API may change as development progresses. Feedback and contributions are welcome!
The Pipe class is a versatile utility for TypeScript that enables fluent and expressive operations on iterable sequences. It is designed to provide a chainable, lazy evaluation approach for handling collections of data. With methods like where, each, take, and all, Pipe allows users to efficiently process and manipulate iterable sequences.
To use the Pipe class, simply import it into your TypeScript project:
import Pipe from './path/to/pipe';Ensure that the file containing the Pipe class is correctly located in your project structure.
You can create a Pipe instance by passing any iterable object to its constructor.
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const pipe = new Pipe(numbers);-
where(predicate: Predicate<TSequence>): Filters the sequence based on a given predicate.
const evenNumbers = pipe.where(n => n % 2 === 0);
-
each<TReturn>(transform: Transform<TSequence, TReturn>): Transforms each element in the sequence using the provided function.
const doubled = pipe.each(n => n * 2);
-
take(count: number): Takes the first
countelements from the sequence.const firstTwo = pipe.take(2);
-
all(): Converts the entire sequence into an array.
const allNumbers = pipe.all();
The Pipe class implements the iterable protocol, so you can use it directly with a for...of loop.
for (const num of pipe) {
console.log(num);
}Methods of Pipe return a new Pipe instance, allowing for method chaining.
const result = new Pipe(numbers)
.where(n => n % 2 === 0)
.each(n => n * 2)
.take(3)
.all();- Predicate<TValue>: A function type that takes a value of type
TValueand returns a boolean. - Transform<TValue, TReturn>: A function type that takes a value of type
TValueand transforms it into a value of typeTReturn.
The Pipe class is compatible with any TypeScript project that supports ES6 features, particularly iterable protocols and generator functions.
This project is open-sourced and can be freely used and modified in accordance with the chosen license terms.