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

@typed-f/either

v0.3.8

Published

[![NPM Version][either-npm-version-badge]][either-npm] [repo-circleci-badge]: https://img.shields.io/circleci/project/github/Ailrun/typed-f/master.svg?logo=circleci [![Known Vulnerabilities][either-snyk-badge]][either-snyk] [![Supported TypeScript Version

Downloads

40

Readme

@typed-f/either

NPM Version repo-circleci-badge: https://img.shields.io/circleci/project/github/Ailrun/typed-f/master.svg?logo=circleci Known Vulnerabilities Supported TypeScript Version PRs Welcome

Watch on GitHub Star on GitHub

Disjoint union type for Typed-F

Installation

# for NPM>=5
npm install @typed-f/either
# or
npm i @typed-f/either
# for NPM<5
npm install --save @typed-f/either
# or
npm i -S @typed-f/either

Why do we need Either (Disjoint union type)?

See this document.

APIs

This package includes type definition for Either type, which is an union type of Right and Left. This package also includes their methods and some utility functions for them.

Types

  • Either<L, R>
  • Right<L, R>
  • Left<L, R>

Methods only for Either

  • isLeft(this: Either<L, R>): this is Left<L, R>
    Checks whether this is Left or not. You can use this with if statement like following.
    declare const e: Either<string, number>;
      
    if (e.isLeft()) {
      // Now `e` is `Left<string, number>`
      e.value.slice(0, 1);
    }
  • isRight(this: Either<L, R>): this is Right<L, R>
    Counterpart of isLeft. You can use this with if statement too.
  • leftOr(this: Either<L, R>, def: L): L
    If this is Left, this function returns the inner value of this. If not, this returns def (default value) you passed.
  • leftOrThrow(this: Either<L, R>, err: Error): L
    If this is Left, this function returns the inner value of this. If not, this throws err you passed.
  • leftOrCompute(this: Either<L, R>, f: Fun<[R], L>): L
    If this is Left, this function returns the inner value of this. If not, this invoke f with inner value of current this, and return the result.
  • rightOr(this: Either<L, R>, def: R): R
    Counterpart of leftOr
  • rightOrThrow(this: Either<L, R>, err: Error): R
    Counterpart of leftOrThrow
  • rightOrCompute(this: Either<L, R>, f: Fun<[L], R>): R
    Counterpart of leftOrCompute

Implemented Typeclasses

Most of typeclass implementation of Either considers Right as a valid value container, and Left as an error value container.

  • Monad
    • bind<U>(this: Either<L, R>, f: Fun<[R], Either<L, U>>): Either<L, U> If this is Right, applies f to its inner value and return the result. If this is Left, returns this.
  • Applicative
    • unit<U>(v: U): Either<any, U> Return Right of v.
    • ap<U>(this: Either<L, R>, f: Either<L, Fun<[R], U>>): Either<L, U> If this is Right and f is Right, applies the inner value of f to the inner value of this and returns Right of the result. If this is Right but f is Left, returns f. If this is Left, returns this.
  • Functor
    • map<U>(this: Either<L, R>, f: Fun<[R], U>): Either<L, U> If this is Right, applies f to the inner value of this and returns Right of the result. If this is Left, returns this.
  • Matchable
    • matchWith<U>(this: Either<L, R>, cases: EitherPatterns<L, R, U>): U If this is Right, applies cases.right to the inner value of this and returns the result. If this is Left, applies cases.left to the inner value of this and returns the result.
  • Setoid
    • equals(other: Either<any, any>): boolean If this equals to other, return true. If this does not equal to other, return false. When both this and other have complex inner values (object), this function tries equals method of inner value of this. In other words, this.equals(other) === this.value.equals(other.value)
    • notEquals(other: Either<any, any>): boolean Returns true if and only if this.equals(other) returns false.

Utility Functions

You can use these functions like Either.<function name>, for example, in case of map, you can access it with Either.map.

  • unit<R>(value: R): Either<any, R> Returns Right of value.
  • of Alias of unit
  • sequenceObject<L, O extends object>(obj: { [K in keyof O]: Either<L, O[K]> }): Either<L, O> Takes an obj of Eithers and returns an Either of object. If for all keys of obj, correspoding values are Right, then this will return a Right of object whose keys are original keys and correspoding values are inner value of original values (Rights). If for one or more keys of obj, values are Left, then this will return first encounted Left value. (Be careful, if there are more than one Left values in obj, returned value can be changed by JS engine codes run with)
  • sequenceArray<L, R>(array: Either<L, R>[]): Either<L, R[]> Takes an array of Eithers and returns an Either of array. If all entries of array are Right, this will return a Right of array whose entries are inner values of original entries. Corresponding entries will have same indices. If one or more entries of array are Left, this will return a leftmost Left entry (whose index is minimum).
  • map<L, R, U>(f: Fun<[R], U>): Fun<[Either<L, R>], Either<L, U>> Returns a function that takes Either<L, R> and maps its right inner value using f. map(f)(a) is same with a.map(f).