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

either-ts

v1.0.27

Published

Zero-depedency implementation of 'Either' abstract datatype, for typescript and javascript

Downloads

5

Readme

either-ts

A minimalist, zero dependency implementation of an 'Either' or 'Result' type. Inspired by similar types in Ocaml, Haskell, Scala, F#, Rust etc.

This is written in Javascript, but can be used in Typescript, or in js files that are being type-checked by tsc

What's an Either type?

Either, aka Result, is a pattern for methods and functions that can return 'either' an error of type L or a normal result of type R. It's an alternative to error codes, or even using exceptions. The advantage is that it doesn't interrupt your control flow like exceptions and it doesn't let you ignore it like an error code.

Why have you made this and not used other EIther libraries floating around?

Because this one is smaller, and just does one thing. It's not an attempt to reconstruct Haskell in Javascript - I am just taking one useful pattern I was productive with in other languages.

If there's a either implementation smaller than this, let me know!

Installation

yarn add either-ts
npm install either-ts

Usage

import * as either from 'either-ts'

API

Creating an Either

left

L -> Either<L, R>

right

R -> Either<L, R>

create

boolean -> (() -> L) -> (() -> R) -> Either<L, R>

If false, will create an either with the result of the thunk that produces L. If true, it will use the thunk that produces R.

fromThrowable

fromNullable

Using an Either

Mapping

Transforms one side, while leaving the other side intact

.map

Either<L, R> -> (R -> R2) -> Either<L, R2>

.leftMap

Either<L, R> -> (L -> L2) -> Either<L2, R>

Mapping and promisifying

Transforms an Either with a function that returns a promise, and promisifies the entire Either. This can be more convenient than having one side wrapped in a promise and one side not.

.mapThen

Either<L, R> -> (R -> Promise<R2>) -> Promise<Either<L, R2>>

.leftMapThen

Either<L, R> -> (L -> Promise<L2>) -> Promise<Either<L2, R>>

Flat-Mapping

Like mapping, but using a function that returns an Either. This is useful for flattening out and chaining Either producing functions.

.flatMap

Either<L, R> -> (R -> Either<L, R2>) -> Either<L, R2>

.leftFlatMap

Either<L, R> -> (L -> Either<L2, R>) -> Either<L2, R>

Flat-Mapping then Promisifying

.flatMapThen

Either<L, R> -> (R -> Promise<Either<L, R2>>) -> Promise<Either<L, R2>>

.leftFlatMapThen

Either<L, R> -> (L -> Promise<Either<L2, R>>) -> Promise<Either<L2, R>>

Un-Wrapping

.rightOrElse

Either<L, R> -> (L -> R) -> R

.value

Either<L, R> -> L | R

Gets the raw value of an either. Useful for when you have used map and or leftMap to make the types the same.

.isOk

Either<L, R> -> boolean

Returns true if it's right. If you're using this, you're using the either data type wrong.

Lists of Eithers

These are free-standing functions, not methods of the Either object.

either.partition

[Either<L, R>] -> [[L], [R]]

Returns a tuple of all the left values and all the right values in the list, in order.

either.sequence

[Either<L, R>] -> Either<L, R[]?

Returns the first left value in the list. Otherwise returns all the right values flattened into a list. Useful for parsing.