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

@standardize/result

v0.0.1

Published

AKA _Either_ _Left/Right_ _Operation_ _Result(Kotlin)_

Downloads

2

Readme

@standardize/result

AKA Either Left/Right Operation Result(Kotlin)

Why?

Exceptions are commonplace in most codebases, this class allows "dangerous" operations to be wrapped in a Result<T, E> which encapsulate the possible errors or success states from a given method.

Typically the only time knowledge of an external package and its errors will be encoded is when the service is first written. By defining typed exceptions you encode information of possibly handleable errors you may otherwise feed directly to your users.

The async version of this monad AsyncResult<T, E> is best described as a promise with typed errors. The interface of the sync and async versions match to simplify access and allow method chaining.

Result<T, E> is not required in all of your code and should not be overused. It is best used at boundaries between services where retries or API changes are solid and infrequent.

// Async
await Result
  .fromPromise<User, NotFoundError | PGError>(getUser(userId))
  .map(setUser, logError);

// Sync
Result
  .fromThrowable<User, NotFoundError | PGError>(() => getUserSync(userId))
  .map(setUser, logError);

Public Interface

Instantiation Methods

fromThrowable

given a function that when invoked may throw, wraps the caught error as a Fail<E> and a successful completion as a Success<T>

Result.fromThrowable<User, NotFoundError>(getUserSync) // Result<User, NotFoundError>

fromPromise

Given a promise that rejects, will return a Fail<E> or if it resolves a Success<T>

await Result.fromPromise<User, NotFoundError>(getUser) // Result<User, NotFoundError>

success

Instantiates a Success<T> result

Result.success('Value')

fail

Instantiates a Fail<E> result

Result.fail(new Error(''))

Transformation Methods

map / mapFailure

Apply the passed function to the wrapped successful or unsuccessful value and returns a new Result<T, E> of the result.

await Result.fromPromise(getUser).map(setUser, onError) // Result<void, void>

await Result.fromPromise(getUser).map(toViewUser).map(setUser) // Result<void, E>

swap

Swaps the types <T> and <E>. If a Result is a Success<T> it will return a Fail<T> and vice versa.

Extraction Methods

getEither

Returns T | E given Result<T, E>. Useful to merge multiple promises.

await Result
  .fromPromise(sendEmail)
  .map(confirmEmailDelivery, rescheduleDelivery)
  .getEither()

getOrThrowFailure

Returns T or throws E given Result<T, E>. This essentially reverses the value having been wrapped in a result as an AsyncResult will be rejected and a Result will throw