Skip to content

Commit

Permalink
feat: add examples for isOk
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Mar 23, 2024
1 parent 57a6564 commit 854e57a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Rust-like `Result` for JavaScript.
- [Installation](#installation)
- [Usage](#usage)
- [About Rust `Option`](#about-rust-option)
- [Rust `Result` Methods](#rust-result-methods)
- [Additional Methods](#additional-methods)
- [equal](#equal)
- [Methods Documentation](#methods-documentation)
- [Rust `Result` Methods](#rust-result-methods)
- [isOk](#isok)
- [Additional Methods](#additional-methods)
- [equal](#equal)
- [Helpers for Resultifying](#helpers-for-resultifying)
- [resultify](#resultify)
- [resultify.sync](#resultifysync)
Expand Down Expand Up @@ -65,7 +67,8 @@ This package doesn't implement Rust-like `Option`. Handling `undefined`/`null` i
[proposal-optional-chaining]: https://github.com/tc39/proposal-optional-chaining
[proposal-nullish-coalescing]: https://github.com/tc39/proposal-nullish-coalescing

## Rust `Result` Methods
## Methods Documentation
### Rust `Result` Methods

The Rust-like `Result` implements the following methods:

Expand Down Expand Up @@ -149,6 +152,21 @@ hash
[or_else]: https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else
[transpose]: https://doc.rust-lang.org/std/result/enum.Result.html#method.transpose

#### `isOk`

Returns `true` if the result is `Ok`.

Examples:

```ts
const x: Result<number, string> = Ok(2);
console.assert(x.isOk() === true);

const y: Result<number, string> = Err('Some error message');
console.assert(y.isOk() === false);
```

<!--
Some of the methods have asynchronous versions to help you handle asynchronous logic, for example:
```ts
const result = await Ok(1)
Expand All @@ -158,9 +176,10 @@ const result = await Ok(1)
.then((result) => result.andThenAsync(asyncFn4))
.then((result) => result.andThenAsync(asyncFn5));
```
-->

## Additional Methods
### equal
### Additional Methods
#### equal

You can not just use `===` or `==` to compare `Result`, so `Result` itself provides an method call `equal` for that.

Expand Down
10 changes: 10 additions & 0 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export class RustlikeResult<T, E> implements Result<T, E> {
* Returns `true` if the result is `Ok`.
*
* ref: https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok
*
* Examples:
*
* ```
* const x: Result<number, string> = Ok(2);
* console.assert(x.isOk() === true);
*
* const y: Result<number, string> = Err('Some error message');
* console.assert(y.isOk() === false);
* ```
*/
isOk(): boolean {
return this._type === 'ok';
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export interface Result<T, E> {
* Returns `true` if the result is `Ok`.
*
* ref: https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok
*
* Examples:
*
* ```
* const x: Result<number, string> = Ok(2);
* console.assert(x.isOk() === true);
*
* const y: Result<number, string> = Err('Some error message');
* console.assert(y.isOk() === false);
* ```
*/
isOk(): boolean;

Expand Down

0 comments on commit 854e57a

Please sign in to comment.