1 | const { either, fnOrValue, identity } = require('../logic');
|
2 | const { curry } = require('../fp');
|
3 |
|
4 | const 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 |
|
10 | const jsonOr = parseOr(JSON.parse);
|
11 | const floatOr = parseOr(parseFloat);
|
12 | const intOr = parseOr(parseInt);
|
13 |
|
14 | const setPrecision = curry((value, num) => +num.toFixed(value));
|
15 |
|
16 | module.exports = { parseOr, jsonOr, floatOr, intOr, setPrecision };
|