From a94d8044717d5abc084d8ede9aedef3ffc2c8537 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Sun, 31 Dec 2017 15:16:08 +0000 Subject: [PATCH] Travis build: 740 [ci skip] --- README.md | 34 ++++++++++++++++++++++++++++++++++ docs/index.html | 9 ++++++++- snippets/sortedIndex.md | 6 +++--- tag_database | 1 + 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 82c18d2cf..e1864745a 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,15 @@ +### _Uncategorized_ + +
+View contents + +* [`sortedIndex`](#sortedindex) + +
+ --- ## 🔌 Adapter @@ -4515,6 +4524,31 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _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. + +```js +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; +}; +``` + +```js +sortedIndex([5, 3, 2, 1], 4); // 1 +sortedIndex([30, 50], 40); // 1 +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index 22e389e92..c6dcd331c 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 ]
@@ -952,4 +952,11 @@ 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) => {
+  const isDescending = arr[0] > arr[arr.length - 1];
+  const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));
+  return index === -1 ? arr.length : index;
+};
+
sortedIndex([5, 3, 2, 1], 4); // 1
+sortedIndex([30, 50], 40); // 1
 

\ No newline at end of file diff --git a/snippets/sortedIndex.md b/snippets/sortedIndex.md index f83da6ec0..e8d6a6d86 100644 --- a/snippets/sortedIndex.md +++ b/snippets/sortedIndex.md @@ -8,12 +8,12 @@ Use `Array.findIndex()` to find the appropriate index where the element should b ```js const sortedIndex = (arr, n) => { const isDescending = arr[0] > arr[arr.length - 1]; - const index = arr.findIndex(el => isDescending ? n >= el : n <= el); + const index = arr.findIndex(el => (isDescending ? n >= el : n <= el)); return index === -1 ? arr.length : index; }; ``` ```js -sortedIndex([5,3,2,1], 4); // 1 -sortedIndex([30,50],40); // 1 +sortedIndex([5, 3, 2, 1], 4); // 1 +sortedIndex([30, 50], 40); // 1 ``` diff --git a/tag_database b/tag_database index 82a224942..7e280efbd 100644 --- a/tag_database +++ b/tag_database @@ -130,6 +130,7 @@ similarity:array size:object sleep:function sortCharactersInString:string +sortedIndex:uncategorized speechSynthesis:browser splitLines:string spreadOver:adapter