Skip to content

Commit

Permalink
Move to camelCase as primary implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilson committed Feb 25, 2022
1 parent 77ed970 commit 495aecd
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 298 deletions.
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ for TypeScript.
- Express, chain and map values as if you were writing in Rust.
- Make guarded functions that return at the first sign of trouble (`?`).
- Use the `match` adaptation to simplify conditionals.
- API available both `snake_case` and `camelCase`.
- ~~API available both `snake_case` and `camelCase`.~~

Zero dependencies, full test coverage and examples for every function at your
fingertips with JSDoc comments.

<sub>Exported functions are also types, so VSCode shows them a different
colour. This might not be a feature but I'm really digging it. YMMV.</sub>
## Patch 0.9.7

After lots of great feedback, I've decided that the `snake_case` API will be
removed in the 1.0 release. Patch 0.9.7 moves `camelCase` to the front seat
without breaking anything.

# Installation

Expand Down Expand Up @@ -75,14 +78,14 @@ const res: number = val.unwrap();
// Throw our own error message in the case of None:
const res: number = val.expect("Division Failed");
// Pull the value out, or use a default if None:
const res: number = val.unwrap_or(1);
const res: number = val.unwrapOr(1);

// Map the Option<T> to Option<U> by applying a function:
const strval: Option<string> = val.map((num) => `Result = ${num}`);
// Then unwrap the value or use a default if None:
const res: string = strval.unwrap_or("Error");
const res: string = strval.unwrapOr("Error");
// Map, assign a default and unwrap in one line:
const res: string = val.map_or("Error", (num) => `Result = ${num}`);
const res: string = val.mapOr("Error", (num) => `Result = ${num}`);
```

_The type annotations applied to the const variables are for information -_
Expand Down Expand Up @@ -112,18 +115,18 @@ const val = divide(100, 20);
// These are the same as Option (as are many of the other methods):
const res: number = val.unwrap();
const res: number = val.expect("Division Failed");
const res: number = val.unwrap_or(1);
const res: number = val.unwrapOr(1);
// Map Result<T, E> to Result<U, E>, similar to mapping Option<T> to Option<U>
const strval: Result<string, string> = val.map((num) => `Result = ${num}`);
const res: string = strval.unwrap_or("Error");
const res: string = val.map_or("Error", (num) => `Result = ${num}`);
const res: string = strval.unwrapOr("Error");
const res: string = val.mapOr("Error", (num) => `Result = ${num}`);

// We can unwrap the error, which throws if the Result is Ok:
const err: string = val.unwrap_err();
const err: string = val.expect_err("Expected this to fail");
const err: string = val.unwrapErr();
const err: string = val.expectErr("Expected this to fail");

// Or map the error, mapping Result<T, E> to Result<T, F>
const objerr: Result<number, Error> = val.map_err((message) => {
const objerr: Result<number, Error> = val.mapErr((message) => {
return new Error(message);
});
```
Expand All @@ -139,7 +142,7 @@ Because they are so similar, it's possible to transform an `Option<T>` into a
const val: Option<number> = divide(100, 10);

// Here, the argument provides the Err value to be used if val is None:
const res: Result<number, string> = val.ok_or("Division Error");
const res: Result<number, string> = val.okOr("Division Error");

// And to turn it back into an Option:
const opt: Option<number> = res.ok();
Expand Down Expand Up @@ -268,7 +271,7 @@ const get_pos = Option((guard, x: number, y: number) => {
});

function show_pos(x: number, y: number): string {
return get_pos(x, y).map_or("Invalid Pos", ({ x, y }) => `Pos (${x},${y})`);
return get_pos(x, y).mapOr("Invalid Pos", ({ x, y }) => `Pos (${x},${y})`);
}

assert.equal(show_pos(10, 20), "Pos (100,200)");
Expand Down Expand Up @@ -306,7 +309,7 @@ const get_pos = Result((guard: Guard<string>, x: number, y: number) => {
});

function show_pos(x: number, y: number): string {
return get_pos(x, y).map_or_else(
return get_pos(x, y).mapOrElse(
(err) => `Error: ${err}`,
({ x, y }) => `Pos (${x},${y})`
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oxide.ts",
"version": "0.9.4",
"version": "0.9.7",
"description": "Rust's Option<T> and Result<T, E>, implemented for TypeScript.",
"main": "dist",
"types": "dist",
Expand Down
20 changes: 9 additions & 11 deletions src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,16 @@ function match<T, E, U>(

if (Option.is(val) && is_object_like(pat)) {
const { Some, None, _ } = pat as MappedOption<T, U>;
return val.is_some()
? call_or_branch(val.unwrap_unchecked() as T, Some, _)
return val.isSome()
? call_or_branch(val.unwrapUnchecked() as T, Some, _)
: call_or_branch(Default, None, _);
}

if (Result.is(val) && is_object_like(pat)) {
const { Ok, Err, _ } = pat as MappedResult<T, E, U>;
return val.is_ok()
? call_or_branch(val.unwrap_unchecked() as T, Ok, _)
: call_or_branch(val.unwrap_unchecked() as E, Err, _);
return val.isOk()
? call_or_branch(val.unwrapUnchecked() as T, Ok, _)
: call_or_branch(val.unwrapUnchecked() as E, Err, _);
}
}

Expand Down Expand Up @@ -306,8 +306,7 @@ function matches<T>(cond: BranchCondition<T> | Default, val: T): boolean {

if (Option.is(cond) || Result.is(cond)) {
return (
cond.is(val) &&
matches(cond.unwrap_unchecked(), val.unwrap_unchecked())
cond.is(val) && matches(cond.unwrapUnchecked(), val.unwrapUnchecked())
);
}

Expand Down Expand Up @@ -357,7 +356,7 @@ export function Fn<T extends (...args: any) => any>(fn: T): () => T {
* @deprecated
*/
export function SomeIs<T>(fn: (val: T) => boolean): Mapped<Option<T>, boolean> {
return (opt: Option<T>) => opt.is_some() && fn(opt.unwrap_unchecked() as T);
return (opt: Option<T>) => opt.isSome() && fn(opt.unwrapUnchecked() as T);
}

/**
Expand All @@ -369,7 +368,7 @@ export function SomeIs<T>(fn: (val: T) => boolean): Mapped<Option<T>, boolean> {
export function OkIs<T, E>(
fn: (val: T) => boolean
): Mapped<Result<T, E>, boolean> {
return (res: Result<T, E>) => res.is_ok() && fn(res.unwrap_unchecked() as T);
return (res: Result<T, E>) => res.isOk() && fn(res.unwrapUnchecked() as T);
}

/**
Expand All @@ -381,8 +380,7 @@ export function OkIs<T, E>(
export function ErrIs<E, T>(
fn: (val: E) => boolean
): Mapped<Result<T, E>, boolean> {
return (res: Result<T, E>) =>
res.is_err() && fn(res.unwrap_unchecked() as E);
return (res: Result<T, E>) => res.isErr() && fn(res.unwrapUnchecked() as E);
}

const BubbleToDefault = Symbol("BubbleToDefault");
Expand Down
Loading

0 comments on commit 495aecd

Please sign in to comment.