-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e429ec
commit 8ced6fd
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { type Err, type Ok, type Result, ResultType } from "../spec.ts"; | ||
|
||
/** Returns `true` if the {@linkcode result} is a {@linkcode Ok}. | ||
* | ||
* @example | ||
* ```ts | ||
* import { isOk } from "https://deno.land/x/result_js/operators/query.ts"; | ||
* import { Ok, type Result } from "https://deno.land/x/result_js/spec.ts"; | ||
* import { assert } from "https://deno.land/std/testing/asserts.ts"; | ||
* | ||
* const result: Result<unknown, unknown> = Ok(1); | ||
* assert(isOk(result)); | ||
* ``` | ||
*/ | ||
export function isOk<T>(result: Result<T, unknown>): result is Ok<T> { | ||
return result.type === ResultType.Ok; | ||
} | ||
|
||
/** Returns `true` if the {@linkcode result} is a {@linkcode Err}. | ||
* | ||
* @example | ||
* ```ts | ||
* import { isErr } from "https://deno.land/x/result_js/operators/query.ts"; | ||
* import { Err, type Result } from "https://deno.land/x/result_js/spec.ts"; | ||
* import { assert } from "https://deno.land/std/testing/asserts.ts"; | ||
* | ||
* const result: Result<unknown, unknown> = Err(0); | ||
* assert(isErr(result)); | ||
* ``` | ||
*/ | ||
export function isErr<E>(result: Result<unknown, E>): result is Err<E> { | ||
return result.type === ResultType.Err; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license. | ||
|
||
import { isErr, isOk } from "./query.ts"; | ||
import { Err, Ok } from "../spec.ts"; | ||
import { assert, assertFalse, describe, it } from "../_dev_deps.ts"; | ||
|
||
describe("isOk", () => { | ||
it("should return true if it is Ok", () => { | ||
assert(isOk(Ok(0))); | ||
}); | ||
|
||
it("should return false if it is Err", () => { | ||
assertFalse(isOk(Err(0))); | ||
}); | ||
}); | ||
|
||
describe("isErr", () => { | ||
it("should return true if it is Err", () => { | ||
assert(isErr(Err(0))); | ||
}); | ||
|
||
it("should return false if it is Ok", () => { | ||
assertFalse(isErr(Ok(0))); | ||
}); | ||
}); |