Skip to content

Commit

Permalink
feat(query): add query operators
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jul 6, 2023
1 parent 4e429ec commit 8ced6fd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
36 changes: 36 additions & 0 deletions operators/query.ts
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;
}
25 changes: 25 additions & 0 deletions operators/query_test.ts
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)));
});
});

0 comments on commit 8ced6fd

Please sign in to comment.