UNPKG

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