gigobyte / purify

Functional programming library for TypeScript - https://gigobyte.github.io/purify/
ISC License
1.5k stars 59 forks source link

chainNullable for Either #598

Closed mahulst closed 1 year ago

mahulst commented 2 years ago

Sometimes when I'm mapping over an Either that contains an object or an array, I want to do a similar action as chainNullable from the Maybe type does.

type MyType = { [key: string]: number }
const a: MyType = { a: 1 };
const either: Either<string, MyType>  = Right(a);

const eitherNumber = either.chainNullable(obj => obj['a'], 'Left in case of not found'); 

Current workaround is:

type MyType = { [key: string]: number }
const a: MyType = { a: 1 };
const either: Either<string, MyType>  = Right(a);

const eitherNumber = either.chain(obj => {
    return Maybe.fromNullable(
        obj['a'],
    ).toEither('Left in case of not found');
});

I was wondering if this usecase was usefull enough to warrant a helper method on the Either type.

gigobyte commented 1 year ago

Since there has been no further interest in this method I'll consider it too niche to be part of the library.