Skip to content

Commit

Permalink
Remove from, into
Browse files Browse the repository at this point in the history
  • Loading branch information
bkiac committed Nov 10, 2023
1 parent 80ec5f9 commit 9aea9d3
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 95 deletions.
4 changes: 2 additions & 2 deletions src/option/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ export interface OptionMethods<T> {
unwrapOrElse<U>(defaultValue: () => U): T | U
xor<U>(other: Option<U>): Option<T | U>

into(): T | null
match<A, B>(some: (value: T) => A, none: () => B): A | B

toString(): `Some(${string})` | "None"
[inspectSymbol](): ReturnType<OptionMethods<T>["toString"]>
toObject(): {some: true; value: T} | {some: false; value: null}
toJSON(): {meta: "Some"; data: T} | {meta: "None"}
toJSON(): {meta: "Some"; value: T} | {meta: "None"; value: null}
}

export interface SomeVariant<T> {
Expand All @@ -34,6 +33,7 @@ export interface SomeVariant<T> {
export interface NoneVariant {
readonly some: false
readonly none: true
readonly value: null
}

export type OptionVariants<T> = SomeVariant<T> | NoneVariant
Expand Down
5 changes: 3 additions & 2 deletions src/option/none.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {OptionMethods, Option, NoneVariant} from "./interface"
export class NoneImpl implements NoneVariant, OptionMethods<never> {
readonly some = false
readonly none = true
readonly value = null

and<U>(_other: Option<U>) {
return None
Expand Down Expand Up @@ -62,7 +63,7 @@ export class NoneImpl implements NoneVariant, OptionMethods<never> {
return other
}

into() {
get() {
return null
}

Expand All @@ -83,7 +84,7 @@ export class NoneImpl implements NoneVariant, OptionMethods<never> {
}

toJSON() {
return {meta: "None"} as const
return {meta: "None", value: null} as const
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/option/option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ describe.concurrent("xor", () => {
describe.concurrent("into", () => {
it("returns the value when called on a Some option", () => {
const option = Some(42)
expect(option.into()).toEqual(42)
expect(option.get()).toEqual(42)
})

it("throws when called on a None option", () => {
const option = None
expect(option.into()).toEqual(null)
expect(option.get()).toEqual(null)
})
})

Expand Down
12 changes: 0 additions & 12 deletions src/option/promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,6 @@ describe.concurrent("xor", () => {
})
})

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

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

describe.concurrent("match", () => {
it("returns the mapped value for a Some option", async () => {
const option = promiseSome(42)
Expand Down
4 changes: 0 additions & 4 deletions src/option/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ export class PromiseOption<T> implements PromiseLike<Option<T>> {
)
}

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

async match<A, B>(some: (value: T) => A, none: () => B) {
return (await this).match(some, none)
}
Expand Down
12 changes: 5 additions & 7 deletions src/option/some.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class SomeImpl<T> implements SomeVariant<T>, OptionMethods<T> {
return other.some ? None : this
}

into() {
get() {
return this.value
}

Expand All @@ -89,13 +89,11 @@ export class SomeImpl<T> implements SomeVariant<T>, OptionMethods<T> {
}

toJSON() {
return {meta: "Some", data: this.value} as const
}

static from<T>(value: T): Some<T> {
return new SomeImpl(value)
return {meta: "Some", value: this.value} as const
}
}

export interface Some<T> extends SomeImpl<T> {}
export const Some = SomeImpl.from
export function Some<T>(value: T): Some<T> {
return new SomeImpl(value)
}
44 changes: 21 additions & 23 deletions src/result/err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type {ErrVariant, Result, ResultMethods} from "./interface"
export class ErrImpl<E> implements ErrVariant<E>, ResultMethods<never, E> {
readonly ok = false
readonly err = true
readonly error: E
readonly value: E

constructor(error: E) {
this.error = error
this.value = error
}

and<U, F>(_other: Result<U, F>) {
Expand All @@ -20,19 +20,19 @@ export class ErrImpl<E> implements ErrVariant<E>, ResultMethods<never, E> {
}

expect(panic: string): never {
throw new Panic(panic, this.error)
throw new Panic(panic, this.value)
}

expectErr(_panic: string) {
return this.error
return this.value
}

inspect(_f: (value: never) => void) {
return this
}

inspectErr(f: (error: E) => void) {
f(this.error)
f(this.value)
return this
}

Expand All @@ -41,71 +41,69 @@ export class ErrImpl<E> implements ErrVariant<E>, ResultMethods<never, E> {
}

mapErr<F>(f: (error: E) => F) {
return Err(f(this.error))
return Err(f(this.value))
}

mapOr<A, B>(defaultValue: A, _f: (value: never) => B) {
return defaultValue
}

mapOrElse<A, B>(defaultValue: (error: E) => A, _f: (value: never) => B) {
return defaultValue(this.error)
return defaultValue(this.value)
}

or<U, F>(other: Result<U, F>) {
return other
}

orElse<U, F>(f: (error: E) => Result<U, F>) {
return f(this.error)
return f(this.value)
}

unwrap(): never {
throw new UnwrapPanic(`called "unwrap()" on ${this.toString()}`)
}

unwrapErr() {
return this.error
return this.value
}

unwrapOr<U>(defaultValue: U) {
return defaultValue
}

unwrapOrElse<U>(defaultValue: (error: E) => U) {
return defaultValue(this.error)
return defaultValue(this.value)
}

into() {
return this.error
get() {
return this.value
}

match<A, B>(_ok: (value: never) => A, err: (error: E) => B) {
return err(this.error)
return err(this.value)
}

toString() {
return `Err(${this.error})` as const
return `Err(${this.value})` as const
}

[inspectSymbol]() {
return this.toString()
}

toObject() {
return {ok: false, error: this.error} as const
return {ok: false, value: this.value} as const
}

toJSON() {
return {meta: "Err", data: this.error} as const
}

static from(): Err
static from<E>(error: E): Err<E>
static from<E>(error?: E): Err<E> {
return new ErrImpl(error ? error : null) as Err<E>
return {meta: "Err", value: this.value} as const
}
}

export interface Err<E = null> extends ErrImpl<E> {}
export const Err = ErrImpl.from
export function Err(): Err
export function Err<E>(value: E): Err<E>
export function Err<E>(value?: E): Err<E> {
return new ErrImpl(value ? value : null) as Err<E>
}
7 changes: 3 additions & 4 deletions src/result/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ export interface ResultMethods<T, E> {
unwrapOr<U>(defaultValue: U): T | U
unwrapOrElse<U>(defaultValue: (error: E) => U): T | U

into(): T | E
match<A, B>(ok: (value: T) => A, err: (error: E) => B): A | B

toString(): `Ok(${string})` | `Err(${string})`
[inspectSymbol](): ReturnType<ResultMethods<T, E>["toString"]>
toObject(): {ok: true; value: T} | {ok: false; error: E}
toJSON(): {meta: "Ok"; data: T} | {meta: "Err"; data: E}
toObject(): {ok: true; value: T} | {ok: false; value: E}
toJSON(): {meta: "Ok"; value: T} | {meta: "Err"; value: E}
}

export interface OkVariant<T> {
Expand All @@ -36,7 +35,7 @@ export interface OkVariant<T> {
export interface ErrVariant<E> {
readonly ok: false
readonly err: true
readonly error: E
readonly value: E
}

export type ResultVariants<T, E> = OkVariant<T> | ErrVariant<E>
Expand Down
16 changes: 7 additions & 9 deletions src/result/ok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class OkImpl<T> implements OkVariant<T>, ResultMethods<T, never> {
return this.value
}

into() {
get() {
return this.value
}

Expand All @@ -97,15 +97,13 @@ export class OkImpl<T> implements OkVariant<T>, ResultMethods<T, never> {
}

toJSON() {
return {meta: "Ok", data: this.value} as const
}

static from(): Ok
static from<T>(value: T): Ok<T>
static from<T>(value?: T): Ok<T> {
return new OkImpl(value ? value : null) as Ok<T>
return {meta: "Ok", value: this.value} as const
}
}

export interface Ok<T = null> extends OkImpl<T> {}
export const Ok = OkImpl.from
export function Ok(): Ok
export function Ok<T>(value: T): Ok<T>
export function Ok<T>(value?: T): Ok<T> {
return new OkImpl(value ? value : null) as Ok<T>
}
12 changes: 0 additions & 12 deletions src/result/promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,6 @@ describe.concurrent("unwrapOrElse", () => {
})
})

describe.concurrent("into", () => {
it("returns the value for an Ok result", async () => {
const result = promiseOk(42)
await expect(result.into()).resolves.toEqual(42)
})

it("returns the err for an Err result", async () => {
const result = promiseErr("error")
await expect(result.into()).resolves.toEqual("error")
})
})

describe.concurrent("match", () => {
it("calls the ok function for an Ok result", async () => {
const result = new PromiseResult(Promise.resolve(Ok(42)))
Expand Down
4 changes: 0 additions & 4 deletions src/result/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ export class PromiseResult<T, E> implements PromiseLike<Result<T, E>> {
return (await this).unwrapOrElse(defaultValue)
}

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

async match<A, B>(ok: (value: T) => A, err: (error: E) => B) {
return (await this).match(ok, err)
}
Expand Down
16 changes: 2 additions & 14 deletions src/result/result.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, it, expect} from "vitest"
import {Panic, UnwrapPanic, Ok, Err, Result} from "../internal"
import {Panic, UnwrapPanic, Ok, Err} from "../internal"

const testErr = new Error("hello")

Expand All @@ -15,7 +15,7 @@ describe.concurrent("err", () => {
it("returns an Err result", () => {
const result = Err("error")
expect(result.ok).toEqual(false)
expect(result.error).toEqual("error")
expect(result.value).toEqual("error")
})
})

Expand Down Expand Up @@ -293,18 +293,6 @@ describe.concurrent("unwrapOrElse", () => {
})
})

describe.concurrent("into", () => {
it("returns the value for an Ok result", () => {
const result = Ok(42) as Result<number, Error>
expect(result.into()).toEqual(42)
})

it("returns the err for an Err result", () => {
const result = Err(42) as Result<string, number>
expect(result.into()).toEqual(42)
})
})

describe.concurrent("match", () => {
it("calls the ok function for an Ok result", () => {
const result = Ok(42)
Expand Down

0 comments on commit 9aea9d3

Please sign in to comment.