Skip to content

Commit

Permalink
feat(query): add checking option type functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jul 4, 2023
1 parent 421d050 commit 4fee41c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
34 changes: 34 additions & 0 deletions operators/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { type None, type Option, OptionType, type Some } from "../spec.ts";

/** Returns `true` if the {@link option} is a {@link Some}.
*
* @example
* ```ts
* import { isSome, Option, Some } from "https://deno.land/x/optio/mod.ts";
* import { assert } from "https://deno.land/std/testing/asserts.ts";
*
* const option: Option<number> = Some.of(2);
* assert(isSome(option));
* ```
*/
export function isSome<T>(option: Readonly<Option<T>>): option is Some<T> {
return option.type === OptionType.Some;
}

/** Returns `true` if the {@link option} is a {@link None}.
*
* @example
* ```ts
* import { isNone, None, Option } from "https://deno.land/x/optio/mod.ts";
* import { assert } from "https://deno.land/std/testing/asserts.ts";
*
* const option: Option<unknown> = None;
* assert(isNone(option));
* ```
*/
export function isNone(option: Readonly<Option<unknown>>): option is None {
return option.type === OptionType.None;
}
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 { isNone, isSome } from "./query.ts";
import { None, Some } from "../spec.ts";
import { assert, assertFalse, describe, it } from "../_dev_deps.ts";

describe("isSome", () => {
it("should return true if it is Some", () => {
assert(isSome(Some.of(0)));
});

it("should return false if it is None", () => {
assertFalse(isSome(None));
});
});

describe("isNone", () => {
it("should return true if it is None", () => {
assert(isNone(None));
});

it("should return false if it is Some", () => {
assertFalse(isNone(Some.of(0)));
});
});

0 comments on commit 4fee41c

Please sign in to comment.