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

View File

@ -53,6 +53,7 @@
<a class="sublink-1" href="#initializearraywithvalues">initializeArrayWithValues</a>
<a class="sublink-1" href="#intersection">intersection</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="#pick">pick</a>
<a class="sublink-1" href="#pull">pull</a>
@ -70,7 +71,7 @@
<h3>Browser
</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="#getscrollposition">getScrollPosition</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 ]
</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">
<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>
<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
@ -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]
</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">
<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.
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;
@ -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];
// last([1,2,3]) -&gt; 3
</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">
<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.
@ -372,7 +383,7 @@ This method also works with strings.</p>
// tail([1]) -&gt; [1]
</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">
<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>
<pre><code class="language-js">const take = (arr, n = 1) =&gt; arr.slice(0, n);
// 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;
// bottomVisible() -&gt; true
</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>Use <code>window.location.href</code> to get current URL.</p>
<pre><code class="language-js">const currentURL = () =&gt; window.location.href;
@ -780,7 +791,7 @@ const JSONToFile = (obj, filename) =&gt; fs.writeFile(`${filename}.json`, JSON.s
<p>Returns an array of lines from the specified file.</p>
<p>Use <code>readFileSync</code> function in <code>fs</code> node package to create a <code>Buffer</code> from a file.
convert buffer to string using <code>toString(encoding)</code> function.
creating an array from contents of file by <code>split</code>ing file content line by line(each <code>\n</code>).</p>
creating an array from contents of file by <code>split</code>ing file content line by line (each <code>\n</code>).</p>
<pre><code class="language-js">const fs = require('fs');
const readFileLines = filename =&gt; fs.readFileSync(filename).toString('UTF8').split('\n');
/*
@ -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">
<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.
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; {
Object.keys(obj).forEach(key =&gt; {
if (key === childIndicator) {
@ -852,7 +863,7 @@ Base cases are for string <code>length</code> equal to <code>2</code> or <code>1
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="capitalize">Capitalize</h3></div><div class="section double-padded">
<p>Capitalizes the first letter of a string.</p>
<p>Use destructuring and <code>toUpperCase()</code> to capitalize first letter, <code>...rest</code> to get array of characters after first letter and then <code>Array.join('')</code> to make it a string again.
Omit the <code>lowerRest</code> parameter to keep the rest of the string intact, or set it to <code>true</code> to convert to lower case.</p>
Omit the <code>lowerRest</code> parameter to keep the rest of the string intact, or set it to <code>true</code> to convert to lowercase.</p>
<pre><code class="language-js">const capitalize = ([first,...rest], lowerRest = false) =&gt;
first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
// capitalize('myName') -&gt; 'MyName'
@ -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">
<p>Converts a string from camelcase.</p>
<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;
str.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
.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>
</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 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;
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
// getType(new Set([1,2,3])) -&gt; &quot;set&quot;