Skip to content

Commit

Permalink
Merge branch 'master' into breaking/const-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsoft committed Oct 27, 2018
2 parents f8fb5da + f7641f3 commit 75da592
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/src/pages/docs/functions/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ exercise some discipline here to not mutate.
`crocks/Result/tryCatch`

```haskell
tryCatch :: (a -> b) -> a -> Result e b
tryCatch :: ((*) -> b) -> (*) -> Result e b
```

Typical try-catch blocks are very imperative in their usage. This `tryCatch`
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/docs/functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ need to account for for the rest of your flow.
| [`setPath`][setpath] | <code>[ String &#124; Integer ] -> a -> (Object &#124; Array) -> (Object &#124; Array)</code> | `crocks/helpers/setPath` |
| [`tap`][tap] | `(a -> b) -> a -> a` | `crocks/helpers/tap` |
| [`toPairs`][topairs] | `Object -> List (Pair String a)` | `crocks/Pair/toPairs` |
| [`tryCatch`][trycatch] | `(a -> b) -> a -> Result e b` | `crocks/Result/tryCatch` |
| [`tryCatch`][trycatch] | `((*) -> b) -> (*) -> Result e b` | `crocks/Result/tryCatch` |
| [`unary`][unary] | `((*) -> b) -> a -> b` | `crocks/helpers/unary` |
| [`unit`][unit] | `() -> undefined` | `crocks/helpers/unit` |
| [`unsetPath`][unsetpath] | <code>[ String &#124; Integer] -> (Object &#124; Array) -> (Object &#124; Array)</code> | `crocks/helpers/unsetPath` |
Expand Down
8 changes: 6 additions & 2 deletions src/Result/tryCatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ function tryCatch(fn) {
throw new TypeError('tryCatch: Function required for first argument')
}

return function(x) {
try { return Ok(fn(x)) }
const safe = function() {
try { return Ok(fn.apply(this, arguments)) }
catch(e) { return Err(e) }
}

Object.defineProperty(safe, 'length', { value: fn.length })

return safe
}

module.exports = curry(tryCatch)
10 changes: 9 additions & 1 deletion src/Result/tryCatch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const bindFunc = helpers.bindFunc

const Result = require('.')
const isFunction = require('../core/isFunction')
const equals = require('../core/equals')
const isSameType = require('../core/isSameType')
const unit = require('../core/_unit')

Expand Down Expand Up @@ -43,7 +44,6 @@ test('tryCatch functionality', t => {

const f = x => x
const g = () => { throw new Error(msg) }

const extract =
either(identity, constant('Ok'))

Expand All @@ -59,5 +59,13 @@ test('tryCatch functionality', t => {
t.equals(good, 'Ok', 'returns an Ok when no error')
t.equals(bad.message, msg, 'returns an Err with error on error')

const x = (a, b, c) => ({ a, b, c })

const noncurried = tryCatch(x, 1,2,3).either(identity, identity)
const curried = tryCatch(x, 1)(2)(3).either(identity, identity)

t.equals(equals(noncurried, { a: 1, b: 2, c: 3 }), true, 'preserves the arity of the passed function')
t.equals(equals(curried, { a: 1, b: 2, c: 3 }), true, 'returned function is curried')

t.end()
})

0 comments on commit 75da592

Please sign in to comment.