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

@luizcgr/either-ts

v1.0.11

Published

Either-ts is a light typescript library created to help developers to use main set of functional programming patterns. Borned to be _SIMPLE!_

Downloads

11

Readme

either-ts

Either-ts is a light typescript library created to help developers to use main set of functional programming patterns. Borned to be SIMPLE!

Table of contents

  1. Installation
  2. Documentation

Installation

npm install @luizcgr/either-ts

or

yarn add @luizcgr/either-ts

Documentation

Either object

isRight

True if the object represents the right part of Either object.

const eitherResult = doAnything()
if (eitherResult.isRight()) {
  // do something
}

isLeft

True if the object represents the left part of Either object.

const eitherResult = doAnything()
if (eitherResult.isLeft()) {
  // do something
}

fold

Folds the Either object in two parts: left and right.

const eitherResult = doAnything() // Left - SomeError | Right - string
return eitherResult.fold(
  (err) => console.log(err.message), // err is an instance of SomeError class indicated in left either part
  (doc) => console.log(doc), // doc is an instance of string class indicated in right either part
)

Is possible indicate the fold return using generics notation.

/**
 * If the query result is right and returns a number greater than 10, the function must return true.
 * If the query result is left, the function must return false.
 */
function exists(): boolean {
  const eitherResult = countAnything() // Left - SomeError | Right - number
  eitherResult.fold<boolean>(
    (err) => false,
    (count) => count > 10,
  )
}

getRight

Returns the right part of Either object if the object represets right result.

const eitherResult = doAnything() // Left - SomeError | Right - number
const result = eitherResult.getRight() // Extracts the right value
console.log(result) // number value

getLeft

Returns the left part of Either object if the object represets left result.

const eitherResult = doAnything() // Left - SomeError | Right - number
const result = eitherResult.getRight() // Extracts the right value
console.log(result) // SomeError object

right and left functions

Creates new instance of Either with a right or left value.

function doAnything(number value): Either<Error, boolean> {
  if (value > 10) {
    return right(value)
  }
  return left(Error('Incorrect value'))
}

EitherP object

Encapsulates a promise of Either. See the complete example below. The main goal is simplify the function declaration reducing the code verbosity.

SearchUserByLogin class makes a direct call to users repository. Instead of re-throw the error, the function returns the Either (left ou right) object that will be analyzed by the caller.

export class SearchUserByLogin {
  constructor(private _userRepository: UserRepository) {}

  async find(login: string): EitherP<UserError, User> {
    // EitherP<UserError, User> === Promise<Either<UserError, User>>
    try {
      const eitherResult = await this._userRepository.findByLogin(login)
      return eitherResult.fold<EitherP<UserError, User>>(
        async (err) => left(new UserError('User not found')),
        async (user) => right(user),
      )
    } catch (err) {
      return left(new UserError('Connection error'))
    }
  }
}

The caller function will treat the result.

const searchUserByLogin = ... // inject an SearchIserByLogin instance
const eitherResult = await searchUserByLogin.find('logan')
eitherResult.fold(
  err => res.status(400).json({error: err.message}),
  user => res.status(200).json(user)
)