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: collections/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: collections/index.js</h1>
|
30 | </header>
|
31 | <article>
|
32 | <pre class="prettyprint linenums"><code>/** @module collections */
|
33 | const { curry } = require('../fp');
|
34 |
|
35 | /** Reduces an array/object based on the reduction function
|
36 | * and the initialization value passed
|
37 | * @argument {function} fn Reduce function
|
38 | * @argument {*} init Initial value to accumulate
|
39 | * @argument {Array|Object} collection iterable collection to traverse
|
40 | * @example
|
41 | * //Arrays
|
42 | * reduce((a,b)=> a+b,0,[1,2,3]) // -> 6
|
43 | * reduce((a,b)=> a.concat(b+1),[],[1,2,3]) // -> [2,3,4]
|
44 | * //Objects
|
45 | * reduce((a,v,k)=> ({..a,[k],v+1}),{},{a:1,b:2}) // -> {a:2,b:3}
|
46 | * @returns {*}
|
47 | * @see [collectionsTest.js](https://github.com/nerac/keyu/blob/master/test/collectionsTest.js)
|
48 | * @method
|
49 | * */
|
50 | const reduce = curry((fn, init, collection) => {
|
51 | if (Array.isArray(collection)) {
|
52 | return collection.reduce(fn, init);
|
53 | }
|
54 | return Object.entries(collection).reduce((acc, [k, v]) => fn(acc, v, k), init);
|
55 | });
|
56 |
|
57 | /** Maps over an array/object applying the passed function
|
58 | * @argument {function} fn Reduce function
|
59 | * @argument {Array|Object} collection iterable collection to traverse
|
60 | * @example
|
61 | * //Arrays
|
62 | * map(x => x+1, [1,2,3]) // -> [2,3,4]
|
63 | * //Objects
|
64 | * map(x => x+1, {a:1,b:2}) // -> {a:2,b:3}
|
65 | * @returns {Array|Object}
|
66 | * @see [collectionsTest.js](https://github.com/nerac/keyu/blob/master/test/collectionsTest.js)
|
67 | * @method
|
68 | * */
|
69 | const map = curry((fn, collection) => {
|
70 | if (Array.isArray(collection)) {
|
71 | return collection.map(fn);
|
72 | }
|
73 | return reduce((acc, v, k) => ({ ...acc, [k]: fn(v, k) }), {}, collection);
|
74 | });
|
75 |
|
76 | /** Filters an array/object based on the boolean evaluation of the passed function.
|
77 | * @argument {function} fn Reduce function
|
78 | * @argument {Array|Object} collection iterable collection to traverse
|
79 | * @example
|
80 | * //Arrays
|
81 | * filter(x => x > 1, [1,2,3]) // -> [2,3]
|
82 | * //Objects
|
83 | * filter(x => x > 1, {a:1,b:2}) // -> {b:2}
|
84 | * @returns {Array|Object}
|
85 | * @see [collectionsTest.js](https://github.com/nerac/keyu/blob/master/test/collectionsTest.js)
|
86 | * @method
|
87 | * */
|
88 | const filter = curry((fn, collection) => {
|
89 | if (Array.isArray(collection)) {
|
90 | return collection.filter(fn);
|
91 | }
|
92 | return reduce((acc, v, k) => (fn(v, k) &amp;&amp; { ...acc, [k]: v }) || {}, {}, collection);
|
93 | });
|
94 |
|
95 | module.exports = { map, filter, reduce };
|
96 | </code></pre>
|
97 | </article>
|
98 | </div>
|
99 | </div>
|
100 | <nav id="jsdoc-toc-nav" role="navigation"></nav>
|
101 | </div>
|
102 | </div>
|
103 | <footer id="jsdoc-footer" class="jsdoc-footer">
|
104 | <div id="jsdoc-footer-container">
|
105 | <p>
|
106 | Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc</a> 3.6.2 on June 17, 2019.
|
107 | </p>
|
108 | </div>
|
109 | </footer>
|
110 | <script src="scripts/jquery.min.js"></script>
|
111 | <script src="scripts/jquery.cookie.js"></script>
|
112 | <script src="scripts/tree.jquery.js"></script>
|
113 | <script src="scripts/prettify.js"></script>
|
114 | <script src="scripts/jsdoc-toc.js"></script>
|
115 | <script src="scripts/linenumber.js"></script>
|
116 | <script src="scripts/scrollanchor.js"></script>
|
117 | </body>
|
118 |
|
119 | </html> |
\ | No newline at end of file |