This commit is contained in:
Angelos Chalaris
2017-12-18 13:06:26 +02:00
parent a23408e99f
commit afc292d706
4 changed files with 78 additions and 23 deletions

View File

@ -104,6 +104,7 @@
* [`objectFromPairs`](#objectfrompairs)
* [`objectToPairs`](#objecttopairs)
* [`shallowClone`](#shallowclone)
* [`truthCheckCollection`](#truthcheckcollection)
### String
* [`anagrams`](#anagrams)
@ -117,6 +118,8 @@
* [`truncateString`](#truncatestring)
### Utility
* [`coalesce`](#coalesce)
* [`coalesceFactory`](#coalescefactory)
* [`extendHex`](#extendhex)
* [`getType`](#gettype)
* [`hexToRGB`](#hextorgb)
@ -1384,6 +1387,19 @@ a === b -> false
*/
```
[⬆ back to top](#table-of-contents)
### truthCheckCollection
Checks if the predicate (second argument) is truthy on all elements of a collection (first argument).
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]));
// truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> true
```
[⬆ back to top](#table-of-contents)
## String
@ -1528,6 +1544,33 @@ const truncateString = (str, num) =>
[⬆ back to top](#table-of-contents)
## Utility
### coalesce
Returns the first non-null/undefined argument.
Use `Array.find()` to return the first non `null`/`undefined` argument.
```js
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_))
// coalesce(null,undefined,"",NaN, "Waldo") -> ""
```
[⬆ back to top](#table-of-contents)
### coalesceFactory
Returns a customized coalesce function that returns the first argument that returns `true` from the provided argument validation function.
Use `Array.find()` to return the first argument that returns `true` from the provided argument validation function.
```js
const coalesceFactory = valid => (...args) => args.find(valid);
// const customCoalesce = coalesceFactory(_ => ![null, undefined, "", NaN].includes(_))
// customCoalesce(undefined, null, NaN, "", "Waldo") //-> "Waldo"
```
[⬆ back to top](#table-of-contents)
### extendHex
Extends a 3-digit color code to a 6-digit color code.
@ -1565,12 +1608,11 @@ Use bitwise right-shift operator and mask bits with `&` (and) operator to conver
```js
const hexToRgb = hex => {
const extendHex = shortHex =>
const extendHex = shortHex =>
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
return hex.slice(1).length==3 ?
`rgb(${parseInt(extendHex(hex).slice(1), 16) >> 16}, ${(parseInt(extendHex(hex).slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendHex(hex).slice(1), 16) & 0x0000ff})`:
`rgb(${parseInt(hex.slice(1), 16) >> 16}, ${(parseInt(hex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(hex.slice(1), 16) & 0x0000ff})`;
}
const extendedHex = hex.slice(hex.startsWith('#') ? 1 : 0).length === 3 ? extendHex(hex) : hex;
return `rgb(${parseInt(extendedHex.slice(1), 16) >> 16}, ${(parseInt(extendedHex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendedHex.slice(1), 16) & 0x0000ff})`;
}
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
```

View File

@ -127,6 +127,7 @@
<a class="sublink-1" href="#objectfrompairs">objectFromPairs</a>
<a class="sublink-1" href="#objecttopairs">objectToPairs</a>
<a class="sublink-1" href="#shallowclone">shallowClone</a>
<a class="sublink-1" href="#truthcheckcollection">truthCheckCollection</a>
<h3>String
</h3><a class="sublink-1" href="#anagrams">anagrams</a>
@ -140,7 +141,9 @@
<a class="sublink-1" href="#truncatestring">truncateString</a>
<h3>Utility
</h3><a class="sublink-1" href="#extendhex">extendHex</a>
</h3><a class="sublink-1" href="#coalesce">coalesce</a>
<a class="sublink-1" href="#coalescefactory">coalesceFactory</a>
<a class="sublink-1" href="#extendhex">extendHex</a>
<a class="sublink-1" href="#gettype">getType</a>
<a class="sublink-1" href="#hextorgb">hexToRGB</a>
<a class="sublink-1" href="#isarray">isArray</a>
@ -846,6 +849,12 @@ const b = shallowClone(a);
a === b -&gt; false
*/
</code></pre>
</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]));
// 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>
<div class="card fluid"><div class="section double-padded"><h3 id="anagrams">anagrams</h3></div><div class="section double-padded">
<p>Generates all anagrams of a string (contains duplicates).</p>
@ -925,7 +934,20 @@ Return the string truncated to the desired length, with <code>...</code> appende
// truncateString('boomerang', 7) -&gt; 'boom...'
</code></pre>
</div></div><br/><h2 style="text-align:center;">Utility</h2>
<div class="card fluid"><div class="section double-padded"><h3 id="extendhex">extendHex</h3></div><div class="section double-padded">
<div class="card fluid"><div class="section double-padded"><h3 id="coalesce">coalesce</h3></div><div class="section double-padded">
<p>Returns the first non-null/undefined argument.</p>
<p>Use <code>Array.find()</code> to return the first non <code>null</code>/<code>undefined</code> argument.</p>
<pre><code class="language-js">const coalesce = (...args) =&gt; args.find(_ =&gt; ![undefined, null].includes(_))
// coalesce(null,undefined,&quot;&quot;,NaN, &quot;Waldo&quot;) -&gt; &quot;&quot;
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="coalescefactory">coalesceFactory</h3></div><div class="section double-padded">
<p>Returns a customized coalesce function that returns the first argument that returns <code>true</code> from the provided argument validation function.</p>
<p>Use <code>Array.find()</code> to return the first argument that returns <code>true</code> from the provided argument validation function.</p>
<pre><code class="language-js">const coalesceFactory = valid =&gt; (...args) =&gt; args.find(valid);
// const customCoalesce = coalesceFactory(_ =&gt; ![null, undefined, &quot;&quot;, NaN].includes(_))
// customCoalesce(undefined, null, NaN, &quot;&quot;, &quot;Waldo&quot;) //-&gt; &quot;Waldo&quot;
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="extendhex">extendHex</h3></div><div class="section double-padded">
<p>Extends a 3-digit color code to a 6-digit color code.</p>
<p>Use <code>Array.map()</code>, <code>split()</code> and <code>Array.join()</code> to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
<code>Array.slice()</code> is used to remove <code>#</code> from string start since it's added once.</p>
@ -945,12 +967,11 @@ Return the string truncated to the desired length, with <code>...</code> appende
<p>Converts a colorcode to a <code>rgb()</code> string.</p>
<p>Use bitwise right-shift operator and mask bits with <code>&amp;</code> (and) operator to convert a hexadecimal color code (prefixed with <code>#</code>) to a string with the RGB values. In case it's a 3-digit-colorcode, do the same with the 6-digit-colorcode extended by the extendHex() function (ref. <code>extendHex</code> snippet)</p>
<pre><code class="language-js">const hexToRgb = hex =&gt; {
const extendHex = shortHex =&gt;
const extendHex = shortHex =&gt;
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x =&gt; x+x).join('');
return hex.slice(1).length==3 ?
`rgb(${parseInt(extendHex(hex).slice(1), 16) &gt;&gt; 16}, ${(parseInt(extendHex(hex).slice(1), 16) &amp; 0x00ff00) &gt;&gt; 8}, ${parseInt(extendHex(hex).slice(1), 16) &amp; 0x0000ff})`:
`rgb(${parseInt(hex.slice(1), 16) &gt;&gt; 16}, ${(parseInt(hex.slice(1), 16) &amp; 0x00ff00) &gt;&gt; 8}, ${parseInt(hex.slice(1), 16) &amp; 0x0000ff})`;
}
const extendedHex = hex.slice(hex.startsWith('#') ? 1 : 0).length === 3 ? extendHex(hex) : hex;
return `rgb(${parseInt(extendedHex.slice(1), 16) &gt;&gt; 16}, ${(parseInt(extendedHex.slice(1), 16) &amp; 0x00ff00) &gt;&gt; 8}, ${parseInt(extendedHex.slice(1), 16) &amp; 0x0000ff})`;
}
// hexToRgb('#27ae60') -&gt; 'rgb(39, 174, 96)'
// hexToRgb('#acd') -&gt; 'rgb(170, 204, 221)'
</code></pre>

View File

@ -1,11 +0,0 @@
### First one to pass truth test
A function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument).
```js
findElement = (arr, func) => {
filterArr = arr.filter(func); //filter array with the function provided
return filterArr[0]; //return the first element that returns true, or undefined if no elements return true
}
// findElement([1, 2, 3, 4], function(num){ return num !== 2 && num !== 1 }); //3 <- first element in array to be passed truth test
```

View File

@ -9,6 +9,8 @@ capitalizeEveryWord:string
chainAsync:function
chunk:array
cleanObj:object
coalesce:utility
coalesceFactory:utility
collatz:math
compact:array
compose:function
@ -96,6 +98,7 @@ toCamelCase:string
toEnglishDate:date
toOrdinalSuffix:utility
truncateString:string
truthCheckCollection:object
union:array
UUIDGenerator:utility
validateEmail:utility