UNPKG

6.62 kBHTMLView Raw
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 &raquo; 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 */
33const { either, fnOrValue } &#x3D; require(&#x27;../logic&#x27;);
34const { curry } &#x3D; require(&#x27;../fp&#x27;);
35const { isNumber, isObject } &#x3D; require(&#x27;../types&#x27;);
36
37/** Generic function. Parses a value using a parser function, then
38 * evaluates the result with an evaluator function. &amp;lt;sup&gt;(curried)&amp;lt;/sup&gt;
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 &#x3D; parseOr(JSON.parse,isObject);
47 * jsonOr(0,{}) // -&gt; {}
48 * jsonOr(0,null) // -&gt; 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 */
54const parseOr &#x3D; curry((parser, evaluator, defaultValue, idata) &#x3D;&gt;
55 either(data &#x3D;&gt; {
56 const res &#x3D; parser(data);
57 return typeof evaluator &#x3D;&#x3D;&#x3D; &#x27;function&#x27; &amp;amp;&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. &amp;lt;sup&gt;(curried)&amp;lt;/sup&gt;
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,&quot;sfdjl&quot;) // -&gt; -1
66 * jsonOr(() &#x3D;&gt; throw Errot(&quot;Ups!&quot;),&quot;sfdjl&quot;) // -&gt; Error: Ups!
67 * jsonOr(-1)(&#x27;{&quot;a&quot;:1}&#x27;) // -&gt; {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 */
73const jsonOr &#x3D; parseOr(JSON.parse, isObject);
74/** Converts passed data to float or returns the default value on failure. &amp;lt;sup&gt;(curried)&amp;lt;/sup&gt;
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,&quot;x33x&quot;) // -&gt; -1
79 * floatOr(() &#x3D;&gt; throw Errot(&quot;Ups!&quot;),&quot;x33x&quot;) // -&gt; Error: Ups!
80 * floatOr(-1)(&#x27;45.553&#x27;) // -&gt; 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 */
86const floatOr &#x3D; parseOr(parseFloat, isNumber);
87/** Converts passed data to int or returns the default value on failure. &amp;lt;sup&gt;(curried)&amp;lt;/sup&gt;
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,&quot;x33x&quot;) // -&gt; -1
92 * intOr(() &#x3D;&gt; throw Errot(&quot;Ups!&quot;),&quot;x33x&quot;) // -&gt; Error: Ups!
93 * intOr(-1)(&#x27;45.553&#x27;) // -&gt; 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 */
99const intOr &#x3D; parseOr(num &#x3D;&gt; parseInt(&#x60;${num}&#x60;, 10), isNumber);
100
101/** Fixes the number of decimals of a float.
102 * Returns the default value if non numeric value passed.&amp;lt;sup&gt;(curried)&amp;lt;/sup&gt;
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&#x27;s decimals.
106 * @example
107 * setPrecisionOr(1,&quot;fail&quot;, 3.44) // -&gt; 3.4
108 * setPrecisionOr(1,&quot;fail&quot;, null) // -&gt; &quot;fail&quot;
109 * setPrecisionOr(0,&quot;fail&quot;, 3.44) // -&gt; 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 */
115const setPrecisionOr &#x3D; curry((decimals, defaultValue, num) &#x3D;&gt; (isNumber(num) ? Number(num.toFixed(decimals)) : fnOrValue(defaultValue, num)));
116
117module.exports &#x3D; { 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