@panyam/tsutils
Version:
Some basic TS utils for personal use
40 lines • 1.05 kB
JavaScript
export function* streamArray(arr) {
for (let i = 0; i < arr.length; i++) {
yield [i, arr[i]];
}
}
export function* streamDict(dict) {
for (const key in dict) {
yield [key, dict[key]];
}
}
export function* mapStream(stream, mapper) {
let i = 0;
for (let next = stream.next(); !next.done; next = stream.next()) {
yield mapper(next.value, i);
i++;
}
}
export function* filterStream(stream, filterFunc) {
let i = 0;
for (let next = stream.next(); !next.done; next = stream.next()) {
if (filterFunc) {
if (filterFunc(next.value, i)) {
yield next.value;
}
}
else if (next.value) {
yield next.value;
}
i++;
}
}
export function collectStream(stream, collector, collection) {
let i = 0;
for (let next = stream.next(); !next.done; next = stream.next()) {
collection = collector(next.value, collection, i);
i++;
}
return collection;
}
//# sourceMappingURL=streams.js.map