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