This commit is contained in:
Angelos Chalaris
2017-12-20 11:30:54 +02:00
parent cc7c172e94
commit 321d18d91c
3 changed files with 102 additions and 4 deletions

View File

@ -29,6 +29,7 @@
* [`groupBy`](#groupby)
* [`head`](#head)
* [`initial`](#initial)
* [`initialize2DArray`](#initialize2darray)
* [`initializeArrayWithRange`](#initializearraywithrange)
* [`initializeArrayWithValues`](#initializearraywithvalues)
* [`intersection`](#intersection)
@ -52,6 +53,7 @@
* [`zip`](#zip)
### Browser
* [`arrayToHtmlList`](#arraytohtmllist)
* [`bottomVisible`](#bottomvisible)
* [`currentURL`](#currenturl)
* [`elementIsVisibleInViewport`](#elementisvisibleinviewport)
@ -85,6 +87,7 @@
* [`fibonacci`](#fibonacci)
* [`gcd`](#gcd)
* [`hammingDistance`](#hammingdistance)
* [`isArmstrongNumber`](#isarmstrongnumber)
* [`isDivisible`](#isdivisible)
* [`isEven`](#iseven)
* [`isPrime`](#isprime)
@ -122,6 +125,7 @@
* [`fromCamelCase`](#fromcamelcase)
* [`reverseString`](#reversestring)
* [`sortCharactersInString`](#sortcharactersinstring)
* [`stringToArrayOfWords`](#stringtoarrayofwords)
* [`toCamelCase`](#tocamelcase)
* [`truncateString`](#truncatestring)
@ -400,6 +404,19 @@ const initial = arr => arr.slice(0, -1);
[⬆ back to top](#table-of-contents)
### initialize2DArray
Initializes an 2D array of given width and height and value.
Use `Array.map()` to generate h rows where each is a new array of size w initialize with value. If value is not provided, default to `null`.
```js
const initialize2DArray = (w, h, val = null) => Array(h).fill().map(() => Array(w).fill(val));
// initializeArrayWithRange(2, 2, 0) -> [[0,0], [0,0]]
```
[⬆ back to top](#table-of-contents)
### initializeArrayWithRange
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive.
@ -749,6 +766,19 @@ const zip = (...arrays) => {
[⬆ back to top](#table-of-contents)
## Browser
### arrayToHtmlList
Converts the given array elements into '<li>' tags and appends them to the list of the given id.
Use `Array.map()` and `document.querySelector()` to create a list of html tags.
```js
const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`<li>${item}</li>`);
// arrayToHtmlList(['item 1', 'item 2'],'myListID')
```
[⬆ back to top](#table-of-contents)
### bottomVisible
Returns `true` if the bottom of the page is visible, `false` otherwise.
@ -1190,6 +1220,22 @@ const hammingDistance = (num1, num2) =>
[⬆ back to top](#table-of-contents)
### isArmstrongNumber
Checks if the given number is an armstrong number or not.
Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`.
```js
const isArmstrongNumber = digits =>
( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) );
// isArmstrongNumber(1634) -> true
// isArmstrongNumber(371) -> true
// isArmstrongNumber(56) -> false
```
[⬆ back to top](#table-of-contents)
### isDivisible
Checks if the first numeric argument is divisible by the second one.
@ -1530,7 +1576,7 @@ Checks if the predicate (second argument) is truthy on all elements of a collect
Use `Array.every()` to check if each passed object has the specified property and if it returns a truthy value.
```js
truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre]));
const truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre]));
// truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> true
```
@ -1659,6 +1705,21 @@ const sortCharactersInString = str =>
[⬆ back to top](#table-of-contents)
### stringToArrayOfWords
Converts a given string into an array of words.
Use `String.split()` with a supplied pattern (defaults to non alpha as a regex) to convert to an array of strings. Use `Array.filter()` to remove any empty strings.
Omit the second argument to use the default regex.
```js
const stringToArrayOfWords = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean);
// stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"]
// stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"]
```
[⬆ back to top](#table-of-contents)
### toCamelCase
Converts a string to camelcase.

View File

@ -71,6 +71,7 @@
<a class="sublink-1" href="#groupby">groupBy</a>
<a class="sublink-1" href="#head">head</a>
<a class="sublink-1" href="#initial">initial</a>
<a class="sublink-1" href="#initialize2darray">initialize2DArray</a>
<a class="sublink-1" href="#initializearraywithrange">initializeArrayWithRange</a>
<a class="sublink-1" href="#initializearraywithvalues">initializeArrayWithValues</a>
<a class="sublink-1" href="#intersection">intersection</a>
@ -94,7 +95,8 @@
<a class="sublink-1" href="#zip">zip</a>
<h3>Browser
</h3><a class="sublink-1" href="#bottomvisible">bottomVisible</a>
</h3><a class="sublink-1" href="#arraytohtmllist">arrayToHtmlList</a>
<a class="sublink-1" href="#bottomvisible">bottomVisible</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>
@ -127,6 +129,7 @@
<a class="sublink-1" href="#fibonacci">fibonacci</a>
<a class="sublink-1" href="#gcd">gcd</a>
<a class="sublink-1" href="#hammingdistance">hammingDistance</a>
<a class="sublink-1" href="#isarmstrongnumber">isArmstrongNumber</a>
<a class="sublink-1" href="#isdivisible">isDivisible</a>
<a class="sublink-1" href="#iseven">isEven</a>
<a class="sublink-1" href="#isprime">isPrime</a>
@ -164,6 +167,7 @@
<a class="sublink-1" href="#fromcamelcase">fromCamelCase</a>
<a class="sublink-1" href="#reversestring">reverseString</a>
<a class="sublink-1" href="#sortcharactersinstring">sortCharactersInString</a>
<a class="sublink-1" href="#stringtoarrayofwords">stringToArrayOfWords</a>
<a class="sublink-1" href="#tocamelcase">toCamelCase</a>
<a class="sublink-1" href="#truncatestring">truncateString</a>
@ -315,6 +319,12 @@ Use <code>Array.reduce()</code> to create an object, where the keys are produced
<pre><code class="language-js">const initial = arr =&gt; arr.slice(0, -1);
// initial([1,2,3]) -&gt; [1,2]
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="initialize2darray">initialize2DArray</h3></div><div class="section double-padded">
<p>Initializes an 2D array of given width and height and value.</p>
<p>Use <code>Array.map()</code> to generate h rows where each is a new array of size w initialize with value. If value is not provided, default to <code>null</code>.</p>
<pre><code class="language-js">const initialize2DArray = (w, h, val = null) =&gt; Array(h).fill().map(() =&gt; Array(w).fill(val));
// initializeArrayWithRange(2, 2, 0) -&gt; [[0,0], [0,0]]
</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>Initializes an array containing the numbers in the specified range where <code>start</code> and <code>end</code> are inclusive.</p>
<p>Use <code>Array((end + 1) - start)</code> to create an array of the desired length, <code>Array.map()</code> to fill with the desired values in a range.
@ -515,7 +525,13 @@ If lengths of the argument-arrays vary, <code>undefined</code> is used where no
//zip(['a'], [1, 2], [true, false]); -&gt; [['a', 1, true], [undefined, 2, false]]
</code></pre>
</div></div><br/><h2 style="text-align:center;">Browser</h2>
<div class="card fluid"><div class="section double-padded"><h3 id="bottomvisible">bottomVisible</h3></div><div class="section double-padded">
<div class="card fluid"><div class="section double-padded"><h3 id="arraytohtmllist">arrayToHtmlList</h3></div><div class="section double-padded">
<p>Converts the given array elements into '&lt;li&gt;' tags and appends them to the list of the given id.</p>
<p>Use <code>Array.map()</code> and <code>document.querySelector()</code> to create a list of html tags.</p>
<pre><code class="language-js">const arrayToHtmlList = (arr, listID) =&gt; arr.map(item =&gt; document.querySelector(&quot;#&quot;+listID).innerHTML+=`&lt;li&gt;${item}&lt;/li&gt;`);
// arrayToHtmlList(['item 1', 'item 2'],'myListID')
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="bottomvisible">bottomVisible</h3></div><div class="section double-padded">
<p>Returns <code>true</code> if the bottom of the page is visible, <code>false</code> otherwise.</p>
<p>Use <code>scrollY</code>, <code>scrollHeight</code> and <code>clientHeight</code> to determine if the bottom of the page is visible.</p>
<pre><code class="language-js">const bottomVisible = () =&gt;
@ -766,6 +782,15 @@ Count and return the number of <code>1</code>s in the string, using <code>match(
((num1 ^ num2).toString(2).match(/1/g) || '').length;
// hammingDistance(2,3) -&gt; 1
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="isarmstrongnumber">isArmstrongNumber</h3></div><div class="section double-padded">
<p>Checks if the given number is an armstrong number or not.</p>
<p>Convert the given number into array of digits. Use <code>Math.pow()</code> to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return <code>true</code> otherwise <code>false</code>.</p>
<pre><code class="language-js">const isArmstrongNumber = digits =&gt;
( arr =&gt; arr.reduce( ( a, d ) =&gt; a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) );
// isArmstrongNumber(1634) -&gt; true
// isArmstrongNumber(371) -&gt; true
// isArmstrongNumber(56) -&gt; false
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="isdivisible">isDivisible</h3></div><div class="section double-padded">
<p>Checks if the first numeric argument is divisible by the second one.</p>
<p>Use the modulo operator (<code>%</code>) to check if the remainder is equal to <code>0</code>.</p>
@ -961,7 +986,7 @@ a === b -&gt; false
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="truthcheckcollection">truthCheckCollection</h3></div><div class="section double-padded">
<p>Checks if the predicate (second argument) is truthy on all elements of a collection (first argument).</p>
<p>Use <code>Array.every()</code> to check if each passed object has the specified property and if it returns a truthy value.</p>
<pre><code class="language-js">truthCheckCollection = (collection, pre) =&gt; (collection.every(obj =&gt; obj[pre]));
<pre><code class="language-js">const truthCheckCollection = (collection, pre) =&gt; (collection.every(obj =&gt; obj[pre]));
// truthCheckCollection([{&quot;user&quot;: &quot;Tinky-Winky&quot;, &quot;sex&quot;: &quot;male&quot;}, {&quot;user&quot;: &quot;Dipsy&quot;, &quot;sex&quot;: &quot;male&quot;}], &quot;sex&quot;) -&gt; true
</code></pre>
</div></div><br/><h2 style="text-align:center;">String</h2>
@ -1031,6 +1056,14 @@ Combine characters to get a string using <code>join('')</code>.</p>
str.split('').sort((a, b) =&gt; a.localeCompare(b)).join('');
// sortCharactersInString('cabbage') -&gt; 'aabbceg'
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="stringtoarrayofwords">stringToArrayOfWords</h3></div><div class="section double-padded">
<p>Converts a given string into an array of words.</p>
<p>Use <code>String.split()</code> with a supplied pattern (defaults to non alpha as a regex) to convert to an array of strings. Use <code>Array.filter()</code> to remove any empty strings.
Omit the second argument to use the default regex.</p>
<pre><code class="language-js">const stringToArrayOfWords = (str, pattern = /[^a-zA-Z-]+/) =&gt; str.split(pattern).filter(Boolean);
// stringToArrayOfWords(&quot;I love javaScript!!&quot;) -&gt; [&quot;I&quot;, &quot;love&quot;, &quot;javaScript&quot;]
// stringToArrayOfWords(&quot;python, javaScript &amp; coffee&quot;) -&gt; [&quot;python&quot;, &quot;javaScript&quot;, &quot;coffee&quot;]
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="tocamelcase">toCamelCase</h3></div><div class="section double-padded">
<p>Converts a string to camelcase.</p>
<p>Use <code>replace()</code> to remove underscores, hyphens and spaces and convert words to camelcase.</p>

View File

@ -3,6 +3,7 @@ arrayAverage:math
arrayMax:array
arrayMin:array
arraySum:math
arrayToHtmlList:browser
bottomVisible:browser
capitalize:string
capitalizeEveryWord:string
@ -47,9 +48,11 @@ hammingDistance:math
head:array
hexToRGB:utility
initial:array
initialize2DArray:array
initializeArrayWithRange:array
initializeArrayWithValues:array
intersection:array
isArmstrongNumber:math
isArray:utility
isBoolean:utility
isDivisible:math
@ -96,6 +99,7 @@ sleep:function
sortCharactersInString:string
speechSynthesis:media
standardDeviation:math
stringToArrayOfWords:string
symmetricDifference:array
tail:array
take:array