New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

my-only-either

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

my-only-either

Only support either interface

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.1K
decreased by-0.88%
Maintainers
1
Weekly downloads
 
Created
Source

my-only-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 don't have implmentation. So you can choose another functional implementation like fp-ts or anther functional utility. But if you need only Eihter, this package is good alternative.

Why? use Either?

Zero Dependency

my-only-either not use package. only 1k(1,489byte) size.

Help functional programming

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): PassFailEither<Error, Record<any, any>> {
  try {
    return pass(json5Parse(value));
  } catch (err) {
    return fail(new Error(err));
  }
}

function jsoncParse(value: string): PassFailEither<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;    
  }

  throw new Error(json5ParsedEither.fail);
}

throw keyword move control-flow. But Either, PassFailEither don't move control-flow besides Either helpful functional programming and function pipe.

Either

Name using left and right.

namecategorydescription
ILefttypeLeft interface
IRighttypeRight interface
EithertypeEither type using ILeft and IRight
TPickLeftutility typeReturn Type of left in Either
TPickILeftutility typeReturn Type of ILeft in Either
TPickRightutility typeReturn Type of right in Either
TPickIRightutility typeReturn Type of IRight in Either
leftfunctionvalue convert ILeft type
rightfunctionvalue convert IRight type
isLeftfunctioncheck given value is ILeft type
isRightfunctioncheck given value is IRight type

PassFailEither

Name using pass and fail.

namecategorydescription
IFailtypeFail interface
IPasstypePass interface
PassFailEithertypePassFailEither type using IFail and IPass
TPickFailutility typeReturn Type of fail in Either
TPickIFailutility typeReturn Type of IFail in Either
TPickPassutility typeReturn Type of pass in Either
TPickIPassutility typeReturn Type of IPass in Either
failfunctionvalue convert IFail type
passfunctionvalue convert IPass type
efailfunctionvalue convert IFail type, exactly same fail. If you use jest or test runner this function is helpful for auto import & auto complete
epassfunctionvalue convert IPass type, exactly same pass. If you use jest or test runner this function is helpful for auto import & auto complete
isFailfunctioncheck given value is IFail type
isPassfunctioncheck given value is IPass type

Type order in Eiter, PassFailEither

First type arguments is ILeft or IFail. Because fp-ts and many functional programming language choose first type is left(or fail).

type Either<TLEFT, TRIGHT> = ILeft<TLEFT> | IRight<TRIGHT>;

Keywords

FAQs

Package last updated on 30 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc