Socket
Socket
Sign inDemoInstall

@openally/result

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @openally/result

Another inspired Rust's Result implementation.


Version published
Maintainers
1
Install size
14.3 kB
Created

Readme

Source

Result

[WIP] Another Rust's Result implementation (inspired by ts-results)

Requirements

  • Node.js v16 or higher

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @openally/result
# or
$ yarn add @openally/result

Usage example

import fs from "node:fs";
import { Result, Ok, Err } from "@openally/result";

function readFile(path: string): Result<string, "invalid path"> {
  if (existsSync(path)) {
    return Ok(fs.readFileSync(path, "utf8"));
  }

  return Err("invalid path");
}

const fileContentStr = readFile("test.txt").unwrap();
console.log(fileContentStr);

API

unwrap()

Unwrap value (throw if error).

Ok(1).unwrap(); // 1
Err("oops").unwrap(); // Error: Tried to unwrap Error: oops

unwrapOr(value)

Unwrap with a default value (if an error is detected).

Ok(1).unwrapOr(5); // 1
Err("oops").unwrapOr(5); // 5

map()

Map for an Ok value.

Ok(1)
  .map((v) => v + 1)
  .unwrap(); // 2

mapErr()

Map for an Error value.

andThen()

Ok(1)
  .andThen((value) => Ok(value + 1))
  .unwrap(); // 2

License

MIT

FAQs

Last updated on 05 Apr 2024

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc