1.1.4 • Published 2 years ago
@szilanor/stream v1.1.4
Stream API
Type-safe API for processing Iterable data (Arrays, Sets, Maps, Iterables) similarly to Java 8 Stream API, LINQ or Kotlin Sequences.
Classic Javascript solution
const result = [1, 2, 3].filter(x => x % 2 === 0).map(x => x * 2);
Stream API solution
Creating a Stream object
import {Stream, of, from} from '@szilanor/stream';
let stream: Stream<number>;
stream = new Stream([1, 2, 3]); // With constructor;
stream = of(1, 2, 3); // With the 'of' creator function
stream = from([1, 2, 3]); // With the 'from' creator function
Operations on stream entries for the same result
import {filter, map, compound} from '@szilanor/stream';
stream = stream.pipe(
filter(x => x % 2 === 0),
map(x => x * 2)
);
Process the stream for the same result
import {toArray} from '@szilanor/stream';
// With a collector
result = stream.collect(toArray());
// Using for..of
let result = [];
for (let entry of stream) {
result.push(entry);
}
// Using spread operator
result = [...stream];
Why Stream API?
- Can achieve faster results due to sequential processing
let allEven: boolean;
const input = [1, 2, 3, 4, 5];
// Classic JS maps all the entries first then returns false
allEven = input.map(x => x + 1).every(x => x % 2 === 0);
// vs Stream API maps only the first element then returns false
allEven = from(input)
.pipe(map(x => x + 1))
.collect(all(x => x % 2 === 0));
- More readable code
import {from, distinct, collect} from '@szilanor/stream';
// Filtering duplicates and group them by whether they are even or odd
const input = [1, 1, 1, 1, 2, 3, 4, 4, 5];
// Classic JS
const resultClassic: Map<string, number[]> = new Map<string, number[]>();
Array.from(new Set<number>(input)).forEach(x => {
const key = x % 2 === 0 ? 'even' : 'odd';
resultClassic.set(key, [...(resultClassic.get(key) || []), x]);
});
// Stream API
const resultStreamApi: Map<string, number[]> = from(input)
.pipe(distinct())
.collect(groupBy(x => (x % 2 === 0 ? 'even' : 'odd')));
- You can create your own operators and collectors if you don't find what you need
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());
1.1.4
2 years ago
1.1.3
2 years ago
1.1.2
2 years ago
1.1.1
2 years ago
1.0.44
2 years ago
1.0.43
2 years ago
1.0.42
2 years ago
1.0.41
2 years ago
1.0.40
2 years ago
1.0.39
2 years ago
1.0.38
2 years ago
1.0.37
2 years ago
1.0.36
2 years ago
1.0.35
2 years ago
1.0.34
2 years ago
1.0.33
2 years ago
1.0.32
2 years ago
1.0.31
2 years ago
1.0.30
2 years ago
1.0.29
2 years ago
1.0.28
2 years ago
1.0.27
2 years ago
1.0.26
2 years ago
1.0.25
2 years ago
1.0.24
2 years ago
1.0.23
2 years ago
1.0.21
2 years ago
1.0.20
2 years ago
1.0.19
2 years ago
1.0.18
2 years ago
1.0.17
2 years ago
1.0.16
2 years ago
1.0.15
2 years ago
1.0.14
2 years ago
1.0.12
2 years ago
1.0.11
2 years ago
1.0.10
2 years ago
1.0.9
2 years ago
1.0.7
2 years ago
1.0.6
2 years ago
1.0.5
2 years ago
1.0.3
2 years ago
1.0.4
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago