Type-safe API for processing Iterable and AsyncIterable data (Arrays, Sets, Maps) similarly to Java 8 Stream API, LINQ or Kotlin Sequences.
// Classic
let result;
result = [1, 2, 3].filter(x => x % 2 === 0).map(x => x * 2);
// Stream API
result = stream([1, 2, 3])
.pipe(
filter(x => x % 2 === 0),
map(x => x * 2)
)
.collect(toArray());
const input = [1, 2, 3, .... 10000];
let allOdd: boolean;
// Classic JS
allOdd = input
.map(x => x + 1)
.every(x => x % 2 === 1);
// Result: 2, 3, 4 .... 10000 false
// Stream API
allOdd = from(input)
.pipe(map(x => x + 1))
.collect(every(x => x % 2 === 1));
// Result: 2, false
const result = await stream([1, 2, 3, 4])
.pipeAsync(mapAsync(async (id) =>
await fetch(`https://jsonplaceholder.typicode.com/users/${id}`)
.then(result => result.json()))
)
.collectAsync(toArrayAsync());
const input = [1, 1, 1, 1, 2, 3, 4, 4, 5];
let oddOrEvenWithoutDuplicates: Map<string, number[]>;
// Classic JS
oddOrEvenWithoutDuplicates = new Map<string, number[]>();
for (let x of new Set<number>(input))
const key = x % 2 === 0 ? 'even' : 'odd';
if (resultClassic.has(key)) {
resultClassic.get(key).push(x);
} else {
resultClassic.set(key, [x]);
}
}
// Stream API
oddOrEvenWithoutDuplicates = stream(input)
.pipe(distinct())
.collect(groupBy(x => (x % 2 === 0 ? 'even' : 'odd')));
import {CollectorFunction, OperationFunction} from '@szilanor/stream';
const myAwesomeCollector: CollectorFunction<unknown, unknown> = {
/* your own implementation */
};
const myAwesomeOperation: OperationFunction<unknown, unknown> = {
/* your own implementation */
};
const result = of(1, 2, 3)
.pipe(myAwesomeOperation())
.collect(myAwesomeCollector());