The guard
function helps to verify an input parameters and result of your
functions.
You can use it as follow
const fun = guard(guards, fn);
where:
guards
is a list of validator functions, e.g.[g1, g2, gR]
whereg1
is function (guard) for variablex1
, functiong2
forx2
andgR
is guard for result offn
,fn
is your function.
> import * as G from './guards';
> const tenDividedBy = G.guard([G.notZero, G.isLess(4)], (x) => 10 / x);
> tenDividedBy(2) // 5
> tenDividedBy(0) // Error: The value is zero!
> tenDividedBy(5) // Error: The value is less than 4!
> const add = G.guard([G.notZero, G.isLess(1), G.isGreater(3)], (x, y) => x + y);
> add(2, 0) // ... try it yourselves
- all files
npm run test
- single file, e.g. guards.test.js
npm test -t Guard
"devDependencies": {
"check-types": "^11.1.2",
"fast-check": "^1.23.0",
"jest": "25.1.0",
"ramda": "^0.27.0"
}