-
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(logical): add or operator for option
- Loading branch information
1 parent
fee4860
commit a8b61cf
Showing
2 changed files
with
45 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,25 @@ | ||
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { isSome } from "./query.ts"; | ||
import { type Option } from "../spec.ts"; | ||
|
||
/** Returns the {@link option} if it contains a value, otherwise returns {@link obtb}. | ||
* | ||
* @example | ||
* ```ts | ||
* import { None, or, Some } from "https://deno.land/x/optio/mod.ts"; | ||
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; | ||
* | ||
* const x = Some.of(2); | ||
* assertEquals(or(x, None), Some.of(2)); | ||
* | ||
* const y = None; | ||
* assertEquals(or(y, Some.of(100)), Some.of(100)); | ||
* ``` | ||
*/ | ||
export function or<T>(option: Option<T>, obtb: Option<T>): Option<T> { | ||
if (isSome(option)) return option; | ||
|
||
return obtb; | ||
} |
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,20 @@ | ||
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { or } from "./logical.ts"; | ||
import { None, Some } from "../spec.ts"; | ||
import { assert, describe, it } from "../_dev_deps.ts"; | ||
|
||
describe("or", () => { | ||
it("should return option if it is Some", () => { | ||
const option = Some.of(0); | ||
assert(or(option, Some.of(0)) === option); | ||
assert(or(option, None) === option); | ||
}); | ||
|
||
it("should return optb if it is None", () => { | ||
const optb = Some.of(0); | ||
assert(or(None, optb) === optb); | ||
assert(or(None, None) === None); | ||
}); | ||
}); |