Closed wassim-k closed 2 years ago
Closing, as I managed to achieve the same result with the use of:
export function propagateNull<T, R>(fn: (value: NonNullable<T>) => R) {
return (v: T): R | Extract<T, null | undefined> => isNonNull(v) ? fn(v as NonNullable<T>) : v;
}
pipeWith(
a,
propagateNull(b => calcB(b)),
propagateNull(c => calcC(c)),
propagateNull(d => calcD(d))
);
Hi,
Great work on this library, it's very clean and simple. I wanted to suggest a function that I believe can provide a lot of value to the library without comprimising its simplicity. The function is
rail
/railWith
, it's similar to pipe except it automatically exits when it first encounters anull | undefined
value.It basically allows developers to rewrite this:
into this:
The name of the function can be changed into whatever, but I chose it based very loosely on rail road programming principles.
Here's an example implementation:
If you see the value in it too, let me know and I'll be happy to polish the solution and raise a PR.
Keep up the good work 👍