-
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.
feat(query): add checking option type functions
- Loading branch information
1 parent
421d050
commit 4fee41c
Showing
2 changed files
with
59 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,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; | ||
} |
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 { 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))); | ||
}); | ||
}); |