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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## [1.2.0]

- Added new utility types to `Option` and `Result` types
- Option
- inspect(callback) -- Calls the provided `callback` if called on
a `Some` variant
- Result
- inspect(callback) -- Calls the provided `callback` if called on
a `Failure` variant
- expect(message) -- Unwraps the inner value if variant is `Success`,
otherwise throws an `Error` with the provided `message`
- Update test structure to be more readable and easier to add tests

## [1.1.0]

- Added new unsafe functions to the `Option` type to return unchecked values.
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.1.0",
"version": "1.2.0",
"description": "A collection of helpful TypeScript utilities with the aim of having limited production dependencies.",
"main": "dist/index.js",
"files": [
Expand Down
10 changes: 10 additions & 0 deletions src/option/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export interface OptionUtils<T> {
* @returns The value of the `Option<T>` if it is a `Some<T>`.
*/
expect(message: string): T;
/**
* Peek into the inner value of this Option<T>, and if `isSome()` then the callback function will be run.
* @param callback the callback to run if this Option `isSome`
* @returns this `Option<T>`
*/
inspect(callback: (value: T) => void): Option<T>;
/**
* Converts this `Option<T>` type into a `Result<T, E>` type. The `Result<T, E>` will be an `Err<E>` if the `Option<T>` is a `None`.
* @param error The error to return if the `Option<T>` is a `None`.
Expand Down Expand Up @@ -146,6 +152,10 @@ function buildOption<T>(innerType: Some<T> | None): Option<T> {
if (this.isSome()) return this.value;
throw new OptionIsEmptyError(message);
},
inspect(callback) {
if (this.isSome()) callback(this.value);
return this;
},
okOr(error) {
if (this.isNone()) {
if (error instanceof Error) return result.err(error);
Expand Down
Loading