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