Skip to content
Carlos De Dios edited this page Oct 20, 2015 · 1 revision

Description

Wrapper whose only purpose is to check whether a value is valid or invalid according to the constraints that are passed to it.

Could work as replacement for if/else.

Example

/* Regular Approach */

let v = getRandomValue();
let r = 0;

if (v % 20 === 0) {
  r = v;
} else {
  r = v % 20;
}

return r;

/* Validator Approach */
import { Validator, id } from 'functional-programming-utilities';

return Validator
  .from(getRandomValue())
  .where(v => v % 20 === 0)
  .match({
    valid: id,
    invalid: v => v % 20
  });

Clone this wiki locally