1.0.0 • Published 3 years ago
my-simple-either v1.0.0
my-simple-either
Simple Type and Function set for Either interface. Either important concept in functional programming. But Node.js and TypeScript not support this concept. So you can choose fp-ts or anther functional utility. But if you need only Eihter, this package is good alternative.
Why? use Either?
Helpful for functional programming and integrate return type. See below.
import { parse as jsoncParse } from 'jsonc-parser';
import { parse as json5Parse } from 'json5';
function json5Parse(value: string): SimpleEither<Error, Record<any, any>> {
try {
return pass(json5Parse(value));
} catch (err) {
return fail(new Error(err));
}
}
function jsoncParse(value: string): SimpleEither<Error, Record<any, any>> {
try {
return pass(jsoncParse(value));
} catch (err) {
return fail(new Error(err));
}
}
function parse(value: string): Record<any, any> {
const jsoncParsedEither = jsoncParse(value);
if (isPass(jsoncParsedEither)) {
return jsoncParsedEither.pass;
}
const json5ParsedEither = json5Parse(value);
if (isPass(json5ParsedEither)) {
return json5ParsedEither.pass;
}
// also you can use "throw new Error(jsoncParsedEither.fail);"
throw new Error(json5ParsedEither.fail);
}throw keyword move control-flow. But Either, SimpleEither don't move control-flow besides Either helpful functional programming and function pipe.
Either
Name using left and right.
| name | description |
|---|---|
| ILeft | Left interface |
| IRight | Right interface |
| Either | Either type using ILeft and IRight |
| left | value convert ILeft type |
| right | value convert IRight type |
| isLeft | check given value is ILeft type |
| isRight | check given value is IRight type |
SimpleEither
Name using pass and fail.
| name | description |
|---|---|
| IFail | Fail interface |
| IPass | Pass interface |
| SimpleEither | SimpleEither type using IFail and IPass |
| fail | value convert IFail type |
| pass | value convert IPass type |
| isFail | check given value is IFail type |
| isPass | check given value is IPass type |
Generic type in Either, SimpleEither
First generic type is ILeft or IFail. Because fp-ts and many programming language choose first type is left(or fail).
type Either<TLEFT, TRIGHT> = ILeft<TLEFT> | IRight<TRIGHT>;1.0.0
3 years ago