From ccc7d389307fb024ba529484889ee66827dc8797 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 18 Dec 2017 13:06:26 +0200 Subject: [PATCH] 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