diff --git a/README.md b/README.md index 3203b69a1..d302b8268 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ * [`sampleSize`](#samplesize) * [`shuffle`](#shuffle) * [`similarity`](#similarity) +* [`sortedIndex`](#sortedindex) * [`symmetricDifference`](#symmetricdifference) * [`tail`](#tail) * [`take`](#take) @@ -269,15 +270,6 @@ -### _Uncategorized_ - -
-View contents - -* [`sortedIndex`](#sortedindex) - -
- --- ## 🔌 Adapter @@ -1327,6 +1319,34 @@ similarity([1, 2, 3], [1, 2, 4]); // [1,2]
[⬆ 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. + +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; +}; +``` + +
+Examples + +```js +sortedIndex([5, 3, 2, 1], 4); // 1 +sortedIndex([30, 50], 40); // 1 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### symmetricDifference Returns the symmetric difference between two arrays. @@ -4190,6 +4210,7 @@ Use the spread operator (`...`) to check if the provided argument is iterable in ```js + const isArrayLike = val => try {return [...val], true; } catch (e) { return false; } @@ -4650,31 +4671,6 @@ 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 6951935aa..4f5e25ed6 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 ]
@@ -271,6 +271,13 @@ shuffle(foo); // [2,3,1]
 console.log(foo); // [1,2,3]
 

similarity

Returns an array of elements that appear in both arrays.

Use filter() to remove values that are not part of values, determined using includes().

const similarity = (arr, values) => arr.filter(v => values.includes(v));
 
similarity([1, 2, 3], [1, 2, 4]); // [1,2]
+

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
 

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);
@@ -879,6 +886,7 @@ hexToRGB('#fff'); // 'rgb(255, 255, 255)'
 
isArray(null); // false
 isArray([1]); // true
 

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use the spread operator (...) to check if the provided argument is iterable inside a try... catch block and the comma operator (,) to return the appropriate value.


+
 const isArrayLike = val =>
   try {return [...val], true; }
   catch (e)  { return false; }
@@ -983,11 +991,4 @@ 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/isArrayLike.md b/snippets/isArrayLike.md index 47a3fe158..eff62aae0 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -6,6 +6,7 @@ Use the spread operator (`...`) to check if the provided argument is iterable in ```js + const isArrayLike = val => try {return [...val], true; } catch (e) { return false; }