Build
This commit is contained in:
@ -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 -> 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) => (collection.every(obj => obj[pre]));
|
||||
// truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> 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) -> '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) => args.find(_ => ![undefined, null].includes(_))
|
||||
// coalesce(null,undefined,"",NaN, "Waldo") -> ""
|
||||
</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 => (...args) => args.find(valid);
|
||||
// const customCoalesce = coalesceFactory(_ => ![null, undefined, "", NaN].includes(_))
|
||||
// customCoalesce(undefined, null, NaN, "", "Waldo") //-> "Waldo"
|
||||
</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>&</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 => {
|
||||
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)'
|
||||
</code></pre>
|
||||
|
||||
Reference in New Issue
Block a user