UNPKG

4.97 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: fp/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: fp/index.js</h1>
30 </header>
31 <article>
32 <pre class="prettyprint linenums"><code>/** @module fp **/
33
34/** Reducer function used by &#x60;pipe&#x60; and &#x60;compose&#x60; functions to compose sync and async functions
35 * @argument {function|Promise} chain a chain of functions or promises
36 * @argument {function|Promise} func a new function or promise to add to the chain
37 * @method
38 */
39const mixCompose &#x3D; (chain, func) &#x3D;&gt; (chain instanceof Promise || typeof chain.then &#x3D;&#x3D;&#x3D; &#x27;function&#x27; ? chain.then(func) : func(chain));
40
41/** Compose regular functions or promises generating a final function.
42 * - Compose works from left to right.
43 * - If you compose one single promise the final result will be a promise too.
44 * - You can only compose functions with the same arity.
45 * @argument {function|Promise} arguments N number of functions or promises.
46 * @returns {function|Promise} function or Promise that execute the all composed ones.
47 * @example
48 * const sum3 &#x3D; a &#x3D;&gt; a+3
49 * const mult2 &#x3D; a &#x3D;&gt; a*2
50 * const sum2Async &#x3D; a &#x3D;&gt; Promise.resolve(a+2) // Simulate async response
51 *
52 * const sumAndMult &#x3D; pipe(sum3,mult2);
53 * sumAndMult(1) // -&gt; (1+3)*2 &#x3D; 8
54 *
55 * const sumAndMultAsync &#x3D; pipe(sum3,mult2,sum2Async);
56 * await sumAndMultAsync(1) // -&gt; ((1+3)*2)+2 &#x3D; 10
57 * @method
58 */
59const pipe &#x3D; (...fn) &#x3D;&gt; input &#x3D;&gt; fn.reduce(mixCompose, input);
60
61/** Compose regular functions or promises generating a final function.
62 * - Compose works from right to left.
63 * - If you compose one single promise the final result will be a promise too.
64 * - You can only compose functions with the same arity.
65 * @argument {function|Promise} arguments N number of functions or promises.
66 * @returns {function|Promise} function or Promise that execute the all composed ones.
67 * @example
68 * const sum3 &#x3D; a &#x3D;&gt; a+3
69 * const mult2 &#x3D; a &#x3D;&gt; a*2
70 * const sum2Async &#x3D; a &#x3D;&gt; Promise.resolve(a+2) // Simulate async response
71 *
72 * const sumAndMult &#x3D; compose(sum3,mult2);
73 * sumAndMult(1) // -&gt; (1*2)+3 &#x3D; 5
74 *
75 * const sumAndMultAsync &#x3D; compose(sum3,mult2,sum2Async);
76 * await sumAndMultAsync(1) // -&gt; ((1+2)*2)+3 &#x3D; 9
77 * @method
78 */
79const compose &#x3D; (...fn) &#x3D;&gt; input &#x3D;&gt; fn.reduceRight(mixCompose, input);
80
81/** Currify any function allowing the partial application of its arguments
82 * @argument {function} function function with at least two arguments
83 * @returns {function} curried function.
84 * @example
85 * const sum &#x3D; curry((a,b) &#x3D; a+b);
86 * const sum3 &#x3D; sum(3);
87 * sum3(3) // -&gt; 6
88 * sum(3,3) // -&gt; 6
89 * sum(3)(3) // -&gt; 6
90 * @method
91 */
92const curry &#x3D; f &#x3D;&gt; {
93 if (typeof f !&#x3D;&#x3D; &#x27;function&#x27;) {
94 throw new Error(&#x60;curry requires a function, [${typeof f}] passed&#x60;);
95 }
96 return function currify(...arg) {
97 const args &#x3D; Array.prototype.slice.call(arg);
98 return args.length &gt;&#x3D; f.length ? f(...args) : currify.bind(null, ...args);
99 };
100};
101module.exports &#x3D; { pipe, compose, curry };
102</code></pre>
103 </article>
104 </div>
105 </div>
106 <nav id="jsdoc-toc-nav" role="navigation"></nav>
107 </div>
108 </div>
109 <footer id="jsdoc-footer" class="jsdoc-footer">
110 <div id="jsdoc-footer-container">
111 <p>
112 Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc</a> 3.6.2 on June 17, 2019.
113 </p>
114 </div>
115 </footer>
116 <script src="scripts/jquery.min.js"></script>
117 <script src="scripts/jquery.cookie.js"></script>
118 <script src="scripts/tree.jquery.js"></script>
119 <script src="scripts/prettify.js"></script>
120 <script src="scripts/jsdoc-toc.js"></script>
121 <script src="scripts/linenumber.js"></script>
122 <script src="scripts/scrollanchor.js"></script>
123</body>
124
125</html>
\No newline at end of file