@justinc/to-validation

1.0.0 • Public • Published

Install with:

npm i @justinc/to-validation

Exports the following function:

function toValidation (isValidation, isFailureLike, toFailure, toSuccess, v) {
  if (isValidation(v)) return v
  if (isFailureLike(v)) return toFailure(v)
  return toSuccess(v)
}

with one catch, the function can be invoked using "FPO-style" named-arguments (and it's multi-curried).

This means you can invoke it like this:

import toValidation from '@justinc/to-validation'
let toV = toValidation({
  isValidation: v => {
    // your logic for determining if v is a Validation, typically:
    // Validation.hasInstance(v),
    // see tests.
  },
  isFailureLike: (v) => {
    // your logic for determining if v is failure like
  }
})

Having supplied the isValidation and isFailureLike parameters (using named-arguments), we only need to supply three more - toFailure, toSuccess, and v (note how we can split up how we want to supply them since the function is multi-curried):

let onlyVLeft = toV({
  toFailure: (v) => // how you'd change v to a Validation.Failure
  toSuccess: () => // how you'd change v to a Validation.Success
})

let asValidation = v => onlyVLeft({ v })

This package is experimental. Only really meant to DRY some code.

Package Sidebar

Install

npm i @justinc/to-validation

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • justinc