crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
36 lines (26 loc) • 1 kB
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var Result = require('.')
var Either = require('../core/types').proxy('Either')
var curry = require('../core/curry')
var isFunction = require('../core/isFunction')
var isSameType = require('../core/isSameType')
var applyTransform = function (either) { return either.either(Result.Err, Result.Ok); }
// eitherToResult : Either e a -> Result e a
// eitherToResult : (a -> Either e b) -> a -> Result e b
function eitherToResult(either) {
if(isFunction(either)) {
return function(x) {
var m = either(x)
if(!isSameType(Either, m)) {
throw new TypeError('eitherToResult: Either returning function required')
}
return applyTransform(m)
}
}
if(isSameType(Either, either)) {
return applyTransform(either)
}
throw new TypeError('eitherToResult: Either or Either returning function required')
}
module.exports = curry(eitherToResult)