rustly
v0.0.4
Published
lightweight and versatile utility that brings the power and convenience of Rust's Option and Result types to JavaScript and TypeScript. Inspired by Rust's functional error-handling and optional value handling patterns, his package offers a clean and expre
Downloads
3
Maintainers
Readme
Introduction
rustly
is a TypeScript library that brings the power of Rust's Option
and Result
types to the TypeScript ecosystem. This package provides robust error handling and nullable value representation, allowing you to write more reliable and expressive code. The Option
type deals with nullable values, while the Result
type handles potential errors.
Features
- Rust-like
Option
andResult
types for TypeScript - Safe and expressive error handling
- Familiar Rust-inspired API
- Easily manage nullable values and errors in a type-safe manner
Installation
You can install rustly
using npm:
npm install --save rustly
Usage
Result Usage
import { Ok, Err, Result } from 'rustly';
const success = Ok(1);
const failure = Err('not a number');
success.ok(); // Option(1)
success.isOk(); // true
success.isOkAnd(val => val === 1); // true
success.err(); // None
success.isErr(); // false
success.isErrAnd(val => val === 1); // false
success.unwrap(); // 1
success.unwrapOr('not a number'); // 1
success.unwrapErr(); // throws
success.unwrapErrOr(42); // 42
success.expect('not a number'); // 1
success.expectErr('not a number'); // throws
const successPlusOneOrZeroIfErr = success.match({
ok(val) {
// If it is Ok return the value + 1
return val + 1;
},
err() {
// Return 0 if there was an error
return 0;
}
})
success.map(val => val + 1); // Ok(2)
success.mapErr(val => val + 1); // Ok(1)
success.and(Ok(2)); // Ok(2)
success.and(Err('not a number')); // Err('not a number')
// Merge array of Result into a single Result
const arrayOfResult = [Ok(1), Ok(2), Ok(3)], Err('not a number')];
Result.merge(arrayOfResult); // Ok([1, 2, 3])
Result.mergeErr(arrayOfResult); // Err(['not a number'])
// Nested Result
const nestedResult = Ok(Ok(1));
nestedResult.flatten(); // Ok(1)
success.map(val => Err()).flatten(); // Err()
Option usage
import { Some, None, Option } from 'rustly';
const some = Some(1);
const none = None();
some.isSome(); // true
some.isSomeAnd(val => val === 1); // true
some.isNone(); // false
some.isNoneAnd(val => val === 1); // false
some.unwrap(); // 1
some.unwrapOr('not a number'); // 1
some.expect('not a number'); // 1
const somePlusOneOrZeroIfNone = some.match({
some(val) {
// If it is Some return the value + 1
return val + 1;
},
none() {
// Return 0 if it is None
return 0;
}
})
none.insert(5); // Some(5)
const taken = some.take(); // Some(1) and some is now None
some.replace(2); // Some(2)
some.map(val => val + 1); // Some(2)
some.mapOr(42, val => val + 1); // Some(2)
none.mapOr(42, val => val + 1); // 42
some.okOr('not a number'); // Ok(1)
none.okOr('not a number'); // Err('not a number')
some.or(Some(2)); // Some(1)
none.or(Some(2)); // Some(2)
some.and(Some(2)); // Some(2)
none.and(Some(2)); // None
Promise override
Promise.resolveFromOk(1)
.thenOnOk(okvalue => console.log(okvalue))
.thenOnErr(errvalue => console.log(errvalue));
Promise.resolveFromErr(1)
.thenOnOk(okvalue => console.log(okvalue))
.thenOnErr(errvalue => console.log(errvalue));
Contributing
Contributions are welcome! If you find any issues or have new features to add, please feel free to open an issue or submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.