UNPKG

506 BJavaScriptView Raw
1const { either, fnOrValue } = require('../logic');
2const { curry } = require('../fp');
3
4const parseOr = curry((parser, def) =>
5 either(data => {
6 let res = parser(data);
7 return res || (res === 0 ? res : fnOrValue(def, data));
8 }, def)
9);
10
11const jsonOr = parseOr(JSON.parse);
12const floatOr = parseOr(parseFloat);
13const intOr = parseOr(num => parseInt(`${num}`));
14
15const setPrecision = curry((value, num) => +num.toFixed(value));
16
17module.exports = { parseOr, jsonOr, floatOr, intOr, setPrecision };