This commit is contained in:
Angelos Chalaris
2017-12-18 12:14:24 +02:00
parent 6c449763e3
commit f951e3f896
3 changed files with 50 additions and 20 deletions

View File

@ -30,6 +30,7 @@
* [`initializeArrayWithValues`](#initializearraywithvalues) * [`initializeArrayWithValues`](#initializearraywithvalues)
* [`intersection`](#intersection) * [`intersection`](#intersection)
* [`last`](#last) * [`last`](#last)
* [`mapObject`](#mapobject)
* [`nthElement`](#nthelement) * [`nthElement`](#nthelement)
* [`pick`](#pick) * [`pick`](#pick)
* [`pull`](#pull) * [`pull`](#pull)
@ -47,7 +48,7 @@
### Browser ### Browser
* [`bottomVisible`](#bottomvisible) * [`bottomVisible`](#bottomvisible)
* [`current-URL`](#current-url) * [`currentURL`](#currenturl)
* [`elementIsVisibleInViewport`](#elementisvisibleinviewport) * [`elementIsVisibleInViewport`](#elementisvisibleinviewport)
* [`getScrollPosition`](#getscrollposition) * [`getScrollPosition`](#getscrollposition)
* [`getURLParameters`](#geturlparameters) * [`getURLParameters`](#geturlparameters)
@ -191,7 +192,7 @@ const compact = (arr) => arr.filter(Boolean);
### countOccurrences ### countOccurrences
Counts the occurences of a value in an array. Counts the occurrences of a value in an array.
Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array. Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array.
@ -362,7 +363,7 @@ const initial = arr => arr.slice(0, -1);
### initializeArrayWithRange ### initializeArrayWithRange
Initialized an array containing the numbers in the specified range. Initializes an array containing the numbers in the specified range.
Use `Array(end-start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. Use `Array(end-start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range.
You can omit `start` to use a default value of `0`. You can omit `start` to use a default value of `0`.
@ -415,6 +416,23 @@ const last = arr => arr[arr.length - 1];
[⬆ back to top](#table-of-contents) [⬆ back to top](#table-of-contents)
### mapObject
Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.
Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new `Array` to stor the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).
```js
const mapObject = (arr, fn) =>
(a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( );
/*
const squareIt = arr => mapObject(arr, a => a*a)
squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 }
*/
```
[⬆ back to top](#table-of-contents)
### nthElement ### nthElement
Returns the nth element of an array. Returns the nth element of an array.
@ -554,7 +572,7 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr;
### take ### take
Returns an array with n elements removed from the beggining. Returns an array with n elements removed from the beginning.
Use `Array.slice()` to create a slice of the array with `n` elements taken from the beginning. Use `Array.slice()` to create a slice of the array with `n` elements taken from the beginning.
@ -1304,7 +1322,7 @@ const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').spl
Removes any properties except the ones specified from a JSON object. Removes any properties except the ones specified from a JSON object.
Use `Object.keys()` method to loop over given json object and deleting keys that are not `include`d in given array. Use `Object.keys()` method to loop over given json object and deleting keys that are not `include`d in given array.
also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too. Also if you give it a special key (`childIndicator`) it will search deeply inside it to apply function to inner objects too.
```js ```js
const cleanObj = (obj, keysToKeep = [], childIndicator) => { const cleanObj = (obj, keysToKeep = [], childIndicator) => {
@ -1436,7 +1454,7 @@ const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
Converts a string from camelcase. Converts a string from camelcase.
Use `replace()` to remove underscores, hyphens and spaces and convert words to camelcase. Use `replace()` to remove underscores, hyphens and spaces and convert words to camelcase.
Omit the scond argument to use a default separator of `_`. Omit the second argument to use a default separator of `_`.
```js ```js
const fromCamelCase = (str, separator = '_') => const fromCamelCase = (str, separator = '_') =>
@ -1529,7 +1547,7 @@ const extendHex = shortHex =>
Returns the native type of a value. Returns the native type of a value.
Returns lower-cased constructor name of value, "undefined" or "null" if value is undefined or null Returns lowercased constructor name of value, "undefined" or "null" if value is undefined or null
```js ```js
const getType = v => const getType = v =>

View File

@ -53,6 +53,7 @@
<a class="sublink-1" href="#initializearraywithvalues">initializeArrayWithValues</a> <a class="sublink-1" href="#initializearraywithvalues">initializeArrayWithValues</a>
<a class="sublink-1" href="#intersection">intersection</a> <a class="sublink-1" href="#intersection">intersection</a>
<a class="sublink-1" href="#last">last</a> <a class="sublink-1" href="#last">last</a>
<a class="sublink-1" href="#mapobject">mapObject</a>
<a class="sublink-1" href="#nthelement">nthElement</a> <a class="sublink-1" href="#nthelement">nthElement</a>
<a class="sublink-1" href="#pick">pick</a> <a class="sublink-1" href="#pick">pick</a>
<a class="sublink-1" href="#pull">pull</a> <a class="sublink-1" href="#pull">pull</a>
@ -70,7 +71,7 @@
<h3>Browser <h3>Browser
</h3><a class="sublink-1" href="#bottomvisible">bottomVisible</a> </h3><a class="sublink-1" href="#bottomvisible">bottomVisible</a>
<a class="sublink-1" href="#current-url">current-URL</a> <a class="sublink-1" href="#currenturl">currentURL</a>
<a class="sublink-1" href="#elementisvisibleinviewport">elementIsVisibleInViewport</a> <a class="sublink-1" href="#elementisvisibleinviewport">elementIsVisibleInViewport</a>
<a class="sublink-1" href="#getscrollposition">getScrollPosition</a> <a class="sublink-1" href="#getscrollposition">getScrollPosition</a>
<a class="sublink-1" href="#geturlparameters">getURLParameters</a> <a class="sublink-1" href="#geturlparameters">getURLParameters</a>
@ -184,7 +185,7 @@ If the original array can't be split evenly, the final chunk will contain the re
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -&gt; [ 1, 2, 3, 'a', 's', 34 ] // compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -&gt; [ 1, 2, 3, 'a', 's', 34 ]
</code></pre> </code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="countoccurrences">countOccurrences</h3></div><div class="section double-padded"> </div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="countoccurrences">countOccurrences</h3></div><div class="section double-padded">
<p>Counts the occurences of a value in an array.</p> <p>Counts the occurrences of a value in an array.</p>
<p>Use <code>Array.reduce()</code> to increment a counter each time you encounter the specific value inside the array.</p> <p>Use <code>Array.reduce()</code> to increment a counter each time you encounter the specific value inside the array.</p>
<pre><code class="language-js">const countOccurrences = (arr, value) =&gt; arr.reduce((a, v) =&gt; v === value ? a + 1 : a + 0, 0); <pre><code class="language-js">const countOccurrences = (arr, value) =&gt; arr.reduce((a, v) =&gt; v === value ? a + 1 : a + 0, 0);
// countOccurrences([1,1,2,1,2,3], 1) -&gt; 3 // countOccurrences([1,1,2,1,2,3], 1) -&gt; 3
@ -271,7 +272,7 @@ Use <code>Array.reduce()</code> to create an object, where the keys are produced
// initial([1,2,3]) -&gt; [1,2] // initial([1,2,3]) -&gt; [1,2]
</code></pre> </code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="initializearraywithrange">initializeArrayWithRange</h3></div><div class="section double-padded"> </div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="initializearraywithrange">initializeArrayWithRange</h3></div><div class="section double-padded">
<p>Initialized an array containing the numbers in the specified range.</p> <p>Initializes an array containing the numbers in the specified range.</p>
<p>Use <code>Array(end-start)</code> to create an array of the desired length, <code>Array.map()</code> to fill with the desired values in a range. <p>Use <code>Array(end-start)</code> to create an array of the desired length, <code>Array.map()</code> to fill with the desired values in a range.
You can omit <code>start</code> to use a default value of <code>0</code>.</p> You can omit <code>start</code> to use a default value of <code>0</code>.</p>
<pre><code class="language-js">const initializeArrayWithRange = (end, start = 0) =&gt; <pre><code class="language-js">const initializeArrayWithRange = (end, start = 0) =&gt;
@ -297,6 +298,16 @@ You can omit <code>value</code> to use a default value of <code>0</code>.</p>
<pre><code class="language-js">const last = arr =&gt; arr[arr.length - 1]; <pre><code class="language-js">const last = arr =&gt; arr[arr.length - 1];
// last([1,2,3]) -&gt; 3 // last([1,2,3]) -&gt; 3
</code></pre> </code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="mapobject">mapObject</h3></div><div class="section double-padded">
<p>Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.</p>
<p>Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new <code>Array</code> to stor the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).</p>
<pre><code class="language-js">const mapObject = (arr, fn) =&gt;
(a =&gt; (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) =&gt; (acc[val] = a[1][ind], acc), {}) )) ( );
/*
const squareIt = arr =&gt; mapObject(arr, a =&gt; a*a)
squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 }
*/
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="nthelement">nthElement</h3></div><div class="section double-padded"> </div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="nthelement">nthElement</h3></div><div class="section double-padded">
<p>Returns the nth element of an array.</p> <p>Returns the nth element of an array.</p>
<p>Use <code>Array.slice()</code> to get an array containing the nth element at the first place. <p>Use <code>Array.slice()</code> to get an array containing the nth element at the first place.
@ -372,7 +383,7 @@ This method also works with strings.</p>
// tail([1]) -&gt; [1] // tail([1]) -&gt; [1]
</code></pre> </code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="take">take</h3></div><div class="section double-padded"> </div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="take">take</h3></div><div class="section double-padded">
<p>Returns an array with n elements removed from the beggining.</p> <p>Returns an array with n elements removed from the beginning.</p>
<p>Use <code>Array.slice()</code> to create a slice of the array with <code>n</code> elements taken from the beginning.</p> <p>Use <code>Array.slice()</code> to create a slice of the array with <code>n</code> elements taken from the beginning.</p>
<pre><code class="language-js">const take = (arr, n = 1) =&gt; arr.slice(0, n); <pre><code class="language-js">const take = (arr, n = 1) =&gt; arr.slice(0, n);
// take([1, 2, 3], 5) -&gt; [1, 2, 3] // take([1, 2, 3], 5) -&gt; [1, 2, 3]
@ -419,7 +430,7 @@ If lengths of the argument-arrays vary, <code>undefined</code> is used where no
document.documentElement.clientHeight + window.scrollY &gt;= document.documentElement.scrollHeight || document.documentElement.clientHeight; document.documentElement.clientHeight + window.scrollY &gt;= document.documentElement.scrollHeight || document.documentElement.clientHeight;
// bottomVisible() -&gt; true // bottomVisible() -&gt; true
</code></pre> </code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="current-url">currentURL</h3></div><div class="section double-padded"> </div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="currenturl">currentURL</h3></div><div class="section double-padded">
<p>Returns the current URL.</p> <p>Returns the current URL.</p>
<p>Use <code>window.location.href</code> to get current URL.</p> <p>Use <code>window.location.href</code> to get current URL.</p>
<pre><code class="language-js">const currentURL = () =&gt; window.location.href; <pre><code class="language-js">const currentURL = () =&gt; window.location.href;
@ -797,7 +808,7 @@ console.log(arr) // -&gt; ['line1', 'line2', 'line3']
<div class="card fluid"><div class="section double-padded"><h3 id="cleanobj">cleanObj</h3></div><div class="section double-padded"> <div class="card fluid"><div class="section double-padded"><h3 id="cleanobj">cleanObj</h3></div><div class="section double-padded">
<p>Removes any properties except the ones specified from a JSON object.</p> <p>Removes any properties except the ones specified from a JSON object.</p>
<p>Use <code>Object.keys()</code> method to loop over given json object and deleting keys that are not <code>include</code>d in given array. <p>Use <code>Object.keys()</code> method to loop over given json object and deleting keys that are not <code>include</code>d in given array.
also if you give it a special key(<code>childIndicator</code>) it will search deeply inside it to apply function to inner objects too.</p> Also if you give it a special key (<code>childIndicator</code>) it will search deeply inside it to apply function to inner objects too.</p>
<pre><code class="language-js">const cleanObj = (obj, keysToKeep = [], childIndicator) =&gt; { <pre><code class="language-js">const cleanObj = (obj, keysToKeep = [], childIndicator) =&gt; {
Object.keys(obj).forEach(key =&gt; { Object.keys(obj).forEach(key =&gt; {
if (key === childIndicator) { if (key === childIndicator) {
@ -873,7 +884,7 @@ Omit the <code>lowerRest</code> parameter to keep the rest of the string intact,
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="fromcamelcase">fromCamelCase</h3></div><div class="section double-padded"> </div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="fromcamelcase">fromCamelCase</h3></div><div class="section double-padded">
<p>Converts a string from camelcase.</p> <p>Converts a string from camelcase.</p>
<p>Use <code>replace()</code> to remove underscores, hyphens and spaces and convert words to camelcase. <p>Use <code>replace()</code> to remove underscores, hyphens and spaces and convert words to camelcase.
Omit the scond argument to use a default separator of <code>_</code>.</p> Omit the second argument to use a default separator of <code>_</code>.</p>
<pre><code class="language-js">const fromCamelCase = (str, separator = '_') =&gt; <pre><code class="language-js">const fromCamelCase = (str, separator = '_') =&gt;
str.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2') str.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2').toLowerCase(); .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2').toLowerCase();
@ -925,7 +936,7 @@ Return the string truncated to the desired length, with <code>...</code> appende
</code></pre> </code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="gettype">getType</h3></div><div class="section double-padded"> </div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="gettype">getType</h3></div><div class="section double-padded">
<p>Returns the native type of a value.</p> <p>Returns the native type of a value.</p>
<p>Returns lower-cased constructor name of value, &quot;undefined&quot; or &quot;null&quot; if value is undefined or null</p> <p>Returns lowercased constructor name of value, &quot;undefined&quot; or &quot;null&quot; if value is undefined or null</p>
<pre><code class="language-js">const getType = v =&gt; <pre><code class="language-js">const getType = v =&gt;
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase(); v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
// getType(new Set([1,2,3])) -&gt; &quot;set&quot; // getType(new Set([1,2,3])) -&gt; &quot;set&quot;

View File

@ -13,7 +13,7 @@ collatz:math
compact:array compact:array
compose:function compose:function
countOccurrences:array countOccurrences:array
current-URL:browser currentURL:browser
curry:function curry:function
deepFlatten:array deepFlatten:array
difference:array difference:array
@ -57,6 +57,7 @@ JSONToDate:date
JSONToFile:node JSONToFile:node
last:array last:array
lcm:math lcm:math
mapObject:array
median:math median:math
nthElement:array nthElement:array
objectFromPairs:object objectFromPairs:object