Skip to content

Commit

Permalink
Deprecated is/eq/neq and add isLike/equals
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilson committed Mar 19, 2022
1 parent 1e3d51c commit 586249d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 23 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Patch 0.9.12

Changes to Option/Result methods:

- `is` has been deprecated and replaced by `isLike`.
- `eq` has been deprecated and replaced by `equals`.
- `neq` has been deprecated.

# Patch 0.9.11

Fix publishing mistake.

# Patch 0.9.10

Deprecated guarded functions.
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.11",
"version": "0.9.12",
"description": "Rust's Option<T> and Result<T, E>, implemented for TypeScript.",
"main": "dist",
"types": "dist",
Expand Down
42 changes: 30 additions & 12 deletions src/monad/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,55 @@ class OptionType<T> {
Object.freeze(this);
}

/**
* See `like()`.
* @deprecated
*/
is(cmp: unknown): cmp is Option<unknown> {
return this.isLike(cmp);
}

/**
* Compares the Option to `cmp`, returns true if both are `Some` or both
* are `None`. Acts as a type guard for `cmp is Option<unknown>`.
* are `None`. Also acts as a type guard for `Option<unknown>`.
*
* ```
* const s: Option<number> = Some(1);
* const n: Option<number> = None;
*
* assert.equal(s.is(Some(10)), true);
* assert.equal(n.is(None), true);
* assert.equal(s.is(n), false);
* assert.equal(s.isLike(Some(10)), true);
* assert.equal(n.isLike(None), true);
* assert.equal(s.isLike(n), false);
* ```
*/
is(cmp: unknown): cmp is Option<unknown> {
isLike(cmp: unknown): cmp is Option<unknown> {
return cmp instanceof OptionType && this[IsSome] === cmp[IsSome];
}

/**
* See `equals()`.
* @deprecated
*/
eq(cmp: Option<T>): boolean {
return this.equals(cmp);
}

/**
* Compares the Option to `cmp` for equality. Returns `true` when both are
* the same type (`Some`/`None`) and their contained values are identical
* (`===`).
* `Some` with identical contained values, or both are `None`.
*
* ```
* const val = { x: 10 };
* const s: Option<{ x: number; }> = Some(val);
* const n: Option<{ x: number; }> = None;
*
* assert.equal(s.eq(Some(val)), true);
* assert.equal(n.eq(None), true):
* assert.equal(s.eq(Some({ x: 10 })), false);
* assert.equal(s.eq(n), false);
* assert.equal(s.equals(Some(val)), true);
* assert.equal(n.equals(None), true):
* assert.equal(s.equals(Some({ x: 10 })), false);
* assert.equal(s.equals(n), false);
* ```
*/
eq(cmp: Option<T>): boolean {
equals(cmp: Option<T>): boolean {
return this[IsSome] === cmp[IsSome] && this.val === cmp.val;
}

Expand All @@ -66,6 +82,8 @@ class OptionType<T> {
* assert.equal(s.neq(Some({ x: 10})), true);
* assert.equal(s.new(n), true);
* ```
*
* @deprecated
*/
neq(cmp: Option<T>): boolean {
return this[IsSome] !== cmp[IsSome] || this.val !== cmp.val;
Expand Down
44 changes: 34 additions & 10 deletions src/monad/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,57 @@ class ResultType<T, E> {
Object.freeze(this);
}

/**
* See `isLike()`.
* @deprecated
*/
is(cmp: unknown): cmp is Result<unknown, unknown> {
return this.isLike(cmp);
}

/**
* Compares the Result to `cmp`, returns true if both are `Ok` or both
* are `Err`. Acts as a type guard for `cmp is Result<unknown, unknown>`.
*
* ```
* const o = Ok(1);
* const e = Err(1);
*
* assert.equal(o.is(Ok(1))), true);
* assert.equal(e.is(Err(1)), true);
* assert.equal(o.is(e), false);
* assert.equal(o.isLike(Ok(1))), true);
* assert.equal(e.isLike(Err(1)), true);
* assert.equal(o.isLike(e), false);
* ```
*/
is(cmp: unknown): cmp is Result<unknown, unknown> {
isLike(cmp: unknown): cmp is Result<unknown, unknown> {
return cmp instanceof ResultType && this[IsOk] === cmp[IsOk];
}

/**
* See `equals()`.
* @deprecated
*/
eq(cmp: Result<T, E>): boolean {
return this.equals(cmp);
}

/**
* Compares the Result to `cmp` for equality. Returns `true` when both are
* the same type (`Ok`/`Err`) and their contained values are identical
* (`===`).
*
* ```
* const val = { x: 10 };
* const o: Result<{ x: number; }, { x: number; }> = Ok(val);
* const e: Result<{ x: number; }, { x: number; }> = Err(val);
*
* assert.equal(o.eq(Ok(val)), true);
* assert.equal(e.eq(Err(val)), true):
* assert.equal(o.eq(Ok({ x: 10 })), false);
* assert.equal(e.eq(Err({ x: 10 })), false);
* assert.equal(o.eq(e), false);
* assert.equal(o.equals(Ok(val)), true);
* assert.equal(e.equals(Err(val)), true):
* assert.equal(o.equals(Ok({ x: 10 })), false);
* assert.equal(e.equals(Err({ x: 10 })), false);
* assert.equal(o.equals(e), false);
* ```
*/
eq(cmp: Result<T, E>): boolean {
equals(cmp: Result<T, E>): boolean {
return this[IsOk] === cmp[IsOk] && this.val === cmp.val;
}

Expand All @@ -55,6 +75,7 @@ class ResultType<T, E> {
* different types (`Ok`/`Err`) or their contained values are not identical
* (`!==`).
*
* ```
* const val = { x: 10 };
* const o: Result<{ x: number; }, { x: number; }> = Ok(val);
* const e: Result<{ x: number; }, { x: number; }> = Err(val);
Expand All @@ -64,6 +85,9 @@ class ResultType<T, E> {
* assert.equal(o.neq(Ok({ x: 10 })), true);
* assert.equal(e.neq(Err({ x: 10 })), true);
* assert.equal(o.neq(e), true);
* ```
*
* @deprecated
*/
neq(cmp: Result<T, E>): boolean {
return this[IsOk] !== cmp[IsOk] || this.val !== cmp.val;
Expand Down

0 comments on commit 586249d

Please sign in to comment.