From b48e971c5063fd4dc79a545a2e94344e35b3b019 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Thu, 14 Dec 2017 18:49:54 +0530 Subject: [PATCH 01/22] Create sqaure_it.md I don't exactly know the use of this snippet but contributors can recommend on this approach of `.reduce()`. --- snippets/sqaure_it.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/sqaure_it.md diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md new file mode 100644 index 000000000..9f25d895e --- /dev/null +++ b/snippets/sqaure_it.md @@ -0,0 +1,18 @@ +### Square The Data + +Pass an array of integers you want to sqaure it +Here using `.reduce()` the new object is accumulator and one by one values from array are passed and go through function. + +``` +const arr = [1,2,3,4,5,6,7]; +arr.reduce(function(a,b){ +a[b] = b * b; +return a; +},{}) // {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} +// +callback accumulator currentValue currentIndex array return value +first call 1 1 0 [0, 1, 2, 3, 4] 1 +second call 2 2 1 [0, 1, 2, 3, 4] 4 +third call 3 3 2 [0, 1, 2, 3, 4] 9 +... +``` From 8ee2afab21ebb2056e760b5d1d4eb2dad0996ac4 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:03:59 +0530 Subject: [PATCH 02/22] Create First-one-to-pass-truth-test.md --- snippets/First-one-to-pass-truth-test.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 snippets/First-one-to-pass-truth-test.md diff --git a/snippets/First-one-to-pass-truth-test.md b/snippets/First-one-to-pass-truth-test.md new file mode 100644 index 000000000..7c50de1a0 --- /dev/null +++ b/snippets/First-one-to-pass-truth-test.md @@ -0,0 +1,14 @@ +### 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). + +``` +function 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 +} + +// test here +findElement([1, 2, 3, 4], function(num){ return num !== 2 && num !== 1 }); //3 <- first element in array to be passed truth test +``` From f504a8cf77fd543ab508e36065ea33f3043c7dd1 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:06:11 +0530 Subject: [PATCH 03/22] Update First-one-to-pass-truth-test.md --- snippets/First-one-to-pass-truth-test.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets/First-one-to-pass-truth-test.md b/snippets/First-one-to-pass-truth-test.md index 7c50de1a0..0732eb51c 100644 --- a/snippets/First-one-to-pass-truth-test.md +++ b/snippets/First-one-to-pass-truth-test.md @@ -3,9 +3,8 @@ A function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument). ``` -function findElement(arr, func) { +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 } From d7c2e3a017997789eb6b72fed4d6569991f8980a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 14:40:03 +0200 Subject: [PATCH 04/22] Update and rename First-one-to-pass-truth-test.md to first-one-to-pass-truth-test.md --- ...pass-truth-test.md => first-one-to-pass-truth-test.md} | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) rename snippets/{First-one-to-pass-truth-test.md => first-one-to-pass-truth-test.md} (66%) diff --git a/snippets/First-one-to-pass-truth-test.md b/snippets/first-one-to-pass-truth-test.md similarity index 66% rename from snippets/First-one-to-pass-truth-test.md rename to snippets/first-one-to-pass-truth-test.md index 0732eb51c..cea62dd43 100644 --- a/snippets/First-one-to-pass-truth-test.md +++ b/snippets/first-one-to-pass-truth-test.md @@ -1,13 +1,11 @@ -### First-one-to-pass-truth-test +### 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 } - -// test here -findElement([1, 2, 3, 4], function(num){ return num !== 2 && num !== 1 }); //3 <- first element in array to be passed truth test +// findElement([1, 2, 3, 4], function(num){ return num !== 2 && num !== 1 }); //3 <- first element in array to be passed truth test ``` From 1f89910782b836d1d2397ebc88a323cdaf60ee0d Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:13:15 +0530 Subject: [PATCH 05/22] Create Truth-check-objects.md --- snippets/Truth-check-objects.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/Truth-check-objects.md diff --git a/snippets/Truth-check-objects.md b/snippets/Truth-check-objects.md new file mode 100644 index 000000000..042d14605 --- /dev/null +++ b/snippets/Truth-check-objects.md @@ -0,0 +1,21 @@ +### Truth-Check-Objects + +Check if the predicate (second argument) is truthy on all elements of a collection (first argument). + + + - For every object in the collection array, check the truthiness of object’s property passed in pre parameter + - Array#every method internally checks if the value returned from the callback is truthy. + - Return true if it passes for every object. Otherwise, return false. + - Also if the object is empty then it will return false + + ``` + function truthCheck(collection, pre) { + // Is everyone being true? + return collection.every(obj => obj[pre]); +} + +truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, +{"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"); +// true + + ``` From 5f181e0003023ad354da906c1d23b981e3dc955d Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:21:21 +0530 Subject: [PATCH 06/22] Update Truth-check-objects.md --- snippets/Truth-check-objects.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/snippets/Truth-check-objects.md b/snippets/Truth-check-objects.md index 042d14605..6404a22f0 100644 --- a/snippets/Truth-check-objects.md +++ b/snippets/Truth-check-objects.md @@ -9,10 +9,7 @@ Check if the predicate (second argument) is truthy on all elements of a collecti - Also if the object is empty then it will return false ``` - function truthCheck(collection, pre) { - // Is everyone being true? - return collection.every(obj => obj[pre]); -} +truthCheck = (collection, pre) => (collection.every(obj => obj[pre])); truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"); From e8838a5c9303045a82be69f47ff549c9a73b466f Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:21:32 +0530 Subject: [PATCH 07/22] Update Truth-check-objects.md --- snippets/Truth-check-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/Truth-check-objects.md b/snippets/Truth-check-objects.md index 6404a22f0..1c17fd949 100644 --- a/snippets/Truth-check-objects.md +++ b/snippets/Truth-check-objects.md @@ -8,7 +8,7 @@ Check if the predicate (second argument) is truthy on all elements of a collecti - Return true if it passes for every object. Otherwise, return false. - Also if the object is empty then it will return false - ``` + ```js truthCheck = (collection, pre) => (collection.every(obj => obj[pre])); truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, From 48f11812bd0076064d022e56f73e06be985f9cc0 Mon Sep 17 00:00:00 2001 From: xaveyaguarez Date: Sun, 17 Dec 2017 00:08:55 -0800 Subject: [PATCH 08/22] coalesce snippet --- snippets/coalesce.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 snippets/coalesce.md diff --git a/snippets/coalesce.md b/snippets/coalesce.md new file mode 100644 index 000000000..d740b14c0 --- /dev/null +++ b/snippets/coalesce.md @@ -0,0 +1,11 @@ +### Coalesce a set of arguments + +Use `find()` to return the first non excluded argument. + +```js +const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_)) +// coalesce(null,undefined,"",NaN, "Waldo") -> "" + +const coalesceFactory = (excludes = [null, undefined]) => (...args) => args.find(_ => !excludes.includes(_)) +// coalesceFactory([null, undefined, "", NaN])(undefined, null, NaN, "", "Waldo") -> "Waldo" +``` From e8abd35702fcedee5aaa790f5012672a5d429a71 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sun, 17 Dec 2017 22:13:10 +0530 Subject: [PATCH 09/22] Update sqaure_it.md --- snippets/sqaure_it.md | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md index 9f25d895e..ff2a274cd 100644 --- a/snippets/sqaure_it.md +++ b/snippets/sqaure_it.md @@ -1,18 +1,8 @@ ### Square The Data -Pass an array of integers you want to sqaure it -Here using `.reduce()` the new object is accumulator and one by one values from array are passed and go through function. +Use mapObject to return a object from array passed as an argument which gets sqaured as math operation -``` -const arr = [1,2,3,4,5,6,7]; -arr.reduce(function(a,b){ -a[b] = b * b; -return a; -},{}) // {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} -// -callback accumulator currentValue currentIndex array return value -first call 1 1 0 [0, 1, 2, 3, 4] 1 -second call 2 2 1 [0, 1, 2, 3, 4] 4 -third call 3 3 2 [0, 1, 2, 3, 4] 9 -... +```js +const squareIt = arr => mapObject(arr, a => a*a) +squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 } ``` From ad9b16b3f6d837250486f7ceb67997eee61a582e Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sun, 17 Dec 2017 22:15:36 +0530 Subject: [PATCH 10/22] Update sqaure_it.md --- snippets/sqaure_it.md | 1 + 1 file changed, 1 insertion(+) diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md index ff2a274cd..665932e3d 100644 --- a/snippets/sqaure_it.md +++ b/snippets/sqaure_it.md @@ -3,6 +3,7 @@ Use mapObject to return a object from array passed as an argument which gets sqaured as math operation ```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 } ``` From 021df91849aad8d1d43db0c8735b40952de45a1e Mon Sep 17 00:00:00 2001 From: xaveyaguarez Date: Sun, 17 Dec 2017 11:39:24 -0800 Subject: [PATCH 11/22] split coalesce into 2 functions --- snippets/coalesce-factory.md | 9 +++++++++ snippets/coalesce.md | 5 +---- 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 snippets/coalesce-factory.md diff --git a/snippets/coalesce-factory.md b/snippets/coalesce-factory.md new file mode 100644 index 000000000..45f2a4cfb --- /dev/null +++ b/snippets/coalesce-factory.md @@ -0,0 +1,9 @@ +### Coalesce factory + +Returns a function which provides a customized coalesce function + +```js +const coalesceFactory = (excludes = [null, undefined]) => (...args) => args.find(_ => !excludes.includes(_)) +// const customCoalesce = coalesceFactory([null, undefined, "", NaN]) +// customCoalesce(undefined, null, NaN, "", "Waldo") -> "Waldo" +``` diff --git a/snippets/coalesce.md b/snippets/coalesce.md index d740b14c0..c8a59d5e0 100644 --- a/snippets/coalesce.md +++ b/snippets/coalesce.md @@ -1,11 +1,8 @@ ### Coalesce a set of arguments -Use `find()` to return the first non excluded argument. +Use `find()` to return the first non null/undefined argument. ```js const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_)) // coalesce(null,undefined,"",NaN, "Waldo") -> "" - -const coalesceFactory = (excludes = [null, undefined]) => (...args) => args.find(_ => !excludes.includes(_)) -// coalesceFactory([null, undefined, "", NaN])(undefined, null, NaN, "", "Waldo") -> "Waldo" ``` From 245e7442d841a1a7487ba81b4d69658051d0351c Mon Sep 17 00:00:00 2001 From: xaveyaguarez Date: Sun, 17 Dec 2017 14:09:04 -0800 Subject: [PATCH 12/22] use function in coalesce factory --- snippets/coalesce-factory.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/snippets/coalesce-factory.md b/snippets/coalesce-factory.md index 45f2a4cfb..cad3a8f21 100644 --- a/snippets/coalesce-factory.md +++ b/snippets/coalesce-factory.md @@ -1,9 +1,11 @@ ### Coalesce factory -Returns a function which provides a customized coalesce function +Returns a customized coalesce function that returns the first argument +that returns true from the provided argument validation function. ```js -const coalesceFactory = (excludes = [null, undefined]) => (...args) => args.find(_ => !excludes.includes(_)) -// const customCoalesce = coalesceFactory([null, undefined, "", NaN]) -// customCoalesce(undefined, null, NaN, "", "Waldo") -> "Waldo" +const coalesceFactory = valid => (...args) => args.find(valid) + +// const customCoalesce = coalesceFactory(_ => ![null, undefined, "", NaN].includes(_)) +// customCoalesce(undefined, null, NaN, "", "Waldo") //-> "Waldo" ``` From 2da5a15f6857af295ca471372a18d7b2875b17a6 Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Mon, 18 Dec 2017 00:38:58 +0100 Subject: [PATCH 13/22] Fix hexToRgb returning wrong value for short hash without # symbol, fix extendHex called 3 times --- snippets/hexToRGB.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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)' ``` From 8f260fdd65d9db4f59f5ceb808f0274a25e80a5b Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Mon, 18 Dec 2017 00:51:27 +0100 Subject: [PATCH 14/22] Fix typos in descriptions --- snippets/capitalize.md | 2 +- snippets/cleanObj.md | 2 +- snippets/countOccurrences.md | 2 +- snippets/fromCamelCase.md | 2 +- snippets/getType.md | 2 +- snippets/initializeArrayWithRange.md | 2 +- snippets/readFileLines.md | 2 +- snippets/take.md | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) 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/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/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/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/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. From c3de880e419da8acbbc3d83cd71b607a7b731361 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Mon, 18 Dec 2017 08:33:35 +0530 Subject: [PATCH 15/22] Update sqaure_it.md --- snippets/sqaure_it.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md index 665932e3d..eed3eb137 100644 --- a/snippets/sqaure_it.md +++ b/snippets/sqaure_it.md @@ -1,6 +1,8 @@ ### Square The Data -Use mapObject to return a object from array passed as an argument which gets sqaured as math operation +Using an anonymous inner function scope we declare an undefined memory space using closures for storing a return value. We then use a new Array to store the array with a map of the function over it's data set and a comma operator to return a second step without needing to move from one context to another thank to closures and order of operations. + +Notice the need to declare an order of operations(parenthesis) around the anonymous inner function so we can call it immediately. ```js const mapObject = (arr, fn) => (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( ) From 71dbbd35565ceaf8f48e67b12a377e72a194db39 Mon Sep 17 00:00:00 2001 From: Soorena Date: Mon, 18 Dec 2017 12:35:03 +0330 Subject: [PATCH 16/22] Rename current-URL.md to currentURL.md renamed for consistency --- snippets/{current-URL.md => currentURL.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/{current-URL.md => currentURL.md} (100%) 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 From 42bff6deb60f13e91c140e31ede53c2f89767aa3 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 18 Dec 2017 12:11:58 +0200 Subject: [PATCH 17/22] Update and rename sqaure_it.md to mapObject.md --- snippets/mapObject.md | 14 ++++++++++++++ snippets/sqaure_it.md | 11 ----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 snippets/mapObject.md delete mode 100644 snippets/sqaure_it.md 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/sqaure_it.md b/snippets/sqaure_it.md deleted file mode 100644 index eed3eb137..000000000 --- a/snippets/sqaure_it.md +++ /dev/null @@ -1,11 +0,0 @@ -### Square The Data - -Using an anonymous inner function scope we declare an undefined memory space using closures for storing a return value. We then use a new Array to store the array with a map of the function over it's data set and a comma operator to return a second step without needing to move from one context to another thank to closures and order of operations. - -Notice the need to declare an order of operations(parenthesis) around the anonymous inner function so we can call it immediately. - -```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 } -``` From f5a375efba4a24a71b978ad2e0bd029a0e5f7a9e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 18 Dec 2017 12:14:24 +0200 Subject: [PATCH 18/22] Build --- README.md | 36 +++++++++++++++++++++++++++--------- docs/index.html | 31 +++++++++++++++++++++---------- tag_database | 3 ++- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c5ab3f8ba..dfda72618 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) @@ -191,7 +192,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 +363,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 +416,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 +572,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 +1298,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 +1322,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) => { @@ -1394,7 +1412,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 +1454,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 = '_') => @@ -1529,7 +1547,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 => diff --git a/docs/index.html b/docs/index.html index 2383f3c8c..a951ded74 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 @@ -184,7 +185,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 +272,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 +298,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 +383,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 +430,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 +791,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 +808,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) {
@@ -852,7 +863,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 +884,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();
@@ -925,7 +936,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"
diff --git a/tag_database b/tag_database
index 3d17b3376..562b3b388 100644
--- a/tag_database
+++ b/tag_database
@@ -13,7 +13,7 @@ collatz:math
 compact:array
 compose:function
 countOccurrences:array
-current-URL:browser
+currentURL:browser
 curry:function
 deepFlatten:array
 difference:array
@@ -57,6 +57,7 @@ JSONToDate:date
 JSONToFile:node
 last:array
 lcm:math
+mapObject:array
 median:math
 nthElement:array
 objectFromPairs:object

From d7be9d908462f0f30bfa35d7442b1656dc9a5b2b Mon Sep 17 00:00:00 2001
From: Angelos Chalaris 
Date: Mon, 18 Dec 2017 12:15:36 +0200
Subject: [PATCH 19/22] Update and rename coalesce-factory.md to
 coalesceFactory.md

---
 snippets/coalesce-factory.md | 11 -----------
 snippets/coalesceFactory.md  | 11 +++++++++++
 2 files changed, 11 insertions(+), 11 deletions(-)
 delete mode 100644 snippets/coalesce-factory.md
 create mode 100644 snippets/coalesceFactory.md

diff --git a/snippets/coalesce-factory.md b/snippets/coalesce-factory.md
deleted file mode 100644
index cad3a8f21..000000000
--- a/snippets/coalesce-factory.md
+++ /dev/null
@@ -1,11 +0,0 @@
-### Coalesce factory
-
-Returns a customized coalesce function that returns 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/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"
+```

From 1657b6141777f0d490abdca6dd1d9e7299f94a24 Mon Sep 17 00:00:00 2001
From: Angelos Chalaris 
Date: Mon, 18 Dec 2017 12:16:18 +0200
Subject: [PATCH 20/22] Update coalesce.md

---
 snippets/coalesce.md | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/snippets/coalesce.md b/snippets/coalesce.md
index c8a59d5e0..22271dc68 100644
--- a/snippets/coalesce.md
+++ b/snippets/coalesce.md
@@ -1,6 +1,8 @@
-### Coalesce a set of arguments
+### coalesce
 
-Use `find()` to return the first non null/undefined argument.
+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(_))

From 5d27345087241513d358d5c799a4e3b7b43fddea Mon Sep 17 00:00:00 2001
From: Angelos Chalaris 
Date: Mon, 18 Dec 2017 13:05:21 +0200
Subject: [PATCH 21/22] Update and rename Truth-check-objects.md to
 truthCheckCollection.md

---
 snippets/Truth-check-objects.md  | 18 ------------------
 snippets/truthCheckCollection.md | 10 ++++++++++
 2 files changed, 10 insertions(+), 18 deletions(-)
 delete mode 100644 snippets/Truth-check-objects.md
 create mode 100644 snippets/truthCheckCollection.md

diff --git a/snippets/Truth-check-objects.md b/snippets/Truth-check-objects.md
deleted file mode 100644
index 1c17fd949..000000000
--- a/snippets/Truth-check-objects.md
+++ /dev/null
@@ -1,18 +0,0 @@
-### Truth-Check-Objects
-
-Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
-
-
- - For every object in the collection array, check the truthiness of object’s property passed in pre parameter
- - Array#every method internally checks if the value returned from the callback is truthy.
- - Return true if it passes for every object. Otherwise, return false.
- - Also if the object is empty then it will return false
- 
- ```js
-truthCheck = (collection, pre) => (collection.every(obj => obj[pre]));
-
-truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, 
-{"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"); 
-// true
- 
- ```
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
+```

From afc292d706895cd052f1a21d46fec5d377ee8fc2 Mon Sep 17 00:00:00 2001
From: Angelos Chalaris 
Date: Mon, 18 Dec 2017 13:06:26 +0200
Subject: [PATCH 22/22] Build

---
 README.md                                | 52 +++++++++++++++++++++---
 docs/index.html                          | 35 ++++++++++++----
 snippets/first-one-to-pass-truth-test.md | 11 -----
 tag_database                             |  3 ++
 4 files changed, 78 insertions(+), 23 deletions(-)
 delete mode 100644 snippets/first-one-to-pass-truth-test.md

diff --git a/README.md b/README.md
index dfda72618..5f279d99f 100644
--- a/README.md
+++ b/README.md
@@ -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)'
 ```
diff --git a/docs/index.html b/docs/index.html
index a951ded74..fc801123a 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -127,6 +127,7 @@
 objectFromPairs
 objectToPairs
 shallowClone
+truthCheckCollection
 
 

String

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

Utility -

extendHex +coalesce +coalesceFactory +extendHex getType hexToRGB isArray @@ -846,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).

@@ -925,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.

@@ -945,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/first-one-to-pass-truth-test.md b/snippets/first-one-to-pass-truth-test.md deleted file mode 100644 index cea62dd43..000000000 --- a/snippets/first-one-to-pass-truth-test.md +++ /dev/null @@ -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 -``` diff --git a/tag_database b/tag_database index 562b3b388..f48f069b3 100644 --- a/tag_database +++ b/tag_database @@ -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