Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@aurelle/result
Advanced tools
A Typescript error handling library inspired by Rust's Result type.
There is a lot of those already, but this one aims to be simple and doesn't attempt to turn Typescript into a functional programming language.
I originally wrote this because I find error handling in Typescript absolutely terrible. Wrapping everything in try/catch blocks really isn't my thing.
If you plan to use this, I recommend you simply copy src/index.ts
into your project instead of installing this package, given how small it is.
import { Result, Ok, Err } from '@aurelle/result';
const divide = (a: number, b: number): Result<number, Error> {
if (b === 0) {
return Err(new Error('Division by zero'));
}
return Ok(a / b);
}
const result = divide(10, 2);
if (result.err) {
// handle error
return ;
}
// work with result
console.log(result.val);
const [divided, divisionErr] = divide(10, 2).split();
if (divisionErr) {
// handle error
return ;
}
// work with result
console.log(divided);
// throws if there is an error
const divided = divide(10, 2).unwrap();
console.log(divided);
It is possible to turn any existing function into a function that returns a Result
by using the resultify
utility:
import { readFileSync } from 'fs';
import { resultify } from '@aurelle/result';
const rfs = resultify(readFileSync);
const file = rfs('file.txt', 'utf8'); // Result<string, Error>
Note that resultify
also works with async functions. It will return a function that returns a Promise<Result<T, E>>
.
this
context issuesIf you need to use a method that relies on the this
context, you should call bind
appropriately or wrap the call in a lambda function.
FAQs
Simple error handling library inspired by Rust's Result type
The npm package @aurelle/result receives a total of 2 weekly downloads. As such, @aurelle/result popularity was classified as not popular.
We found that @aurelle/result demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.