1 | <!doctype html>
|
2 | <html>
|
3 |
|
4 | <head>
|
5 | <meta name="generator" content="JSDoc 3.6.2">
|
6 | <meta charset="utf-8">
|
7 | <title>keyu 2.0.0 » Source: conversions/index.js</title>
|
8 | <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Karla:400,400i,700,700i" type="text/css">
|
9 | <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Noto+Serif:400,400i,700,700i" type="text/css">
|
10 | <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Inconsolata:500" type="text/css">
|
11 | <link href="css/baseline.css" rel="stylesheet">
|
12 | </head>
|
13 |
|
14 | <body onload="prettyPrint()">
|
15 | <nav id="jsdoc-navbar" role="navigation" class="jsdoc-navbar">
|
16 | <div id="jsdoc-navbar-container">
|
17 | <div id="jsdoc-navbar-content">
|
18 | <a href="index.html" class="jsdoc-navbar-package-name">keyu 2.<wbr>0.<wbr>0</a>
|
19 | </div>
|
20 | </div>
|
21 | </nav>
|
22 | <div id="jsdoc-body-container">
|
23 | <div id="jsdoc-content">
|
24 | <div id="jsdoc-content-container">
|
25 | <div id="jsdoc-banner" role="banner">
|
26 | </div>
|
27 | <div id="jsdoc-main" role="main">
|
28 | <header class="page-header">
|
29 | <h1>Source: conversions/index.js</h1>
|
30 | </header>
|
31 | <article>
|
32 | <pre class="prettyprint linenums"><code>/** @module conversions */
|
33 | const { either, fnOrValue } = require('../logic');
|
34 | const { curry } = require('../fp');
|
35 | const { isNumber, isObject } = require('../types');
|
36 |
|
37 | /** Generic function. Parses a value using a parser function, then
|
38 | * evaluates the result with an evaluator function. &lt;sup>(curried)&lt;/sup>
|
39 | *
|
40 | * If the parser throws any exception or the evaluator fails the default value is returned.
|
41 | * @argument {function} parser parse function to transform the data input
|
42 | * @argument {function} evaluator evaluates the output of the parser
|
43 | * @argument {*} defaultValue value/function to be returned/executed if it fails
|
44 | * @argument {*} data any kind of data that we want to parse
|
45 | * @example
|
46 | * jsonOr = parseOr(JSON.parse,isObject);
|
47 | * jsonOr(0,{}) // -> {}
|
48 | * jsonOr(0,null) // -> 0
|
49 | * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
|
50 | * @see [Curring](https://en.wikipedia.org/wiki/Currying)
|
51 | * @returns {*} parsed value or default one
|
52 | * @method
|
53 | */
|
54 | const parseOr = curry((parser, evaluator, defaultValue, idata) =>
|
55 | either(data => {
|
56 | const res = parser(data);
|
57 | return typeof evaluator === 'function' &amp;&amp; evaluator(res) ? res : fnOrValue(defaultValue, data);
|
58 | }, defaultValue)(idata)
|
59 | );
|
60 |
|
61 | /** Converts passed data into a JSON or returns the default value on failure. &lt;sup>(curried)&lt;/sup>
|
62 | * @argument {Object|Function} defaultValue default value or function to be returned if it fails.
|
63 | * @argument {*} data any kind of data that we want to parse as JSON
|
64 | * @example
|
65 | * jsonOr(-1,"sfdjl") // -> -1
|
66 | * jsonOr(() => throw Errot("Ups!"),"sfdjl") // -> Error: Ups!
|
67 | * jsonOr(-1)('{"a":1}') // -> {a:1}
|
68 | * @see [Curring](https://en.wikipedia.org/wiki/Currying)
|
69 | * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
|
70 | * @returns {Object|*} Parsed value or the default one.
|
71 | * @method
|
72 | */
|
73 | const jsonOr = parseOr(JSON.parse, isObject);
|
74 | /** Converts passed data to float or returns the default value on failure. &lt;sup>(curried)&lt;/sup>
|
75 | * @argument {Object|Function} defaultValue default value or function to be returned if it fails.
|
76 | * @argument {*} data any kind of data that we want to parse as float
|
77 | * @example
|
78 | * floatOr(-1,"x33x") // -> -1
|
79 | * floatOr(() => throw Errot("Ups!"),"x33x") // -> Error: Ups!
|
80 | * floatOr(-1)('45.553') // -> 45.553
|
81 | * @see [Curring](https://en.wikipedia.org/wiki/Currying)
|
82 | * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
|
83 | * @returns {Float|*} Parsed value or the default one.
|
84 | * @method
|
85 | */
|
86 | const floatOr = parseOr(parseFloat, isNumber);
|
87 | /** Converts passed data to int or returns the default value on failure. &lt;sup>(curried)&lt;/sup>
|
88 | * @argument {Object|Function} defaultValue default value or function to be returned if it fails.
|
89 | * @argument {*} data any kind of data that we want to parse as int
|
90 | * @example
|
91 | * intOr(-1,"x33x") // -> -1
|
92 | * intOr(() => throw Errot("Ups!"),"x33x") // -> Error: Ups!
|
93 | * intOr(-1)('45.553') // -> 45
|
94 | * @see [Curring](https://en.wikipedia.org/wiki/Currying)
|
95 | * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
|
96 | * @returns {Int|*} Parsed value or the default one.
|
97 | * @method
|
98 | */
|
99 | const intOr = parseOr(num => parseInt(`${num}`, 10), isNumber);
|
100 |
|
101 | /** Fixes the number of decimals of a float.
|
102 | * Returns the default value if non numeric value passed.&lt;sup>(curried)&lt;/sup>
|
103 | * @argument {Int} decimals number of decimals to fix.
|
104 | * @argument {Function|*} defaultValue value to be returned if non number received
|
105 | * @argument {Float} num float number that we want to fix it's decimals.
|
106 | * @example
|
107 | * setPrecisionOr(1,"fail", 3.44) // -> 3.4
|
108 | * setPrecisionOr(1,"fail", null) // -> "fail"
|
109 | * setPrecisionOr(0,"fail", 3.44) // -> 3
|
110 | * @see [Curring](https://en.wikipedia.org/wiki/Currying)
|
111 | * @see [conversionsTest.js](https://github.com/nerac/keyu/blob/master/test/conversionsTest.js)
|
112 | * @returns {Float}
|
113 | * @method
|
114 | */
|
115 | const setPrecisionOr = curry((decimals, defaultValue, num) => (isNumber(num) ? Number(num.toFixed(decimals)) : fnOrValue(defaultValue, num)));
|
116 |
|
117 | module.exports = { parseOr, jsonOr, floatOr, intOr, setPrecisionOr };
|
118 | </code></pre>
|
119 | </article>
|
120 | </div>
|
121 | </div>
|
122 | <nav id="jsdoc-toc-nav" role="navigation"></nav>
|
123 | </div>
|
124 | </div>
|
125 | <footer id="jsdoc-footer" class="jsdoc-footer">
|
126 | <div id="jsdoc-footer-container">
|
127 | <p>
|
128 | Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc</a> 3.6.2 on June 17, 2019.
|
129 | </p>
|
130 | </div>
|
131 | </footer>
|
132 | <script src="scripts/jquery.min.js"></script>
|
133 | <script src="scripts/jquery.cookie.js"></script>
|
134 | <script src="scripts/tree.jquery.js"></script>
|
135 | <script src="scripts/prettify.js"></script>
|
136 | <script src="scripts/jsdoc-toc.js"></script>
|
137 | <script src="scripts/linenumber.js"></script>
|
138 | <script src="scripts/scrollanchor.js"></script>
|
139 | </body>
|
140 |
|
141 | </html> |
\ | No newline at end of file |