@bpwme/either
v1.0.1
Published
Easy typed errors
Readme
either-utils
Usage
const ok = success(42)
const fail = error('Something went wrong')
// Type guards
if (isSuccess(ok)) {
console.log('Data:', ok.data)
}
if (isError(fail)) {
console.error('Error:', fail.error)
}
// Map success
const mapped = mapSuccess(ok, n => n * 2)
// => { success: true, data: 84, error: null }
const skipped = mapSuccess(fail, n => n * 2)
// => { success: false, error: 'Something went wrong', data: null }
// Chain (flatMap) success
const chained = chainSuccess(ok, n =>
n > 0 ? success(n + 1) : error('Invalid')
)
// => { success: true, data: 43 }
const chainedError = chainSuccess(ok, n =>
error('Force fail')
)
// => { success: false, error: 'Force fail' }