Skip to content

Commit

Permalink
feat(logical): add or operator for option
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jul 4, 2023
1 parent fee4860 commit a8b61cf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions operators/logical.ts
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;
}
20 changes: 20 additions & 0 deletions operators/logical_test.ts
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);
});
});

0 comments on commit a8b61cf

Please sign in to comment.