0.1.11 • Published 5 months ago

error-null-handle v0.1.11

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

function programming approach for error and null handling

API Reference

Maybe\

  • just :: \(value: A) => Maybe\

    create a Just object

  • nothing :: \() => Maybe\

    create a Nothing object

  • fromObject :: \(obj: NothingObject | JustObject\) => Result\<Maybe\, string>

    convert a JustObject\ or NothingObject to a Maybe\

  • fromString :: \(s: string) => Result\<Maybe\, string>

    convert a string generated by stringify the Maybe\ to a Maybe\

  • fromNullable :: \(value: A | null | undefined) => Maybe\

    Creates a Maybe instance from a nullable value.

Maybe instance

  • isJust / isNothing :: () => boolean

    indicate the instance is Just\ / Nothing\

  • unwrap :: () => A

    returns the contained Maybe value, would panic on Nothing

  • unwrapOr :: (defaultValue: A) => A

    returns the contained Maybe value or a provided default value(if Nothing).

  • unwrapOrElse: (f: () => A) => A

    returns the contained Maybe value or return the parameter function's return value(if Nothing).

  • map :: \(f: (v: A) => B) => Maybe\

    convert Maybe\ to Maybe\ without unwrap the Maybe type which may caust an error

  • mapOr :: \(f: (v: A) => B, defaultValue: B) => B

    returns the provided default result (if Nothing), or applies the function to the contained value (if Just).

  • mapOrElse :: \(f: (v: A) => B, defaultValue: () => B) => B

    computes a default function result (if Nothing), or applies a different function to the contained value (if Just).

  • toResult: \(err: B) => Result\<A, B>

    transforms the Maybe\ into a Result\<A, B>, mapping Just\ to Ok\ and Nothing to Err\.

  • ap :: \(other: Maybe\) => Maybe\

    apply a Maybe\<(a: A) => B> to a Maybe\,

    note that the caller instance should be Maybe\<(a: A) => B>.

  • bind :: \(f: (a: A) => Maybe\) => Maybe\

    apply a function to the Maybe value without concerning about Nothing.

    return Nothing if Nothing.

  • match :: \(onJust: (a: A) => B, onNothing: () => B) => B

    do something on Maybe\, note that you should handle the two cases: Just\ and Nothing

    if return some value, the two handlers should return the same type.

    if you want to return different types, use do instead.

  • do :: \<T, U>(onJust: (a: A) => T, onNothing: () => U) => T | U

    do something on Maybe\, note that you should handle the two cases: Just\ and Nothing

Result\<A, B>

  • ok :: \<A, B>(value: A) => Result\<A, B>

    create a Ok\ from value

  • err :: \<A, B>(msg: B) => Result\<A, B>

    create a Err\ from value

  • fromString :: \<A, B>(s: string) => Result\<Result\<A, B>, string>

    convert a string generated by stringify the Result\<A, B> to a Result\<A, B>

  • fromObject :: \<A, B>(obj: OkObject\ | ErrObject\) => Result\<Result\<A, B>, string>

    convert a OkObject\ or a ErrObject\ to Result\<A, B>

  • fromPromise :: \<A, B = unknown>(promise: Promise\) => Promise\<Result<A, B>>

    convert a Promise\ to a Promise\<Result\<A, B>>, handle the error by to function

Result instance

  • isOk / isErr :: () => boolean

    indicate the instance is Ok\ / Err\

  • unwrap :: () => A

    returns the contained Ok value, panic on Err.

  • unwrapErr :: () => B

    returns the contained Err value, panic on Ok.

  • unwrapOr :: (defaultValue: A) => A

    returns the contained Ok value or a provided default(if Err).

  • unwrapOrElse :: (f: () => A) => A

    returns the contained Ok value or computes it from the function(if Err).

  • map: \(f: (v: A) => A1)=> Result\<A1, B>

    maps a Result\<A, B> to Result\<A1, B> by applying a function to a contained Ok value, leaving an Err value untouched.

  • mapErr :: \(f: (v: B) => B1) => Result\<A, B1>

    maps a Result\<A, B> to Result\<A, B1> by applying a function to a contained Err value, leaving an Ok value untouched.

  • mapOr: \(f: (v: A) => A1, defaultValue: A1) => A1

    returns the provided default (if Err), or applies a function to the contained value (if Ok) and return it.

  • mapOrElse: \(f: (v: A) => A1, defaultValue: () => A1) => A1

    maps a Result\<A, B> to A1 by applying fallback function default to a contained Err value,

    or function f to a contained Ok value.

  • ap :: (other: Result\<A, B>) => Result\<A1, B>

    apply a Result<(a: A) => A1, B> to a Result<A, B>,

    note that the caller instance should be Result<(a: A) => A1, B>.

  • bind :: \(f: (a: A) => Result\<A1, B>) =>Result\<A1, B>

    apply a function to the Ok value without concerning about Err.

    return Err if Err.

  • match :: \(f: (v: A) => T, g: (v: B) => T) => T

    do something on Result<A, B>, note that you should handle the two cases: Ok\<A, B> and Err\<A, B>

    if return some value, the two handlers should return the same type.

    if you want to return different types, use do instead.

  • do :: \<T, U>(f: (v: A) => T, g: (v: B) => U) => T | U

    do something on Result<A, B>, note that you should handle the two cases: Ok\<A, B> and Err\<A, B>

0.1.10

5 months ago

0.1.11

5 months ago

0.1.9

5 months ago

0.1.8

6 months ago

0.1.7

6 months ago

0.1.6

6 months ago

0.1.5

6 months ago

0.1.4

6 months ago

0.1.3

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago