Skip to content

Instantly share code, notes, and snippets.

@impurist
Created July 8, 2021 04:22
Show Gist options
  • Save impurist/cdfaf0a1f47447841c8ebe6f4662eb33 to your computer and use it in GitHub Desktop.
Save impurist/cdfaf0a1f47447841c8ebe6f4662eb33 to your computer and use it in GitHub Desktop.
either-option
import * as E from 'fp-ts/lib/Either';
import * as O from 'fp-ts/lib/Option';
import { flow, pipe } from 'fp-ts/function';
describe('Either', () => {
describe('.fromNullable', () => {
const errorMessage = 'input was null or undefined';
const toEither = E.fromNullable(errorMessage);
it('should return right with string', async () => {
const expected = 'abc';
const myEither = toEither(expected);
expect(myEither).toEqual(E.right(expected));
});
it('should return left with undefined', async () => {
const myEither = toEither(undefined);
expect(myEither).toEqual(E.left(errorMessage));
});
it('folds left to error state', () => {
const myEither = toEither(undefined);
expect(
pipe(
myEither,
E.fold(
(e) => e,
(a) => a
)
)
).toEqual(errorMessage);
});
it('folds right to success state', () => {
const payloadHandler = E.fromNullable('Payload is null or undefined');
const getAndUpcaseOrError = flow(
E.map((a: string) => a.toUpperCase()),
E.fold(
(e: string) => e,
(a: string) => a
)
);
expect(getAndUpcaseOrError(payloadHandler('payload'))).toEqual('PAYLOAD');
});
});
describe('.tryCatch', () => {
const errorMessage = 'call on nil or undefined nested object';
it('should return right with string', async () => {
const payload = { a: { b: { c: 'c' } } };
const myEither = E.tryCatch(
() => payload.a.b.c.toUpperCase(),
() => Error('unknown error')
);
expect(myEither).toEqual(E.right('C'));
});
it('should return left with error', async () => {
const payload: { a: { b: { c: string | undefined } } } = {
a: { b: { c: undefined } },
};
const error = new Error(errorMessage);
const myEither = E.tryCatch(
() => payload.a.b.c.toUpperCase(),
() => error
);
expect(myEither).toEqual(E.left(error));
});
it('folds left to error state', () => {
const payload: { a: { b: { c: string | undefined } } } = {
a: { b: undefined },
};
const payloadEither = E.tryCatch<Error, string>(
() => payload.a.b.c.toUpperCase(),
() => new Error(errorMessage)
);
const handler = (payloadE: E.Either<Error, string>) =>
pipe(
payloadE,
E.fold(
(e: Error): string => e.message,
(a: string) => a
)
);
expect(handler(payloadEither)).toEqual(errorMessage);
});
it('folds right to success state', () => {
const usePayload = (p: string): string => {
return `used ${p}`;
};
const payloadHandler = E.fromNullable(
new Error('Payload is null or undefined')
);
const getAndUpcaseOrError = (ph) =>
pipe(
ph,
E.map((a: string) => a.toUpperCase()),
E.fold(
(e: Error) => e.message,
(a: string) => usePayload(a)
)
);
expect(getAndUpcaseOrError(payloadHandler('payload'))).toEqual(
'used PAYLOAD'
);
});
});
});
describe('Option', () => {
describe('.fromNullable', () => {
it('should return right with string', async () => {
const expected = 'abc';
const myOption = O.fromNullable(expected);
expect(myOption).toEqual(O.some(expected));
});
it('should return left with undefined', async () => {
const myOption = O.fromNullable(undefined);
expect(myOption).toEqual(O.none);
});
it('folds left to error state', () => {
const myOption = O.fromNullable(undefined);
expect(
pipe(
myOption,
O.fold(
() => O.none,
(a) => a
)
)
).toEqual(O.none);
});
it('folds right to success state', () => {
const getAndUpcaseOrError = flow(
O.map((a: string) => a.toUpperCase()),
O.fold(
() => O.none,
(a: string) => O.some(a)
)
);
expect(getAndUpcaseOrError(O.fromNullable('payload'))).toEqual(
O.some('PAYLOAD')
);
});
});
describe('.tryCatch', () => {});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment