From 9ff0b8e2d7027ef3cdf7e93138200a10c2952c77 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 26 Jan 2018 11:40:48 +0000 Subject: [PATCH] Travis build: 1435 --- README.md | 81 ++++++++++++++++++++++++++++++----- docs/index.html | 27 +++++++++--- snippets/sortedIndexBy.md | 2 +- snippets/sortedLastIndexBy.md | 2 +- 4 files changed, 95 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8fb1c87b8..af9142ac8 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,9 @@ average(1, 2, 3); * [`shuffle`](#shuffle) * [`similarity`](#similarity) * [`sortedIndex`](#sortedindex) +* [`sortedIndexBy`](#sortedindexby) * [`sortedLastIndex`](#sortedlastindex) +* [`sortedLastIndexBy`](#sortedlastindexby) * [`symmetricDifference`](#symmetricdifference) * [`symmetricDifferenceBy`](#symmetricdifferenceby) * [`symmetricDifferenceWith`](#symmetricdifferencewith) @@ -1965,21 +1967,18 @@ sortedIndex([30, 50], 40); // 1
[⬆ Back to top](#table-of-contents) -### sortedLastIndex +### sortedIndexBy -Returns the highest index at which value should be inserted into array in order to maintain its sort order. +Returns the lowest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function. Check if the array is sorted in descending order (loosely). -Use `Array.map()` to map each element to an array with its index and value. -Use `Array.filter()` to find all possible positions where the element could be inserted, `Array.slice(-1)` to get the last one. +Use `Array.findIndex()` to find the appropriate index where the element should be inserted, based on the iterator function `fn`. ```js -const sortedLastIndex = (arr, n) => { - const isDescending = arr[0] > arr[arr.length - 1]; - const index = arr - .map((val, i) => [i, val]) - .filter(el => (isDescending ? n >= el[1] : n >= el[1])) - .slice(-1)[0][0]; +const sortedIndexBy = (arr, n, fn) => { + const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]); + const val = fn(n); + const index = arr.findIndex(el => (isDescending ? val >= fn(el) : val <= fn(el))); return index === -1 ? arr.length : index; }; ``` @@ -1987,6 +1986,37 @@ const sortedLastIndex = (arr, n) => {
Examples +```js +sortedIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 0 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### sortedLastIndex + +Returns the highest 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.map()` to map each element to an array with its index and value. +Use `Array.reverse()` and `Array.findIndex()` to find the appropriate last index where the element should be inserted. + +```js +const sortedLastIndex = (arr, n) => { + const isDescending = arr[0] > arr[arr.length - 1]; + const index = arr + .map((val, i) => [i, val]) + .reverse() + .findIndex(el => (isDescending ? n <= el[1] : n >= el[1])); + return index === -1 ? 0 : arr.length - index - 1; +}; +``` + +
+Examples + ```js sortedLastIndex([10, 20, 30, 30, 40], 30); // 3 ``` @@ -1996,6 +2026,37 @@ sortedLastIndex([10, 20, 30, 30, 40], 30); // 3
[⬆ Back to top](#table-of-contents) +### sortedLastIndexBy + +Returns the highest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function. + +Check if the array is sorted in descending order (loosely). +Use `Array.reverse()` and `Array.findIndex()` to find the appropriate last index where the element should be inserted, based on the iterator function `fn`.. + +```js +const sortedLastIndexBy = (arr, n, fn) => { + const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]); + const val = fn(n); + const index = arr + .map((val, i) => [i, fn(val)]) + .reverse() + .findIndex(el => (isDescending ? val <= el[1] : val >= el[1])); + return index === -1 ? 0 : arr.length - index; +}; +``` + +
+Examples + +```js +sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### symmetricDifference Returns the symmetric difference between two arrays. diff --git a/docs/index.html b/docs/index.html index 453a9237d..e2c8da5f7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

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

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

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

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

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);
@@ -367,15 +367,32 @@ Object.assig
 };
 
sortedIndex([5, 3, 2, 1], 4); // 1
 sortedIndex([30, 50], 40); // 1
-

sortedLastIndex

Returns the highest 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.map() to map each element to an array with its index and value. Use Array.filter() to find all possible positions where the element could be inserted, Array.slice(-1) to get the last one.

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

sortedIndexBy

Returns the lowest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function.

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

const sortedIndexBy = (arr, n, fn) => {
+  const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);
+  const val = fn(n);
+  const index = arr.findIndex(el => (isDescending ? val >= fn(el) : val <= fn(el)));
+  return index === -1 ? arr.length : index;
+};
+
sortedIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 0
+

sortedLastIndex

Returns the highest 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.map() to map each element to an array with its index and value. Use Array.reverse() and Array.findIndex() to find the appropriate last index where the element should be inserted.

const sortedLastIndex = (arr, n) => {
   const isDescending = arr[0] > arr[arr.length - 1];
   const index = arr
     .map((val, i) => [i, val])
-    .filter(el => (isDescending ? n >= el[1] : n >= el[1]))
-    .slice(-1)[0][0];
-  return index === -1 ? arr.length : index;
+    .reverse()
+    .findIndex(el => (isDescending ? n <= el[1] : n >= el[1]));
+  return index === -1 ? 0 : arr.length - index - 1;
 };
 
sortedLastIndex([10, 20, 30, 30, 40], 30); // 3
+

sortedLastIndexBy

Returns the highest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function.

Check if the array is sorted in descending order (loosely). Use Array.reverse() and Array.findIndex() to find the appropriate last index where the element should be inserted, based on the iterator function fn..

const sortedLastIndexBy = (arr, n, fn) => {
+  const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);
+  const val = fn(n);
+  const index = arr
+    .map((val, i) => [i, fn(val)])
+    .reverse()
+    .findIndex(el => (isDescending ? val <= el[1] : val >= el[1]));
+  return index === -1 ? 0 : arr.length - index;
+};
+
sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1
 

symmetricDifference

Returns the symmetric difference between two arrays.

Create a Set from each array, then use Array.filter() on each of them to only keep values not contained in the other.

const symmetricDifference = (a, b) => {
   const sA = new Set(a),
     sB = new Set(b);
diff --git a/snippets/sortedIndexBy.md b/snippets/sortedIndexBy.md
index 97335dc58..b57a26947 100644
--- a/snippets/sortedIndexBy.md
+++ b/snippets/sortedIndexBy.md
@@ -15,5 +15,5 @@ const sortedIndexBy = (arr, n, fn) => {
 ```
 
 ```js
-sortedIndexBy([{ 'x': 4 }, { 'x': 5 }], { 'x': 4 }, o => o.x); // 0
+sortedIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 0
 ```
diff --git a/snippets/sortedLastIndexBy.md b/snippets/sortedLastIndexBy.md
index 02ad7ce60..443ba5045 100644
--- a/snippets/sortedLastIndexBy.md
+++ b/snippets/sortedLastIndexBy.md
@@ -18,5 +18,5 @@ const sortedLastIndexBy = (arr, n, fn) => {
 ```
 
 ```js
-sortedLastIndexBy([{ 'x': 4 }, { 'x': 5 }], { 'x': 4 }, o => o.x); // 1
+sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1
 ```