diff --git a/README.md b/README.md
index 2a5531dc5..89f130919 100644
--- a/README.md
+++ b/README.md
@@ -677,7 +677,7 @@ const sum = pipeAsyncFunctions(
x => x + 3,
async x => (await x) + 4
);
-(async () => {
+(async() => {
console.log(await sum(5)); // 15 (after one second)
})();
```
@@ -892,7 +892,9 @@ Omit the second argument, `delimiter`, to use a default delimiter of `,`.
```js
const arrayToCSV = (arr, delimiter = ',') =>
- arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
+ arr
+ .map(v => v.map(x => (isNaN(x) ? `"${x.replace(/"/g, '""')}"` : x)).join(delimiter))
+ .join('\n');
```
Learn new ES6 JavaScript language features like arrow function, destructuring, generators & more to write cleaner and more productive, readable programs.
Performs left-to-right function composition.
Use Array.prototype.reduce() with the spread operator (...) to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))); diff --git a/docs/function.html b/docs/function.html index f31c958df..f06e8c942 100644 --- a/docs/function.html +++ b/docs/function.html @@ -145,6 +145,7 @@ console.log< ]);
Given a predicate function and a prop string, this curried function will then take an object to inspect by calling the property and passing it to the predicate.
Summon prop on obj, pass it to a provided predicate function and return a masked boolean.
const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]);
+ const lengthIs4 = checkProp(l => l === 4, 'length'); lengthIs4([]); // false lengthIs4([1,2,3,4]); // true diff --git a/docs/index.html b/docs/index.html index 623c127c7..0b901c94a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -103,9 +103,12 @@
any([0, 1, 2, 0], x => x >= 2); // true any([0, 0, 1, 0]); // true
Converts a 2D array to a comma-separated values (CSV) string.
Use Array.prototype.map() and Array.prototype.join(delimiter) to combine individual 1D arrays (rows) into strings. Use Array.prototype.join('\n') to combine all rows into a CSV string, separating each row with a newline. Omit the second argument, delimiter, to use a default delimiter of ,.
const arrayToCSV = (arr, delimiter = ',') => - arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n'); + arr + .map(v => v.map(x => (isNaN(x) ? `"${x.replace(/"/g, '""')}"` : x)).join(delimiter)) + .join('\n');
arrayToCSV([['a', 'b'], ['c', 'd']]); // '"a","b"\n"c","d"' arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '"a";"b"\n"c";"d"' +arrayToCSV([['a', '"b" great'], ['c', 3.1415]]); // '"a","""b"" great"\n"c",3.1415'
Splits values into two groups. If an element in filter is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
Use Array.prototype.reduce() and Array.prototype.push() to add elements to groups, based on filter.
const bifurcate = (arr, filter) => arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);
bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ] @@ -410,9 +413,9 @@
Removes elements from an array for which the given function returns false.
Use Array.prototype.filter() to find array elements that return truthy values and Array.prototype.reduce() to remove elements using Array.prototype.splice(). The func is invoked with three arguments (value, index, array).
const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : [];
remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4]
Returns a random element from an array.
Use Math.random() to generate a random number, multiply it by length and round it off to the nearest whole number using Math.floor(). This method also works with strings.
const sample = arr => arr[Math.floor(Math.random() * arr.length)]; diff --git a/snippets/arrayToCSV.md b/snippets/arrayToCSV.md index a8a499087..727c41f2e 100644 --- a/snippets/arrayToCSV.md +++ b/snippets/arrayToCSV.md @@ -8,7 +8,9 @@ Omit the second argument, `delimiter`, to use a default delimiter of `,`. ```js const arrayToCSV = (arr, delimiter = ',') => - arr.map(v => v.map(x => isNaN(x) ? `"${x.replace(/"/g, '""')}"` : x ).join(delimiter)).join('\n'); + arr + .map(v => v.map(x => (isNaN(x) ? `"${x.replace(/"/g, '""')}"` : x)).join(delimiter)) + .join('\n'); ``` ```js diff --git a/snippets/checkProp.md b/snippets/checkProp.md index c90195730..ddc49eee2 100644 --- a/snippets/checkProp.md +++ b/snippets/checkProp.md @@ -10,6 +10,7 @@ const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]); ```js + const lengthIs4 = checkProp(l => l === 4, 'length'); lengthIs4([]); // false lengthIs4([1,2,3,4]); // true diff --git a/snippets/pipeAsyncFunctions.md b/snippets/pipeAsyncFunctions.md index 8c93add3b..47d6f620c 100644 --- a/snippets/pipeAsyncFunctions.md +++ b/snippets/pipeAsyncFunctions.md @@ -17,7 +17,7 @@ const sum = pipeAsyncFunctions( x => x + 3, async x => (await x) + 4 ); -(async () => { +(async() => { console.log(await sum(5)); // 15 (after one second) })(); ``` diff --git a/snippets/remove.md b/snippets/remove.md index a8c472774..58de9c2e0 100644 --- a/snippets/remove.md +++ b/snippets/remove.md @@ -9,9 +9,9 @@ The `func` is invoked with three arguments (`value, index, array`). const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : []; ``` diff --git a/test/_30s.js b/test/_30s.js index bc92b61ee..22aeaaf6b 100644 --- a/test/_30s.js +++ b/test/_30s.js @@ -53,7 +53,9 @@ const allEqual = arr => arr.every(val => val === arr[0]); const any = (arr, fn = Boolean) => arr.some(fn); const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon; const arrayToCSV = (arr, delimiter = ',') => - arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n'); + arr + .map(v => v.map(x => (isNaN(x) ? `"${x.replace(/"/g, '""')}"` : x)).join(delimiter)) + .join('\n'); const arrayToHtmlList = (arr, listID) => (el => ( (el = document.querySelector('#' + listID)), @@ -991,9 +993,9 @@ const reject = (pred, array) => array.filter((...args) => !pred(...args)); const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : []; const removeNonASCII = str => str.replace(/[^\x20-\x7E]/g, ''); const renameKeys = (keysMap, obj) =>