Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hazae41/result

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hazae41/result

Just a result for JavaScript

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
248
decreased by-66.07%
Maintainers
1
Weekly downloads
 
Created
Source

Result

Just a result

npm i @hazae41/result

Node Package 📦

Features

Current features

  • 100% TypeScript and ESM
  • No external dependencies
  • Rust inspired
  • wrap()/unwrap()/rewrap() conversion (async/sync)
  • isOk()/isErr() type guards
  • map()/tryMap() type mapping (async/sync)
  • Unit-tested

Usage

Unwrapping

Use unwrap() to get the inner data if Ok or throw the inner error if Err

import { Result, Ok, Err } from "@hazae41/result"

function unwrapAndIncrement(result: Result<number, Error>): number {
  return result.unwrap() + 1
}

unwrapAndIncrement(Ok.new(0)) // will return 1
unwrapAndIncrement(Err.error("Error"))) // will throw Error("Error")

Safe mapping

You can easily map inner data if Ok and do nothing if Err, with support for async and sync

import { Result, Ok, Err } from "@hazae41/result"

function tryIncrement(result: Result<number, Error>): Result<number> {
  return result.tryMapSync(x => x + 1)
}

tryIncrement(Ok.new(0)) // Ok(1)
tryIncrement(Err.error("Error")) // Err(Error("Error"))

Type guards

You can easily check for Ok or Err and it's fully type safe

import { Result, Ok, Err } from "@hazae41/result"

function incrementOrFail(result: Result<number, Error>): number | Error {
  if (result.isOk())  // Ok<number>
    return result.inner + 1 // number
  else                // Err<Error>
    return new Error("Failed", { cause: result.inner }) 
}

Wrapping

You can easily wrap try-catch patterns, with support for async and sync

const result = Result.tryWrapSync(() => {
  if (something)
    return 12345
  else
    throw new Error("It failed")
})

Rewrapping

If another library implements its own Result type, as long as it has unwrap(), you can rewrap it to this library in one function

interface OtherResult<T> {
  unwrap(): T
}

function rewrapAndIncrement(other: OtherResult<number>): Result<number> {
  return Result.rewrap(other).tryMapSync(x => x + 1)
}

Keywords

FAQs

Package last updated on 22 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc