UNPKG

4.17 kBJavaScriptView Raw
1/** @module conversions */
2const { either, fnOrValue } = require('../logic');
3const { curry } = require('../fp');
4const { isNumber, isObject } = require('../types');
5
6/** Generic function. Parses a value using a parser function, then
7 * evaluates the result with an evaluator function. <sup>(curried)</sup>
8 *
9 * If the parser throws any exception or the evaluator fails the default value is returned.
10 * @argument {function} parser parse function to transform the data input
11 * @argument {function} evaluator evaluates the output of the parser
12 * @argument {*} defaultValue value/function to be returned/executed if it fails
13 * @argument {*} data any kind of data that we want to parse
14 * @example
15 * jsonOr = parseOr(JSON.parse,isObject);
16 * jsonOr(0,{}) // -> {}
17 * jsonOr(0,null) // -> 0
18 * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
19 * @see [Curring](https://en.wikipedia.org/wiki/Currying)
20 * @returns {*} parsed value or default one
21 * @method
22 */
23const parseOr = curry((parser, evaluator, defaultValue, idata) =>
24 either(data => {
25 const res = parser(data);
26 return typeof evaluator === 'function' && evaluator(res) ? res : fnOrValue(defaultValue, data);
27 }, defaultValue)(idata)
28);
29
30/** Converts passed data into a JSON or returns the default value on failure. <sup>(curried)</sup>
31 * @argument {Object|Function} defaultValue default value or function to be returned if it fails.
32 * @argument {*} data any kind of data that we want to parse as JSON
33 * @example
34 * jsonOr(-1,"sfdjl") // -> -1
35 * jsonOr(() => throw Errot("Ups!"),"sfdjl") // -> Error: Ups!
36 * jsonOr(-1)('{"a":1}') // -> {a:1}
37 * @see [Curring](https://en.wikipedia.org/wiki/Currying)
38 * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
39 * @returns {Object|*} Parsed value or the default one.
40 * @method
41 */
42const jsonOr = parseOr(JSON.parse, isObject);
43/** Converts passed data to float or returns the default value on failure. <sup>(curried)</sup>
44 * @argument {Object|Function} defaultValue default value or function to be returned if it fails.
45 * @argument {*} data any kind of data that we want to parse as float
46 * @example
47 * floatOr(-1,"x33x") // -> -1
48 * floatOr(() => throw Errot("Ups!"),"x33x") // -> Error: Ups!
49 * floatOr(-1)('45.553') // -> 45.553
50 * @see [Curring](https://en.wikipedia.org/wiki/Currying)
51 * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
52 * @returns {Float|*} Parsed value or the default one.
53 * @method
54 */
55const floatOr = parseOr(parseFloat, isNumber);
56/** Converts passed data to int or returns the default value on failure. <sup>(curried)</sup>
57 * @argument {Object|Function} defaultValue default value or function to be returned if it fails.
58 * @argument {*} data any kind of data that we want to parse as int
59 * @example
60 * intOr(-1,"x33x") // -> -1
61 * intOr(() => throw Errot("Ups!"),"x33x") // -> Error: Ups!
62 * intOr(-1)('45.553') // -> 45
63 * @see [Curring](https://en.wikipedia.org/wiki/Currying)
64 * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
65 * @returns {Int|*} Parsed value or the default one.
66 * @method
67 */
68const intOr = parseOr(num => parseInt(`${num}`, 10), isNumber);
69
70/** Fixes the number of decimals of a float.
71 * Returns the default value if non numeric value passed.<sup>(curried)</sup>
72 * @argument {Int} decimals number of decimals to fix.
73 * @argument {Function|*} defaultValue value to be returned if non number received
74 * @argument {Float} num float number that we want to fix it's decimals.
75 * @example
76 * setPrecisionOr(1,"fail", 3.44) // -> 3.4
77 * setPrecisionOr(1,"fail", null) // -> "fail"
78 * setPrecisionOr(0,"fail", 3.44) // -> 3
79 * @see [Curring](https://en.wikipedia.org/wiki/Currying)
80 * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
81 * @returns {Float}
82 * @method
83 */
84const setPrecisionOr = curry((decimals, defaultValue, num) => (isNumber(num) ? Number(num.toFixed(decimals)) : fnOrValue(defaultValue, num)));
85
86module.exports = { parseOr, jsonOr, floatOr, intOr, setPrecisionOr };