https://github.com/kyleect/rsresult
A library to work with serde_json serialized `Result` objects
https://github.com/kyleect/rsresult
Last synced: 12 months ago
JSON representation
A library to work with serde_json serialized `Result` objects
- Host: GitHub
- URL: https://github.com/kyleect/rsresult
- Owner: kyleect
- License: mit
- Created: 2024-11-28T17:52:23.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-18T04:16:39.000Z (over 1 year ago)
- Last Synced: 2025-02-05T06:34:16.854Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://kyleect.github.io/rsresult/
- Size: 165 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rsresult
[](https://github.com/kyleect/rsresult/actions/workflows/ci.yml)
A library to work with [serde_json](https://github.com/serde-rs/json) serialized `Result` objects. This library provides functions for creating, checking, and manipulating results, simplifying error handling in your JavaScript projects.
[API Documentation](https://kyleect.github.io/rsresult/)
## Installation

```bash
npm install rsresult
```
## Usage
```typescript
import assert from "node:assert";
import * as RsResult from "rsresult";
// Successful Result
const success: RsResult.Ok = RsResult.ok(123);
assert(RsResult.isOk(success)); // passes
assert(RsResult.isErr(success)); // fails
// Map successful results
const mappedSuccess = RsResult.map(success, (value) => value * 2);
assert(RsResult.unwrap(mappedSuccess) === 246);
try {
ResultRs.unwrapErr(success); // Throws an error
} catch (error) {
assert(error instanceof Error);
assert(error.message.includes("Unwrapping an ok result: 123"));
}
// Failed Result
const failed: RsResult.Err = RsResult.err("Error message");
// Map error results
const mappedError = RsResult.mapErr(failed, (value) => `Error: ${value}`);
assert(RsResult.unwrap(mappedError) === "Error: Error message");
assert(RsResult.isErr(failed)); // passes
assert(RsResult.isOk(failed)); // fails
try {
RsResult.unwrap(failed); // Throws an error
} catch (error) {
assert(error instanceof Error);
assert(error.message.includes("Unwrapping an error result: Error message"));
}
assert(Results.unwrapErr(failed) === "Error message");
//Example using ifOkOr
RsResult.ifOk(success, (value) => console.log("Success:", value));
RsResult.ifOkOr(
failed,
(value) => console.log("Success:", value),
(error) => console.error("Error:", error)
);
```
## License
MIT