Skip to content

Commit

Permalink
Rename to maybe-result and add example to README
Browse files Browse the repository at this point in the history
Published as v0.1.0 and will add complementary Result class to live up to the new name.
  • Loading branch information
kernwig committed Feb 20, 2025
1 parent 69ba420 commit c880367
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 17 deletions.
96 changes: 89 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# maybe-ts - Safe handling of null and undefined in Typescript and Javascript
# maybe-result - Safe function return handling without null and undefined in Typescript and Javascript

## Introduction

Expand Down Expand Up @@ -33,6 +33,94 @@ In JavaScript we like to throw `Error` types, but in other languages we call the
Here's a nice introduction to the concept:
[Implementing a Maybe Pattern using a TypeScript Type Guard](https://medium.com/@sitapati/implementing-a-maybe-pattern-using-a-typescript-type-guard-81b55efc0af0)

## Example by story

You might have defined a data repository class (access to a data store) like this:

```ts
class WidgetRepository {
get(widgetID: string): Promise<Widget> {
// implementation ...
}
}
```

If the Widget isn't found, you throw a `NotFoundError`. All is well, until you start _expecting_
a Widget not to be found. That becomes valid flow, so you find yourself writing this a lot:

```ts
let widget: Widget | undefined;
try {
widget = await repo.get(widgetID);
}
catch (error) {
if (!(error instanceof NotFoundError)) {
throw error;
}
}

if (widget) { /* ... */ }
```

You may be willing to do that once... but not more. So you first try to change the repository:

```ts
class WidgetRepository {
get(widgetID: string): Promise<Widget | undefined> {
// implementation ...
}
}
```

Now it returns `undefined` instead of throwing. Oh, but what a hassle now you have to _check_ for
`undefined` _every time_ you call the function! So instead, you define _two_ functions:

```ts
class WidgetRepository {
getOrThrow(widgetID: string): Promise<Widget> {
// implementation ...
}
getIfFound(widgetID: string): Promise<Widget | undefined> {
// implementation ...
}
}
```

That makes it easier. It works. You just have to write _two_ functions every time you write a get function. 🙄

**OR...** use Maybe

```ts
class WidgetRepository {
get(widgetID: string): PromiseMaybe<Widget> {
// implementation ...
}
}

// One place elsewhere where you want to throw if not found
const widget = Maybe.unwrap(await get(widgetID));

// Another place elsewhere where you want to handle the mising lookup
const widget = Maybe.unwrapOrNull(await get(widgetID));
if (widget) {
// do work
} else {
// do other work
}

// Someplace where you have a default
const widget = (await get(widgetID)).unwrapOr(defaultWidget);
```

There are many other functions both on the `Maybe` instance and static helper functions in
the `Maybe` namespace.

## API Use

[API Documentation](https://www.jsdocs.io/package/maybe-result)

See the [unit test suite](src/index.spec.ts) for usage examples.

## Origin and Alternatives

This implementation is based on `Option` from [ts-results](https://github.com/vultix/ts-results),
Expand All @@ -45,9 +133,3 @@ It is up to you to decide which option is best for your project.

**The goal of this "maybe" is to be featureful, safe, and easy to understand without
a study of functional programming.**

## API Use

[API Documentation](https://www.jsdocs.io/package/maybe-ts)

See the [unit test suite](src/index.spec.ts) for usage examples.
8 changes: 4 additions & 4 deletions package-lock.json

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

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "maybe-ts",
"version": "1.0.0",
"description": "Safe handling of null and undefined in Typescript and Javascript",
"name": "maybe-result",
"version": "0.1.0",
"description": "Safe function return handling without null and undefined in Typescript and Javascript",
"keywords": [
"maybe",
"typescript",
Expand All @@ -15,12 +15,12 @@
"author": "Rackspace Technology",
"repository": {
"type": "git",
"url": "git+https://github.com/rackspace/maybe-ts.git"
"url": "git+https://github.com/rackspace/maybe.git"
},
"bugs": {
"url": "https://github.com/rackspace/maybe-ts/issues"
"url": "https://github.com/rackspace/maybe/issues"
},
"homepage": "https://github.com/rackspace/maybe-ts",
"homepage": "https://github.com/rackspace/maybe",
"contributors": [
"Adam Fanello <adam.fanello@rackspace.com>"
],
Expand Down

0 comments on commit c880367

Please sign in to comment.