Skip to content

Commit

Permalink
Remove type predicates (#10)
Browse files Browse the repository at this point in the history
* Remove isNone, isSome, isSomeAnd

* Remove isErr, isErrAnd, isOk, isOkAnd
  • Loading branch information
bkiac authored Nov 4, 2023
1 parent 580be8a commit ef04b88
Show file tree
Hide file tree
Showing 12 changed files with 1 addition and 287 deletions.
5 changes: 0 additions & 5 deletions src/option/interface.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import type {Panic} from "../error/panic"
import type {inspectSymbol} from "../util"
import type {None} from "./none"
import type {Some} from "./some"

export interface OptionMethods<T> {
and<U>(other: Option<U>): Option<U>
andThen<U>(f: (value: T) => Option<U>): Option<U>
expect(panic: string | Panic): T
filter(f: (value: T) => boolean): Option<T>
inspect(f: (value: T) => void): Option<T>
isNone(): this is None
isSome(): this is Some<T>
isSomeAnd(f: (value: T) => boolean): this is Some<T>
map<U>(f: (value: T) => U): Option<U>
mapOr<A, B>(defaultValue: A, f: (value: T) => B): A | B
mapOrElse<A, B>(defaultValue: () => A, f: (value: T) => B): A | B
Expand Down
13 changes: 0 additions & 13 deletions src/option/none.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Panic, UnwrapPanic} from "../error/panic"
import {inspectSymbol} from "../util"
import type {OptionMethods, Option, NoneVariant} from "./interface"
import type {Some} from "./some"

export class NoneImpl implements NoneVariant, OptionMethods<never> {
readonly some = false
Expand All @@ -27,18 +26,6 @@ export class NoneImpl implements NoneVariant, OptionMethods<never> {
return this
}

isNone(): this is None {
return true
}

isSome(): this is Some<never> {
return false
}

isSomeAnd(_f: (value: never) => boolean): this is Some<never> {
return false
}

map<U>(_f: (value: never) => U) {
return None
}
Expand Down
41 changes: 0 additions & 41 deletions src/option/option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,47 +98,6 @@ describe.concurrent("inspect", () => {
})
})

describe.concurrent("isNone", () => {
it("returns false for a Some option", () => {
const option = Some(42)
expect(option.isNone()).toEqual(false)
})

it("returns true for a None option", () => {
const option = None
expect(option.isNone()).toEqual(true)
})
})

describe.concurrent("isSome", () => {
it("returns true for a Some option", () => {
const option = Some(42)
expect(option.isSome()).toEqual(true)
})

it("returns false for a None option", () => {
const option = None
expect(option.isSome()).toEqual(false)
})
})

describe.concurrent("isSomeAnd", () => {
it("returns true for a Some option when the predicate returns true", () => {
const option = Some(42)
expect(option.isSomeAnd((value) => value === 42)).toEqual(true)
})

it("returns false for a Some option when the predicate returns false", () => {
const option = Some(42)
expect(option.isSomeAnd((value) => value !== 42)).toEqual(false)
})

it("returns false for a None option", () => {
const option = None
expect(option.isSomeAnd((value) => value === 42)).toEqual(false)
})
})

describe.concurrent("map", () => {
it("returns the mapped value for a Some option", () => {
const option = Some(42)
Expand Down
41 changes: 0 additions & 41 deletions src/option/promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,47 +90,6 @@ describe.concurrent("inspect", () => {
})
})

describe.concurrent("isNone", () => {
it("returns false when called on a Some option", async () => {
const option = promiseSome(42)
await expect(option.isNone()).resolves.toEqual(false)
})

it("returns true when called on a None option", async () => {
const option = promiseNone()
await expect(option.isNone()).resolves.toEqual(true)
})
})

describe.concurrent("isSome", () => {
it("returns true when called on a Some option", async () => {
const option = promiseSome(42)
await expect(option.isSome()).resolves.toEqual(true)
})

it("returns false when called on a None option", async () => {
const option = promiseNone()
await expect(option.isSome()).resolves.toEqual(false)
})
})

describe.concurrent("isSomeAnd", () => {
it("returns true when called on a Some option and the predicate returns true", async () => {
const option = promiseSome(42)
await expect(option.isSomeAnd((value) => value === 42)).resolves.toEqual(true)
})

it("returns false when called on a Some option and the predicate returns false", async () => {
const option = promiseSome(42)
await expect(option.isSomeAnd((value) => value !== 42)).resolves.toEqual(false)
})

it("returns false when called on a None option", async () => {
const option = promiseNone()
await expect(option.isSomeAnd((value) => value === 42)).resolves.toEqual(false)
})
})

describe.concurrent("map", () => {
it("returns the mapped value for a Some option", async () => {
const option = promiseSome(42)
Expand Down
12 changes: 0 additions & 12 deletions src/option/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ export class PromiseOption<T> implements PromiseLike<Option<T>> {
return new PromiseOption<T>(this.then((option) => option.inspect(f)))
}

async isNone() {
return (await this).isNone()
}

async isSome() {
return (await this).isSome()
}

async isSomeAnd(f: (value: T) => boolean) {
return (await this).isSomeAnd(f)
}

async map<U>(f: (value: T) => U) {
return (await this).map(f)
}
Expand Down
14 changes: 1 addition & 13 deletions src/option/some.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@ export class SomeImpl<T> implements SomeVariant<T>, OptionMethods<T> {
return this
}

isNone(): this is None {
return false
}

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

isSomeAnd(f: (value: T) => boolean): this is Some<T> {
return f(this.value)
}

map<U>(f: (value: T) => U) {
return Some(f(this.value))
}
Expand Down Expand Up @@ -78,7 +66,7 @@ export class SomeImpl<T> implements SomeVariant<T>, OptionMethods<T> {
}

xor<U>(other: Option<U>) {
return other.isSome() ? None : this
return other.some ? None : this
}

into() {
Expand Down
17 changes: 0 additions & 17 deletions src/result/err.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Panic, UnwrapPanic} from "../error/panic"
import {inspectSymbol} from "../util"
import type {ErrVariant, Result, ResultMethods} from "./interface"
import type {Ok} from "./ok"

export class ErrImpl<E> implements ErrVariant<E>, ResultMethods<never, E> {
readonly ok = false
Expand Down Expand Up @@ -40,22 +39,6 @@ export class ErrImpl<E> implements ErrVariant<E>, ResultMethods<never, E> {
return this
}

isErr(): this is Err<E> {
return true
}

isErrAnd(f: (error: E) => boolean): this is Err<E> {
return f(this.error)
}

isOk(): this is Ok<never> {
return false
}

isOkAnd(_f: (value: never) => boolean): this is Ok<never> {
return false
}

map<U>(_f: (value: never) => U) {
return this
}
Expand Down
6 changes: 0 additions & 6 deletions src/result/interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type {Panic} from "../error/panic"
import type {inspectSymbol} from "../util"
import type {Err} from "./err"
import type {Ok} from "./ok"

export interface ResultMethods<T, E> {
and<U, F>(other: Result<U, F>): Result<U, E | F>
Expand All @@ -10,10 +8,6 @@ export interface ResultMethods<T, E> {
expectErr(panic: string | Panic): E
inspect(f: (value: T) => void): Result<T, E>
inspectErr(f: (error: E) => void): Result<T, E>
isErr(): this is Err<E>
isErrAnd(f: (error: E) => boolean): this is Err<E>
isOk(): this is Ok<T>
isOkAnd(f: (value: T) => boolean): this is Ok<T>
map<U>(f: (value: T) => U): Result<U, E>
mapErr<F>(f: (error: E) => F): Result<T, F>
mapOr<A, B>(defaultValue: A, f: (value: T) => B): A | B
Expand Down
17 changes: 0 additions & 17 deletions src/result/ok.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Panic, UnwrapPanic} from "../error/panic"
import {inspectSymbol} from "../util"
import type {Err} from "./err"
import type {OkVariant, Result, ResultMethods} from "./interface"

export class OkImpl<T> implements OkVariant<T>, ResultMethods<T, never> {
Expand Down Expand Up @@ -40,22 +39,6 @@ export class OkImpl<T> implements OkVariant<T>, ResultMethods<T, never> {
return this
}

isErr(): this is Err<never> {
return false
}

isErrAnd(_f: (error: never) => boolean): this is Err<never> {
return false
}

isOk(): this is Ok<T> {
return true
}

isOkAnd(f: (value: T) => boolean): this is Ok<T> {
return f(this.value)
}

map<U>(f: (value: T) => U) {
return Ok(f(this.value))
}
Expand Down
58 changes: 0 additions & 58 deletions src/result/promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,64 +137,6 @@ describe.concurrent("inspectErr", async () => {
})
})

describe.concurrent("isErr", () => {
it("returns false for an Ok result", async () => {
const result = new PromiseResult(Promise.resolve(Ok()))
await expect(result.isErr()).resolves.toEqual(false)
})

it("returns true for an Err result", async () => {
const result = new PromiseResult(Promise.resolve(Err(new Error("Test error"))))
await expect(result.isErr()).resolves.toEqual(true)
})
})

describe.concurrent("isErrAnd", () => {
it("returns false for an Ok result", async () => {
const result = new PromiseResult(Promise.resolve(Ok()))
await expect(result.isErrAnd(() => true)).resolves.toEqual(false)
})

it("returns true for an Err result when the provided function returns true", async () => {
const result = new PromiseResult(Promise.resolve(Err(new Error("Test error"))))
await expect(result.isErrAnd(() => true)).resolves.toEqual(true)
})

it("returns false for an Err result when the provided function returns false", async () => {
const result = new PromiseResult(Promise.resolve(Err(new Error("Test error"))))
await expect(result.isErrAnd(() => false)).resolves.toEqual(false)
})
})

describe.concurrent("isOk", () => {
it("returns true for an Ok result", async () => {
const result = new PromiseResult(Promise.resolve(Ok()))
await expect(result.isOk()).resolves.toEqual(true)
})

it("returns false for an Err result", async () => {
const result = new PromiseResult(Promise.resolve(Err(new Error("Test error"))))
await expect(result.isOk()).resolves.toEqual(false)
})
})

describe.concurrent("isOkAnd", () => {
it("returns true for an Ok result when the provided function returns true", async () => {
const result = new PromiseResult(Promise.resolve(Ok()))
await expect(result.isOkAnd(() => true)).resolves.toEqual(true)
})

it("returns false for an Ok result when the provided function returns false", async () => {
const result = new PromiseResult(Promise.resolve(Ok()))
await expect(result.isOkAnd(() => false)).resolves.toEqual(false)
})

it("returns false for an Err result", async () => {
const result = new PromiseResult(Promise.resolve(Err(new Error("Test error"))))
await expect(result.isOkAnd(() => true)).resolves.toEqual(false)
})
})

describe.concurrent("map", () => {
it("returns the mapped value for an Ok result", async () => {
const result = new PromiseResult(Promise.resolve(Ok(42)))
Expand Down
16 changes: 0 additions & 16 deletions src/result/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,6 @@ export class PromiseResult<T, E> implements PromiseLike<Result<T, E>> {
return new PromiseResult<T, E>(this.then((result) => result.inspectErr(f)))
}

async isErr(): Promise<boolean> {
return (await this).isErr()
}

async isErrAnd(f: (error: E) => boolean): Promise<boolean> {
return (await this).isErrAnd(f)
}

async isOk(): Promise<boolean> {
return (await this).isOk()
}

async isOkAnd(f: (value: T) => boolean): Promise<boolean> {
return (await this).isOkAnd(f)
}

map<U>(f: (value: T) => U): PromiseResult<U, E> {
return new PromiseResult(this.then((result) => result.map(f)))
}
Expand Down
Loading

0 comments on commit ef04b88

Please sign in to comment.