sflow
Version:
sflow is a powerful and highly-extensible library designed for processing and manipulating streams of data effortlessly. Inspired by the functional programming paradigm, it provides a rich set of utilities for transforming streams, including chunking, fil
21 lines (18 loc) • 639 B
text/typescript
import type { Awaitable } from "./Awaitable";
import { bys } from "./bys";
import { peeks } from "./peeks";
type MapFnIndexed<T> = (x: T, i?: number) => Awaitable<any>;
// type MapFnUnary<T> = (x: T) => Awaitable<any>;
/**
* log the value and index and return as original stream, handy to debug.
* Note: stream won't await the log function.
*/
export function logs<T>(mapFn: MapFnIndexed<T> = (s, i) => s) {
return bys(
peeks<T>(async (e, i) => {
const ret = mapFn(e, i);
const val = ret instanceof Promise ? await ret : ret;
console.log(typeof val === "string" ? val.replace(/\n$/, "") : val);
}),
);
}