From a0bb2eb5979f1012b0fa9b8801e87b31919c992f Mon Sep 17 00:00:00 2001 From: Travis CI Date: Mon, 1 Jan 2018 12:38:25 +0000 Subject: [PATCH] Travis build: 765 [ci skip] --- README.md | 30 ++++++++++++++++++++++++++++++ docs/index.html | 16 ++++++++++++++-- snippets/join.md | 7 +++---- tag_database | 1 + 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ea9e3def8..de52ae0c2 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,7 @@
View contents +* [`join`](#join) * [`sortedIndex`](#sortedindex)
@@ -4589,6 +4590,35 @@ yesNo('Foo', true); // true --- ## _Uncategorized_ +### join + +Joins all elements of an array into a string and returns this string. Uses a separator and an end separator. + +Use `Array.reduce()` to combine elements into a string. +Omit the second argument, `separator`, to use a default separator of `','`. +Omit the third argument, `end`, to use the same value as `separator` by default. + +```js +const join = (arr, separator = ',', end = separator) => + arr.reduce( + (acc, val, i) => + i == arr.length - 2 + ? acc + val + end + : i == arr.length - 1 ? acc + val : acc + val + separator, + '' + ); +``` + +```js +join(); // '' +join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen" +join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen" +join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen" +``` + +
[⬆ back to top](#table-of-contents) + + ### sortedIndex Returns the lowest index at which value should be inserted into array in order to maintain its sort order. diff --git a/docs/index.html b/docs/index.html index 8e1ea7362..5f00a7d48 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -966,7 +966,19 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

sortedIndex

Returns the lowest index at which value should be inserted into array in order to maintain its sort order.

Check if the array is sorted in descending order (loosely). Use Array.findIndex() to find the appropriate index where the element should be inserted.

const sortedIndex = (arr, n) => {
+

Uncategorized

join

Joins all elements of an array into a string and returns this string. Uses a separator and an end separator.

Use Array.reduce() to combine elements into a string. Omit the second argument, separator, to use a default separator of ','. Omit the third argument, end, to use the same value as separator by default.

const join = (arr, separator = ',', end = separator) =>
+  arr.reduce(
+    (acc, val, i) =>
+      i == arr.length - 2
+        ? acc + val + end
+        : i == arr.length - 1 ? acc + val : acc + val + separator,
+    ''
+  );
+
join(); // ''
+join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen"
+join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen"
+join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen"
+

sortedIndex

Returns the lowest index at which value should be inserted into array in order to maintain its sort order.

Check if the array is sorted in descending order (loosely). Use Array.findIndex() to find the appropriate index where the element should be inserted.

const sortedIndex = (arr, n) => {
   const isDescending = arr[0] > arr[arr.length - 1];
   const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));
   return index === -1 ? arr.length : index;
diff --git a/snippets/join.md b/snippets/join.md
index 476af7b15..bcc63edbf 100644
--- a/snippets/join.md
+++ b/snippets/join.md
@@ -15,12 +15,11 @@ const join = (arr, separator = ',', end = separator) =>
         : i == arr.length - 1 ? acc + val : acc + val + separator,
     ''
   );
-
 ```
 
 ```js
 join(); // ''
-join(['pen','pineapple','apple','pen'],',','&'); //"pen,pineapple,apple&pen"
-join(['pen','pineapple','apple','pen'],','); //"pen,pineapple,apple,pen"
-join(['pen','pineapple','apple','pen']); //"pen,pineapple,apple,pen"
+join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen"
+join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen"
+join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen"
 ```
diff --git a/tag_database b/tag_database
index 6e6624597..6943467cc 100644
--- a/tag_database
+++ b/tag_database
@@ -81,6 +81,7 @@ isPromiseLike:utility
 isString:utility
 isSymbol:utility
 isValidJSON:utility
+join:uncategorized
 JSONToDate:date
 JSONToFile:node
 last:array