Skip to content

Commit

Permalink
feat(option): add option static unwrapOr
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Nov 13, 2024
1 parent 965346b commit fc08856
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ export class Option<T = any> {
: false;
}

/**
* @returns the `unwrapOr()` result if the value is an `Option`, otherwise the value itself.
*
* @param valueOrOption - A value of type `T` or an `Option<T>`
*/
public static unwrapOr<T>(valueOrOption: T | Option<T>): T | undefined {
return Option.isOption(valueOrOption)
? valueOrOption.unwrapOr()
: valueOrOption;
}

private readonly [SPECIES] = SPECIES_OPTION;

private readonly _value: T;
Expand Down Expand Up @@ -346,6 +357,8 @@ export class Option<T = any> {

/**
* @returns the contained `Some` value or `undefined` otherwise.
*
* Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `unwrapOrElse`, which is lazily evaluated.
*/
public unwrapOr(): T | undefined;
/**
Expand Down
2 changes: 2 additions & 0 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ export class Result<T = any, E = any> {

/**
* @returns the contained `Err` error or `undefined` otherwise.
*
* Arguments passed to `unwrapErrOr` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `unwrapErrOrElse`, which is lazily evaluated.
*/
public unwrapErrOr(): E | undefined;
/**
Expand Down
22 changes: 18 additions & 4 deletions test/option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { describe, it, expect, vi } from "vitest";
import { Option, Some, None, Err, Ok } from "../src";

describe("Option", () => {
describe("Some", () => {
describe("Option.Some", () => {
it("creates a Some with a value", () => {
const some = Some("hello");
expect(some.isSome()).toBe(true);
expect(some.unwrap()).toBe("hello");
});
});

describe("None", () => {
describe("Option.None", () => {
it("creates a None", () => {
expect(None.isNone()).toBe(true);
expect(() => None.unwrap()).toThrow();
});
});

describe("from", () => {
describe("Option.from", () => {
it("creates a Some for a truthy value", () => {
const some = Option.from("hello");
expect(some.isSome()).toBe(true);
Expand All @@ -42,7 +42,7 @@ describe("Option", () => {
});
});

describe("isSame", () => {
describe("Option.isSame", () => {
it("returns true if both are the same Some value", () => {
const result = Option.isSame(Some("a"), Some("a"));
expect(result).toBe(true);
Expand Down Expand Up @@ -73,6 +73,20 @@ describe("Option", () => {
});
});

describe("Option.unwrapOr", () => {
it("returns the value for a Some", () => {
expect(Option.unwrapOr(Some("hello"))).toBe("hello");
});

it("returns the value if the value if not an Option", () => {
expect(Option.unwrapOr("hello")).toBe("hello");
});

it("returns undefined for a None", () => {
expect(None.unwrapOr("world")).toBe("world");
});
});

describe("isSome", () => {
it("returns true for a Some", () => {
const some = Some("hello");
Expand Down

0 comments on commit fc08856

Please sign in to comment.