npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

my-only-either

v1.3.0

Published

Only support either interface

Downloads

34,830

Readme

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.

| name | category | description | | :- | :-: | :- | | ILeft | type | Left interface | | IRight | type | Right interface | | Either | type | Either type using ILeft and IRight | | TPickLeft | utility type | Return Type of left in Either | | TPickILeft | utility type | Return Type of ILeft in Either | | TPickRight | utility type | Return Type of right in Either | | TPickIRight | utility type | Return Type of IRight in Either | | left | function | value convert ILeft type | | right | function | value convert IRight type | | isLeft | function | check given value is ILeft type | | isRight | function | check given value is IRight type |

PassFailEither

Name using pass and fail.

| name | category | description | | :-: | :-: | :- | | IFail | type | Fail interface | | IPass | type | Pass interface | | PassFailEither | type | PassFailEither type using IFail and IPass | | TPickFail | utility type | Return Type of fail in Either | | TPickIFail | utility type | Return Type of IFail in Either | | TPickPass | utility type | Return Type of pass in Either | | TPickIPass | utility type | Return Type of IPass in Either | | fail | function | value convert IFail type | | pass | function | value convert IPass type | | efail | function | value convert IFail type, exactly same fail. If you use jest or test runner this function is helpful for auto import & auto complete | | epass | function | value convert IPass type, exactly same pass. If you use jest or test runner this function is helpful for auto import & auto complete | | isFail | function | check given value is IFail type | | isPass | function | check 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>;