Merge branch 'master' of https://github.com/Chalarangelo/30-seconds-of-code into patch-1
This commit is contained in:
80
README.md
80
README.md
@ -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)
|
||||||
@ -103,6 +104,7 @@
|
|||||||
* [`objectFromPairs`](#objectfrompairs)
|
* [`objectFromPairs`](#objectfrompairs)
|
||||||
* [`objectToPairs`](#objecttopairs)
|
* [`objectToPairs`](#objecttopairs)
|
||||||
* [`shallowClone`](#shallowclone)
|
* [`shallowClone`](#shallowclone)
|
||||||
|
* [`truthCheckCollection`](#truthcheckcollection)
|
||||||
|
|
||||||
### String
|
### String
|
||||||
* [`anagrams`](#anagrams)
|
* [`anagrams`](#anagrams)
|
||||||
@ -116,6 +118,8 @@
|
|||||||
* [`truncateString`](#truncatestring)
|
* [`truncateString`](#truncatestring)
|
||||||
|
|
||||||
### Utility
|
### Utility
|
||||||
|
* [`coalesce`](#coalesce)
|
||||||
|
* [`coalesceFactory`](#coalescefactory)
|
||||||
* [`extendHex`](#extendhex)
|
* [`extendHex`](#extendhex)
|
||||||
* [`getType`](#gettype)
|
* [`getType`](#gettype)
|
||||||
* [`hexToRGB`](#hextorgb)
|
* [`hexToRGB`](#hextorgb)
|
||||||
@ -191,7 +195,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 +366,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 +419,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 +575,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 +1325,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) => {
|
||||||
@ -1366,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)
|
[⬆ back to top](#table-of-contents)
|
||||||
## String
|
## String
|
||||||
|
|
||||||
@ -1436,7 +1470,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 = '_') =>
|
||||||
@ -1510,6 +1544,33 @@ const truncateString = (str, num) =>
|
|||||||
[⬆ back to top](#table-of-contents)
|
[⬆ back to top](#table-of-contents)
|
||||||
## Utility
|
## 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
|
### extendHex
|
||||||
|
|
||||||
Extends a 3-digit color code to a 6-digit color code.
|
Extends a 3-digit color code to a 6-digit color code.
|
||||||
@ -1529,7 +1590,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 =>
|
||||||
@ -1549,9 +1610,8 @@ Use bitwise right-shift operator and mask bits with `&` (and) operator to conver
|
|||||||
const hexToRgb = hex => {
|
const hexToRgb = hex => {
|
||||||
const extendHex = shortHex =>
|
const extendHex = shortHex =>
|
||||||
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
|
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
|
||||||
return hex.slice(1).length==3 ?
|
const extendedHex = hex.slice(hex.startsWith('#') ? 1 : 0).length === 3 ? extendHex(hex) : hex;
|
||||||
`rgb(${parseInt(extendHex(hex).slice(1), 16) >> 16}, ${(parseInt(extendHex(hex).slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendHex(hex).slice(1), 16) & 0x0000ff})`:
|
return `rgb(${parseInt(extendedHex.slice(1), 16) >> 16}, ${(parseInt(extendedHex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendedHex.slice(1), 16) & 0x0000ff})`;
|
||||||
`rgb(${parseInt(hex.slice(1), 16) >> 16}, ${(parseInt(hex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(hex.slice(1), 16) & 0x0000ff})`;
|
|
||||||
}
|
}
|
||||||
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
|
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
|
||||||
// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
|
// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
|
||||||
|
|||||||
@ -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>
|
||||||
@ -126,6 +127,7 @@
|
|||||||
<a class="sublink-1" href="#objectfrompairs">objectFromPairs</a>
|
<a class="sublink-1" href="#objectfrompairs">objectFromPairs</a>
|
||||||
<a class="sublink-1" href="#objecttopairs">objectToPairs</a>
|
<a class="sublink-1" href="#objecttopairs">objectToPairs</a>
|
||||||
<a class="sublink-1" href="#shallowclone">shallowClone</a>
|
<a class="sublink-1" href="#shallowclone">shallowClone</a>
|
||||||
|
<a class="sublink-1" href="#truthcheckcollection">truthCheckCollection</a>
|
||||||
|
|
||||||
<h3>String
|
<h3>String
|
||||||
</h3><a class="sublink-1" href="#anagrams">anagrams</a>
|
</h3><a class="sublink-1" href="#anagrams">anagrams</a>
|
||||||
@ -139,7 +141,9 @@
|
|||||||
<a class="sublink-1" href="#truncatestring">truncateString</a>
|
<a class="sublink-1" href="#truncatestring">truncateString</a>
|
||||||
|
|
||||||
<h3>Utility
|
<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="#gettype">getType</a>
|
||||||
<a class="sublink-1" href="#hextorgb">hexToRGB</a>
|
<a class="sublink-1" href="#hextorgb">hexToRGB</a>
|
||||||
<a class="sublink-1" href="#isarray">isArray</a>
|
<a class="sublink-1" href="#isarray">isArray</a>
|
||||||
@ -184,7 +188,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]) -> [ 1, 2, 3, 'a', 's', 34 ]
|
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 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) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
|
<pre><code class="language-js">const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
|
||||||
// countOccurrences([1,1,2,1,2,3], 1) -> 3
|
// countOccurrences([1,1,2,1,2,3], 1) -> 3
|
||||||
@ -271,7 +275,7 @@ Use <code>Array.reduce()</code> to create an object, where the keys are produced
|
|||||||
// initial([1,2,3]) -> [1,2]
|
// initial([1,2,3]) -> [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) =>
|
<pre><code class="language-js">const initializeArrayWithRange = (end, start = 0) =>
|
||||||
@ -297,6 +301,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 => arr[arr.length - 1];
|
<pre><code class="language-js">const last = arr => arr[arr.length - 1];
|
||||||
// last([1,2,3]) -> 3
|
// last([1,2,3]) -> 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) =>
|
||||||
|
(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 }
|
||||||
|
*/
|
||||||
|
</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 +386,7 @@ This method also works with strings.</p>
|
|||||||
// tail([1]) -> [1]
|
// tail([1]) -> [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) => arr.slice(0, n);
|
<pre><code class="language-js">const take = (arr, n = 1) => arr.slice(0, n);
|
||||||
// take([1, 2, 3], 5) -> [1, 2, 3]
|
// take([1, 2, 3], 5) -> [1, 2, 3]
|
||||||
@ -419,7 +433,7 @@ If lengths of the argument-arrays vary, <code>undefined</code> is used where no
|
|||||||
document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight;
|
document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight;
|
||||||
// bottomVisible() -> true
|
// bottomVisible() -> 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 = () => window.location.href;
|
<pre><code class="language-js">const currentURL = () => window.location.href;
|
||||||
@ -797,7 +811,7 @@ console.log(arr) // -> ['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) => {
|
<pre><code class="language-js">const cleanObj = (obj, keysToKeep = [], childIndicator) => {
|
||||||
Object.keys(obj).forEach(key => {
|
Object.keys(obj).forEach(key => {
|
||||||
if (key === childIndicator) {
|
if (key === childIndicator) {
|
||||||
@ -835,6 +849,12 @@ const b = shallowClone(a);
|
|||||||
a === b -> false
|
a === b -> false
|
||||||
*/
|
*/
|
||||||
</code></pre>
|
</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></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">
|
<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>
|
<p>Generates all anagrams of a string (contains duplicates).</p>
|
||||||
@ -873,7 +893,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 = '_') =>
|
<pre><code class="language-js">const fromCamelCase = (str, separator = '_') =>
|
||||||
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();
|
||||||
@ -914,7 +934,20 @@ Return the string truncated to the desired length, with <code>...</code> appende
|
|||||||
// truncateString('boomerang', 7) -> 'boom...'
|
// truncateString('boomerang', 7) -> 'boom...'
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div></div><br/><h2 style="text-align:center;">Utility</h2>
|
</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>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.
|
<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>
|
<code>Array.slice()</code> is used to remove <code>#</code> from string start since it's added once.</p>
|
||||||
@ -925,7 +958,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, "undefined" or "null" if value is undefined or null</p>
|
<p>Returns lowercased constructor name of value, "undefined" or "null" if value is undefined or null</p>
|
||||||
<pre><code class="language-js">const getType = v =>
|
<pre><code class="language-js">const getType = v =>
|
||||||
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])) -> "set"
|
// getType(new Set([1,2,3])) -> "set"
|
||||||
@ -936,9 +969,8 @@ Return the string truncated to the desired length, with <code>...</code> appende
|
|||||||
<pre><code class="language-js">const hexToRgb = hex => {
|
<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('');
|
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
|
||||||
return hex.slice(1).length==3 ?
|
const extendedHex = hex.slice(hex.startsWith('#') ? 1 : 0).length === 3 ? extendHex(hex) : hex;
|
||||||
`rgb(${parseInt(extendHex(hex).slice(1), 16) >> 16}, ${(parseInt(extendHex(hex).slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendHex(hex).slice(1), 16) & 0x0000ff})`:
|
return `rgb(${parseInt(extendedHex.slice(1), 16) >> 16}, ${(parseInt(extendedHex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendedHex.slice(1), 16) & 0x0000ff})`;
|
||||||
`rgb(${parseInt(hex.slice(1), 16) >> 16}, ${(parseInt(hex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(hex.slice(1), 16) & 0x0000ff})`;
|
|
||||||
}
|
}
|
||||||
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
|
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
|
||||||
// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
|
// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
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) => {
|
||||||
|
|||||||
10
snippets/coalesce.md
Normal file
10
snippets/coalesce.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
### 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") -> ""
|
||||||
|
```
|
||||||
11
snippets/coalesceFactory.md
Normal file
11
snippets/coalesceFactory.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
### 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"
|
||||||
|
```
|
||||||
@ -1,6 +1,6 @@
|
|||||||
### 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.
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
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 = '_') =>
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
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 =>
|
||||||
|
|||||||
@ -8,9 +8,8 @@ Use bitwise right-shift operator and mask bits with `&` (and) operator to conver
|
|||||||
const hexToRgb = hex => {
|
const hexToRgb = hex => {
|
||||||
const extendHex = shortHex =>
|
const extendHex = shortHex =>
|
||||||
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
|
'#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
|
||||||
return hex.slice(1).length==3 ?
|
const extendedHex = hex.slice(hex.startsWith('#') ? 1 : 0).length === 3 ? extendHex(hex) : hex;
|
||||||
`rgb(${parseInt(extendHex(hex).slice(1), 16) >> 16}, ${(parseInt(extendHex(hex).slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendHex(hex).slice(1), 16) & 0x0000ff})`:
|
return `rgb(${parseInt(extendedHex.slice(1), 16) >> 16}, ${(parseInt(extendedHex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendedHex.slice(1), 16) & 0x0000ff})`;
|
||||||
`rgb(${parseInt(hex.slice(1), 16) >> 16}, ${(parseInt(hex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(hex.slice(1), 16) & 0x0000ff})`;
|
|
||||||
}
|
}
|
||||||
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
|
// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
|
||||||
// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
|
// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### 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`.
|
||||||
|
|||||||
14
snippets/mapObject.md
Normal file
14
snippets/mapObject.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
### 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 }
|
||||||
|
*/
|
||||||
|
```
|
||||||
@ -1,6 +1,6 @@
|
|||||||
### 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.
|
||||||
|
|
||||||
|
|||||||
10
snippets/truthCheckCollection.md
Normal file
10
snippets/truthCheckCollection.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
### 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
|
||||||
|
```
|
||||||
@ -9,11 +9,13 @@ capitalizeEveryWord:string
|
|||||||
chainAsync:function
|
chainAsync:function
|
||||||
chunk:array
|
chunk:array
|
||||||
cleanObj:object
|
cleanObj:object
|
||||||
|
coalesce:utility
|
||||||
|
coalesceFactory:utility
|
||||||
collatz:math
|
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 +59,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
|
||||||
@ -95,6 +98,7 @@ toCamelCase:string
|
|||||||
toEnglishDate:date
|
toEnglishDate:date
|
||||||
toOrdinalSuffix:utility
|
toOrdinalSuffix:utility
|
||||||
truncateString:string
|
truncateString:string
|
||||||
|
truthCheckCollection:object
|
||||||
union:array
|
union:array
|
||||||
UUIDGenerator:utility
|
UUIDGenerator:utility
|
||||||
validateEmail:utility
|
validateEmail:utility
|
||||||
|
|||||||
Reference in New Issue
Block a user