Skip to content

Releases: musicq/unwrapit

2.4.1

20 May 00:36
Compare
Choose a tag to compare

New Features

  • New API match is added to handle Result value with ease.
let paser = wrap(JSON.parse)

let json = parser.parse('{"a": 1}').match(
  Ok: v => v,
  Err: e => ({error: e})
)

Full Changelog: 2.3.0...2.4.1

2.3.0

28 Oct 09:42
Compare
Choose a tag to compare

New Features

  • unwrapOrElse API
  • mapErr API
  • wrap function type inference enhancement

2.2.0

12 Jun 01:30
Compare
Choose a tag to compare

New Features

  • support for wrapping an asynchronous function

Before if we want to wrap an async value, we need to get the promise value first, then pass it to the wrap function, like this

const v = Promise.resolve(1)

wrap(v).unwrap()

But this is not that convenient if we want to just wrap an async function so that others can use it without the need to wrap it explicitly. Not the wrap function accepts async function.

import {wrap} from 'unwrapit'

const fetchWrapper = wrap(fetch)
const ret = (await fetchWrapper('www.google.com')).unwrap()
const json = await ret.json())

Now you can share a wrapped async function with others, and it will be wrapped with Result types naturally.

2.1.0

18 May 16:56
Compare
Choose a tag to compare

New Features

  • Introducing the toWrap operator for users of rxjs.

Now if you are using rxjs and want to use wrap function to wrap the result of a pipe into Result type, you can directly use toWrap operator.

import { from, map } from 'rxjs'
import { toWrap } from 'unwrapit'

from([1, 2, 3])
  .pipe(
    map((x) => {
      if (x % 2 === 0) throw new Error(`num ${x} is even.`)
      return x
    }),
    toWrap()
  )
  .subscribe((x) => {
    if (!x.ok) return console.error(x.error) // Error: num 2 is even.
    console.log(x.value)
  })

Normally, it is recommended to place the toWrap operator as the last operator in your pipe function to ensure that it can catch all errors. However, you can also position it based on your specific requirements.

2.0.0

18 May 06:43
Compare
Choose a tag to compare

Changes

Won't exit by default in Node

  1. Error won't exit the program by default, instead, it will throw an error

In v1.x, when you try to unwrap a Result that is an error, the program will exit by default in Node. In most cases or a strong system, you may want to handle the error by yourself.

In v2, you can set a customized panic function by using the function setPanic. When an error occurs, it will call your customized panic function, then you can handle it somewhere.

class MyError extends Error {}

setPanic((msg: string) => {
  throw new MyError(msg)
})

However, you can still exit the program by specifying the option panic: true.

err('some error').unwrap({panic: true})

NOTE that you should implement the exit behavior by yourself if you are using customized panic function.

1.2.0

08 Apr 11:29
Compare
Choose a tag to compare
  • Add unwrapOr
  • Add expect