Skip to content

Commit

Permalink
Merge pull request #3 from RobDWaller/0.3.1
Browse files Browse the repository at this point in the history
0.3.1
  • Loading branch information
RobDWaller authored Jun 29, 2020
2 parents f544a80 + 30b019d commit 0496c9e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 29 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The Option objects allow you to return a standard type where the result of a met

## Installation / Setup

To add Resulty to your project simply import the `ok()`, `err()`, `some()` and `none()` methods along with the `Result<T>` and `Opt<T>` types from `https://deno.land/x/resulty@0.3.0/mod.ts`.
To add Resulty to your project simply import the `ok()`, `err()`, `some()` and `none()` methods along with the `Result<T>` and `Opt<T>` types from `https://deno.land/x/resulty@0.3.1/mod.ts`.

```js
import {
Expand All @@ -21,7 +21,7 @@ import {
Result,
Opt,
Panic,
} from "https://deno.land/x/resulty@0.3.0/mod.ts"
} from "https://deno.land/x/resulty@0.3.1/mod.ts"
```

## Usage
Expand All @@ -31,7 +31,7 @@ The core functionality of the this library is contained in the `ok()`, `err()`,
Both `Result<T>` and `Opt<T>` are wrappers for other types and objects. And you can access these contained types and objects via the `unwrap()` method.

```js
import { Result, ok } from "https://deno.land/x/resulty@0.3.0/mod.ts";
import { Result, ok } from "https://deno.land/x/resulty@0.3.1/mod.ts";

const isOk: Result<string> = ok("Hello");

Expand Down Expand Up @@ -61,7 +61,7 @@ Available `Result<T>` methods:
In this example the code returns a `Result<string>`. As you can see both the `ok()` and `err()` methods receive a string.

```js
import { Result, ok, err } from "https://deno.land/x/resulty@0.3.0/mod.ts";
import { Result, ok, err } from "https://deno.land/x/resulty@0.3.1/mod.ts";

const isSandra = function (name: string): Result<string> {
if (name === "Sandra") {
Expand All @@ -83,7 +83,7 @@ console.log(sandra.unwrap());
A more advanced use case may involve a situation where the `ok()` method receives a number and the `err()` method receives a string. In this scenario you can reference a union type in the `Result<number | string>` return type.

```js
import { Result, ok, err } from "https://deno.land/x/resulty@0.3.0/mod.ts";
import { Result, ok, err } from "https://deno.land/x/resulty@0.3.1/mod.ts";

const findNumber = function (toFind: number): Result<number | string> {
const numbers = [1, 4, 6, 7, 21, 33];
Expand Down Expand Up @@ -117,7 +117,7 @@ Available `Opt<T>` methods:
Options are useful in scenarios where a system failure hasn't occurred but either something or nothing can be returned. For instance when looking for a record in a data store of some kind.

```js
import { Opt, some, none } from "https://deno.land/x/resulty@0.3.0/mod.ts";
import { Opt, some, none } from "https://deno.land/x/resulty@0.3.1/mod.ts";

let findRecord = function (id: number): Opt<string> {
let records = [{id: 1, value: "Hello"}, {id: 13, value: "World"}];
Expand Down
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "resulty",
"description": "Provides simple, Rust-like Result and Option objects for Deno.",
"version": "0.3.0",
"version": "0.3.1",
"entry": "./mod.ts",
"stable": false,
"repository": "https://github.com/robdwaller/resulty",
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function some<T>(something: T): Opt<T> {
return new Some<T>(something);
}

export function none(): Opt<null> {
export function none(): None {
return new None();
}

Expand Down
3 changes: 0 additions & 3 deletions src/is.ts

This file was deleted.

17 changes: 8 additions & 9 deletions src/option.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Unwrap } from "./unwrap.ts";
import { Is } from "./is.ts";
import { Panic } from "./panic.ts";

export type Opt<T> = Some<T> | None;
Expand All @@ -8,8 +7,8 @@ interface Options<T> {
readonly option: T | null;
unwrap: Unwrap<T>;
unwrapNone: Unwrap<T>;
isSome: Is;
isNone: Is;
isSome(): this is Some<T>;
isNone(): this is None;
}

export class Some<T> implements Options<T> {
Expand All @@ -27,11 +26,11 @@ export class Some<T> implements Options<T> {
throw new Panic(String(this.option));
}

isSome(): boolean {
isSome(): this is Some<T> {
return true;
}

isNone(): boolean {
isNone(): this is None {
return false;
}
}
Expand All @@ -43,15 +42,15 @@ export class None implements Options<null> {
throw new Panic("Cannot unwrap None.");
}

unwrapNone(): void {
// Return nothing
unwrapNone(): null {
return null;
}

isSome(): boolean {
isSome(): this is Some<null> {
return false;
}

isNone(): boolean {
isNone(): this is None {
return true;
}
}
13 changes: 6 additions & 7 deletions src/result.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Unwrap } from "./unwrap.ts";
import { Is } from "./is.ts";
import { Panic } from "./panic.ts";

export interface Result<T> {
readonly state: T;
unwrap: Unwrap<T>;
unwrapErr: Unwrap<T>;
isError: Is;
isOk: Is;
isError(): this is Err<T>;
isOk(): this is Ok<T>;
}

export class Ok<T> implements Result<T> {
Expand All @@ -25,11 +24,11 @@ export class Ok<T> implements Result<T> {
throw new Panic(String(this.state));
}

isError(): boolean {
isError(): this is Err<T> {
return false;
}

isOk(): boolean {
isOk(): this is Ok<T> {
return true;
}
}
Expand All @@ -49,11 +48,11 @@ export class Err<T> implements Result<T> {
return this.state;
}

isError(): boolean {
isError(): this is Err<T> {
return true;
}

isOk(): boolean {
isOk(): this is Ok<T> {
return false;
}
}
52 changes: 51 additions & 1 deletion tests/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,55 @@ Deno.test("Option Example One", () => {

const notFound = findRecord(2);

assertStrictEq(notFound.unwrapNone(), undefined);
assertStrictEq(notFound.unwrapNone(), null);
});

Deno.test("Result Handle Polymorphism Example", () => {
const isSandra = function (name: string): Result<string> {
if (name === "Sandra") {
return ok("Is Sandra");
}
return err("Is not Sandra");
};

function processResult(result: string): boolean {
return result === "Is Sandra";
}

const geoff: Result<string> = isSandra("Geoff");

if (geoff.isError()) {
assertStrictEq(processResult(geoff.unwrapErr()), false);
}

const sandra: Result<string> = isSandra("Sandra");

if (sandra.isOk()) {
assertStrictEq(processResult(sandra.unwrap()), true);
}
});

Deno.test("Option Handle Polymorphism Example", () => {
const hasSandra = function (name: string): Opt<string> {
if (name === "Sandra") {
return some("Has Sandra");
}
return none();
};

function processResult(result: string): boolean {
return result === "Has Sandra";
}

const geoff: Opt<string> = hasSandra("Geoff");

if (geoff.isNone()) {
assertStrictEq(geoff.unwrapNone(), null);
}

const sandra: Opt<string> = hasSandra("Sandra");

if (sandra.isSome()) {
assertStrictEq(processResult(sandra.unwrap()), true);
}
});
2 changes: 1 addition & 1 deletion tests/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Deno.test("Option None", () => {
const result = maybeNone();

assertStrictEq(result instanceof None, true);
assertStrictEq(result.unwrapNone(), undefined);
assertStrictEq(result.unwrapNone(), null);
});

Deno.test("Is instance of Some or None", () => {
Expand Down

0 comments on commit 0496c9e

Please sign in to comment.