diff --git a/README.md b/README.md index c5ab3f8ba..5f279d99f 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ * [`initializeArrayWithValues`](#initializearraywithvalues) * [`intersection`](#intersection) * [`last`](#last) +* [`mapObject`](#mapobject) * [`nthElement`](#nthelement) * [`pick`](#pick) * [`pull`](#pull) @@ -47,7 +48,7 @@ ### Browser * [`bottomVisible`](#bottomvisible) -* [`current-URL`](#current-url) +* [`currentURL`](#currenturl) * [`elementIsVisibleInViewport`](#elementisvisibleinviewport) * [`getScrollPosition`](#getscrollposition) * [`getURLParameters`](#geturlparameters) @@ -103,6 +104,7 @@ * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) * [`shallowClone`](#shallowclone) +* [`truthCheckCollection`](#truthcheckcollection) ### String * [`anagrams`](#anagrams) @@ -116,6 +118,8 @@ * [`truncateString`](#truncatestring) ### Utility +* [`coalesce`](#coalesce) +* [`coalesceFactory`](#coalescefactory) * [`extendHex`](#extendhex) * [`getType`](#gettype) * [`hexToRGB`](#hextorgb) @@ -191,7 +195,7 @@ const compact = (arr) => arr.filter(Boolean); ### 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. @@ -362,7 +366,7 @@ const initial = arr => arr.slice(0, -1); ### 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. 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) +### 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 Returns the nth element of an array. @@ -554,7 +575,7 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr; ### 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. @@ -1280,7 +1301,7 @@ Returns an array of lines from the specified file. Use `readFileSync` function in `fs` node package to create a `Buffer` from a file. convert buffer to string using `toString(encoding)` function. -creating an array from contents of file by `split`ing file content line by line(each `\n`). +creating an array from contents of file by `split`ing file content line by line (each `\n`). ```js const fs = require('fs'); @@ -1304,7 +1325,7 @@ const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').spl 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. -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 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) ## String @@ -1394,7 +1428,7 @@ const anagrams = str => { Capitalizes the first letter of a string. Use destructuring and `toUpperCase()` to capitalize first letter, `...rest` to get array of characters after first letter and then `Array.join('')` to make it a string again. -Omit the `lowerRest` parameter to keep the rest of the string intact, or set it to `true` to convert to lower case. +Omit the `lowerRest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase. ```js const capitalize = ([first,...rest], lowerRest = false) => @@ -1436,7 +1470,7 @@ const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); Converts a string from 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 const fromCamelCase = (str, separator = '_') => @@ -1510,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. @@ -1529,7 +1590,7 @@ const extendHex = shortHex => 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 const getType = v => @@ -1547,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)' ``` diff --git a/docs/index.html b/docs/index.html index 2383f3c8c..fc801123a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -53,6 +53,7 @@ initializeArrayWithValues intersection last +mapObject nthElement pick pull @@ -70,7 +71,7 @@

Browser

bottomVisible -current-URL +currentURL elementIsVisibleInViewport getScrollPosition getURLParameters @@ -126,6 +127,7 @@ objectFromPairs objectToPairs shallowClone +truthCheckCollection

String

anagrams @@ -139,7 +141,9 @@ truncateString

Utility -

extendHex +coalesce +coalesceFactory +extendHex getType hexToRGB isArray @@ -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 ]

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.

const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
 // countOccurrences([1,1,2,1,2,3], 1) -> 3
@@ -271,7 +275,7 @@ Use Array.reduce() to create an object, where the keys are produced
 // initial([1,2,3]) -> [1,2]
 

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. You can omit start to use a default value of 0.

const initializeArrayWithRange = (end, start = 0) =>
@@ -297,6 +301,16 @@ You can omit value to use a default value of 0.

const last = arr => arr[arr.length - 1];
 // last([1,2,3]) -> 3
 
+

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).

+
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 }
+*/
+

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. @@ -372,7 +386,7 @@ This method also works with strings.

// tail([1]) -> [1]

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.

const take = (arr, n = 1) => arr.slice(0, n);
 // take([1, 2, 3], 5) -> [1, 2, 3]
@@ -419,7 +433,7 @@ If lengths of the argument-arrays vary, undefined is used where no
   document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight;
 // bottomVisible() -> true
 
-

currentURL

+

currentURL

Returns the current URL.

Use window.location.href to get current URL.

const currentURL = () => window.location.href;
@@ -780,7 +794,7 @@ const JSONToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.s
 

Returns an array of lines from the specified file.

Use readFileSync function in fs node package to create a Buffer from a file. convert buffer to string using toString(encoding) function. -creating an array from contents of file by spliting file content line by line(each \n).

+creating an array from contents of file by spliting file content line by line (each \n).

const fs = require('fs');
 const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').split('\n');
 /*
@@ -797,7 +811,7 @@ console.log(arr) // -> ['line1', 'line2', 'line3']
 

cleanObj

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 included 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.

const cleanObj = (obj, keysToKeep = [], childIndicator) => {
   Object.keys(obj).forEach(key => {
     if (key === childIndicator) {
@@ -835,6 +849,12 @@ const b = shallowClone(a);
 a === b -> false
 */
 
+

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.

+
truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre]));
+// truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> true
+

String

anagrams

Generates all anagrams of a string (contains duplicates).

@@ -852,7 +872,7 @@ Base cases are for string length equal to 2 or 1

Capitalize

Capitalizes the first letter of a string.

Use destructuring and toUpperCase() to capitalize first letter, ...rest to get array of characters after first letter and then Array.join('') to make it a string again. -Omit the lowerRest parameter to keep the rest of the string intact, or set it to true to convert to lower case.

+Omit the lowerRest parameter to keep the rest of the string intact, or set it to true to convert to lowercase.

const capitalize = ([first,...rest], lowerRest = false) =>
   first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
 // capitalize('myName') -> 'MyName'
@@ -873,7 +893,7 @@ Omit the lowerRest parameter to keep the rest of the string intact,
 

fromCamelCase

Converts a string from 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 _.

const fromCamelCase = (str, separator = '_') =>
   str.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
     .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 ... appende
 // truncateString('boomerang', 7) -> 'boom...'
 

Utility

-

extendHex

+

coalesce

+

Returns the first non-null/undefined argument.

+

Use Array.find() to return the first non null/undefined argument.

+
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_))
+// coalesce(null,undefined,"",NaN, "Waldo") -> ""
+
+

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.

+
const coalesceFactory = valid => (...args) => args.find(valid);
+// const customCoalesce = coalesceFactory(_ => ![null, undefined, "", NaN].includes(_))
+// customCoalesce(undefined, null, NaN, "", "Waldo") //-> "Waldo"
+
+

extendHex

Extends a 3-digit color code to a 6-digit color code.

Use Array.map(), split() and Array.join() to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form. Array.slice() is used to remove # from string start since it's added once.

@@ -925,7 +958,7 @@ Return the string truncated to the desired length, with ... appende

getType

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

const getType = v =>
   v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
 // getType(new Set([1,2,3])) -> "set"
@@ -934,12 +967,11 @@ Return the string truncated to the desired length, with ... appende
 

Converts a colorcode to a rgb() string.

Use bitwise right-shift operator and mask bits with & (and) operator to convert a hexadecimal color code (prefixed with #) 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. extendHex snippet)

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)'
 
diff --git a/snippets/capitalize.md b/snippets/capitalize.md index f29c933d2..d085a3034 100644 --- a/snippets/capitalize.md +++ b/snippets/capitalize.md @@ -3,7 +3,7 @@ Capitalizes the first letter of a string. Use destructuring and `toUpperCase()` to capitalize first letter, `...rest` to get array of characters after first letter and then `Array.join('')` to make it a string again. -Omit the `lowerRest` parameter to keep the rest of the string intact, or set it to `true` to convert to lower case. +Omit the `lowerRest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase. ```js const capitalize = ([first,...rest], lowerRest = false) => diff --git a/snippets/cleanObj.md b/snippets/cleanObj.md index 8ba4058ed..75a8e900b 100644 --- a/snippets/cleanObj.md +++ b/snippets/cleanObj.md @@ -3,7 +3,7 @@ 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. -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 const cleanObj = (obj, keysToKeep = [], childIndicator) => { diff --git a/snippets/coalesce.md b/snippets/coalesce.md new file mode 100644 index 000000000..22271dc68 --- /dev/null +++ b/snippets/coalesce.md @@ -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") -> "" +``` diff --git a/snippets/coalesceFactory.md b/snippets/coalesceFactory.md new file mode 100644 index 000000000..4b9a78924 --- /dev/null +++ b/snippets/coalesceFactory.md @@ -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" +``` diff --git a/snippets/countOccurrences.md b/snippets/countOccurrences.md index be28947f0..f55de0a62 100644 --- a/snippets/countOccurrences.md +++ b/snippets/countOccurrences.md @@ -1,6 +1,6 @@ ### 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. diff --git a/snippets/current-URL.md b/snippets/currentURL.md similarity index 100% rename from snippets/current-URL.md rename to snippets/currentURL.md diff --git a/snippets/fromCamelCase.md b/snippets/fromCamelCase.md index e9c61f1ba..e004e67ef 100644 --- a/snippets/fromCamelCase.md +++ b/snippets/fromCamelCase.md @@ -3,7 +3,7 @@ Converts a string from 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 const fromCamelCase = (str, separator = '_') => diff --git a/snippets/getType.md b/snippets/getType.md index 3110871fb..53f660420 100644 --- a/snippets/getType.md +++ b/snippets/getType.md @@ -2,7 +2,7 @@ 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 const getType = v => diff --git a/snippets/hexToRGB.md b/snippets/hexToRGB.md index e3118ecf3..273aafd21 100644 --- a/snippets/hexToRGB.md +++ b/snippets/hexToRGB.md @@ -6,12 +6,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)' ``` diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index d90de1136..75dbc8adf 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -1,6 +1,6 @@ ### 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. You can omit `start` to use a default value of `0`. diff --git a/snippets/mapObject.md b/snippets/mapObject.md new file mode 100644 index 000000000..102795a73 --- /dev/null +++ b/snippets/mapObject.md @@ -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 } +*/ +``` diff --git a/snippets/readFileLines.md b/snippets/readFileLines.md index 288c2ebf8..6433c6059 100644 --- a/snippets/readFileLines.md +++ b/snippets/readFileLines.md @@ -4,7 +4,7 @@ Returns an array of lines from the specified file. Use `readFileSync` function in `fs` node package to create a `Buffer` from a file. convert buffer to string using `toString(encoding)` function. -creating an array from contents of file by `split`ing file content line by line(each `\n`). +creating an array from contents of file by `split`ing file content line by line (each `\n`). ```js const fs = require('fs'); diff --git a/snippets/take.md b/snippets/take.md index 85802f6a8..f6afa5dab 100644 --- a/snippets/take.md +++ b/snippets/take.md @@ -1,6 +1,6 @@ ### 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. diff --git a/snippets/truthCheckCollection.md b/snippets/truthCheckCollection.md new file mode 100644 index 000000000..82a6d26cd --- /dev/null +++ b/snippets/truthCheckCollection.md @@ -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 +``` diff --git a/tag_database b/tag_database index 3d17b3376..f48f069b3 100644 --- a/tag_database +++ b/tag_database @@ -9,11 +9,13 @@ capitalizeEveryWord:string chainAsync:function chunk:array cleanObj:object +coalesce:utility +coalesceFactory:utility collatz:math compact:array compose:function countOccurrences:array -current-URL:browser +currentURL:browser curry:function deepFlatten:array difference:array @@ -57,6 +59,7 @@ JSONToDate:date JSONToFile:node last:array lcm:math +mapObject:array median:math nthElement:array objectFromPairs:object @@ -95,6 +98,7 @@ toCamelCase:string toEnglishDate:date toOrdinalSuffix:utility truncateString:string +truthCheckCollection:object union:array UUIDGenerator:utility validateEmail:utility