This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

my-simple-either
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

my-simple-either

Download Status Github Star Github Issues NPM version License

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>;

Readme

Keywords

Package Sidebar

Install

npm i my-simple-either

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

19.4 kB

Total Files

14

Last publish

Collaborators

  • jooni