-
Notifications
You must be signed in to change notification settings - Fork 0
6. Validator
Carlos De Dios edited this page Oct 20, 2015
·
1 revision
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.
/* 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
});