Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [1.1.0]

- Added new unsafe functions to the `Option` type to return unchecked values.
- unsafeUnwrap() -- Returns the inner value regardless on if it is `undefined`
or `null`
- unsafeUnwrapOr() -- Maps the inner value to an optional default value
without checking the default value for `null | undefined`

## [1.0.0]

- Removed multiple named exports from the base library in favor of
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dbidwell94/ts-utils",
"version": "1.0.0",
"version": "1.1.0",
"description": "A collection of helpful TypeScript utilities with the aim of having limited production dependencies.",
"main": "dist/index.js",
"files": [
Expand Down
20 changes: 20 additions & 0 deletions src/option/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,25 @@ export interface OptionUtils<T> {
* @returns The value of the `Option<T>`.
*/
unwrap(): T;
/**
* Extracts the base value from this type, not checking if the value is `None`.
* @returns The raw unchecked inner value of the `Option<T>`
*/
unsafeUnwrap(): T | null | undefined;
/**
* Extracts the base value from this type. If there is no value in the `Option<T>`, the function will return the provided default value.
* @param defaultValue The value to return if the `Option<T>` is a `None`.
* @throws {OptionIsEmptyError} if the default value is `null` or `undefined`.
* @returns The value of the `Option<T>` if it is a `Some<T>`, otherwise the default value.
*/
unwrapOr(defaultValue: T): T;
/**
* Extracts the base value from this type. If there is no value in the `Option<T>`, the function will return whatever you provided as the
* default value, even if that value was not provided (undefined).
* @param defaultValue The value to return if the `Option<T>` is a `None`
* @returns The possibly undefined | null value of the `Option<T>`.
*/
unsafeUnwrapOr(defaultValue?: T | null): T | null | undefined;
/**
* Extracts the base value from this type. The function throws an error with the provided message if there is no value provided.
* @throws {OptionIsEmptyError} if the default value is `null` or `undefined`.
Expand Down Expand Up @@ -114,6 +126,10 @@ function buildOption<T>(innerType: Some<T> | None): Option<T> {
if (this.isNone()) throw new OptionIsEmptyError();
return this.value;
},
unsafeUnwrap() {
if (this.isNone()) return undefined;
return this.value;
},
unwrapOr(defaultValue) {
if (this.isSome()) return this.value;
if (defaultValue === null || defaultValue === undefined)
Expand All @@ -122,6 +138,10 @@ function buildOption<T>(innerType: Some<T> | None): Option<T> {
);
return defaultValue;
},
unsafeUnwrapOr(defaultValue) {
if (this.isNone()) return defaultValue;
return this.value;
},
expect(message) {
if (this.isSome()) return this.value;
throw new OptionIsEmptyError(message);
Expand Down
24 changes: 24 additions & 0 deletions src/option/option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,28 @@ describe("src/utility/option.ts", () => {
])("Properly checks the return type of %s", (_, optVal, validator) => {
expect(validator(optVal)).toBeTruthy();
});

it("Returns `undefined` if unsafeUnwrap() was called on a None type", () => {
const opt = option.unknown(undefined);

expect(opt.unsafeUnwrap()).toEqual(undefined);
});

it("Returns the inner value of Some<T> if unsafeUnwrap was called on a Some type", () => {
const opt = option.some(123);

expect(opt.unsafeUnwrap()).toEqual(123);
});

it("returns the raw default value if unsafeUnwrapOr() was called on a None type", () => {
const opt = option.unknown(undefined);

expect(opt.unsafeUnwrapOr(null)).toEqual(null);
});

it("Returns the inner value of Some<T> if unsafeUnwrapOr() was called on a Some<T> type", () => {
const opt = option.some(123);

expect(opt.unsafeUnwrapOr(null)).toEqual(123);
});
});