https://github.com/constraintautomaton/result-interface-ts

A tiny utility to standardize how to handle results that may succeed or fail inspired by Go-style error handling.

https://github.com/constraintautomaton/result-interface-ts

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (8.3%) to scientific vocabulary

Keywords

type-safety typescript typing
Last synced: 5 months ago · JSON representation

Repository

A tiny utility to standardize how to handle results that may succeed or fail inspired by Go-style error handling.

Basic Info
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
type-safety typescript typing
Created 11 months ago · Last pushed 7 months ago
Metadata Files
Readme License

README.md

result-interface

npm version Unit Tests Status

A tiny utility (mainly interfaces) with zero dependencies to standardize handling results that may succeed or fail, inspired by Go-style error handling.

Installation

bash npm i result-interface

Usage

You can define functions more declaratively when working with possible failures:

```ts import { type Result, isError } from "result-interface";

let VALUE: number | undefined = undefined;

function getValue(): Result { if (VALUE !== undefined) { return { value: VALUE }; } return { error: "The value is undefined" }; }

const resp: Result = getValue();

if (isError(resp)) { console.log(Unable to get the value. Reason: ${resp.error}); process.exit(1); }

console.log(The value multiplied by two is ${resp.value * 2}); ```

You can use helper functions to generate IError and IResult types (the possible types of Result).

```ts import { type Result, isError, result, error } from "result-interface";

function getValue(): Result { if (VALUE !== undefined) { return result(VALUE); } return error("The value is undefined"); } ```

You can specify that a Promise will not throw:

```ts import { isError, SafePromise, type Result } from "result-interface";

let VALUE: number | undefined = undefined;

async function getValueLater(): SafePromise { return new Promise((resolve, reject) => { if (VALUE !== undefined) { resolve({ value:VALUE }) } else { reject("The value is undefined"); } }); }

// Creates a promise that always resolves with a Result. // On failure, it resolves with an error instead of rejecting or throwing. const resp: Result = await getValueLater();

if (isError(resp)) { console.log(Unable to get the value. Reason: ${resp.error}); process.exit(1); }

console.log(The value multiplied by two is ${resp.value * 2}); ```

You can specify that a promise will never fail, thus that it will always be an IResult: ```ts import { SafePromise, type Result } from "./src/index";

let VALUE: number | undefined = undefined;

async function getValueLater(): SafePromise { return new Promise((resolve, reject) => { if (VALUE !== undefined) { resolve({ value: VALUE }) } else { reject("The value is undefined"); } }); }

// Creates a promise that always resolves with a Result. // On failure, it resolves with an error instead of rejecting or throwing. const resp: Result = await getValueLater();

console.log(The value multiplied by two is ${resp.value * 2}); `` You can ensure that aPromise` will not throw:

```ts import { isError, safePromise, type Result } from "result-interface";

let VALUE: number | undefined = undefined;

async function getValueLaterUnsafe(): Promise { return new Promise((resolve, reject) => { if (VALUE !== undefined) { resolve(VALUE) } else { reject("The value is undefined"); } }); }

// Creates a promise that always resolves with a Result. // On failure, it resolves with an error instead of rejecting or throwing. const resp: Result = await safePromise(getValueLaterUnsafe());

if (isError(resp)) { console.log(Unable to get the value. Reason: ${resp.error}); process.exit(1); }

console.log(The value multiplied by two is ${resp.value * 2}); ```

Test

bash bun test

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Owner

  • Name: Bryan-Elliott Tam
  • Login: constraintAutomaton
  • Kind: user
  • Location: Ghent, Belgium
  • Company: imec - Ghent University - IDLab

PhD Student working on querying for semantic web technologies

GitHub Events

Total
  • Push event: 35
  • Create event: 2
Last Year
  • Push event: 35
  • Create event: 2

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • npm 629 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 20
  • Total maintainers: 1
npmjs.org: result-interface

A tiny utility (mainly interfaces) with zero dependencies to standardize handling results that may succeed or fail, inspired by Go-style error handling.

  • Versions: 20
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 629 Last month
Rankings
Dependent repos count: 24.9%
Average: 30.4%
Dependent packages count: 35.9%
Maintainers (1)
Last synced: 6 months ago