diff --git a/README.md b/README.md index 111abf34c..8c8977479 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,16 @@ +### _Uncategorized_ + +
+View contents + +* [`maxN`](#maxn) +* [`minN`](#minn) + +
+ --- ## 🔌 Adapter @@ -5130,6 +5140,47 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _Uncategorized_ + +### maxN + +Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order). + +Sort's the array's shallow copy in descending order and returns the first n elements + +Skip the second argument to get a single element(in the form of a array) +```js +const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n); +``` + +```js +maxN([1, 2, 3]); // [3] +maxN([1, 2, 3], 2); // [3,2] +maxN([1, 2, 3], 4); // [3,2,1] +``` + +
[⬆ back to top](#table-of-contents) + + +### minN + +Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order). + +Sort's the array's shallow copy in ascending order and returns the first n elements + +Skip the second argument to get a single element(in the form of a array) +```js +const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n); +``` +```js +minN([1, 2, 3]); // [1] +minN([1, 2, 3], 2); // [1,2] +minN([1, 2, 3], 4); // [1,2,3] +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index adf23cfc7..82eab6aae 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 ]
@@ -1155,4 +1155,12 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
+

Uncategorized

maxN

Returns the n maximum elements from the provided array. If n is greater than or equal to the provided array's length than return the original array(sorted in descending order).

Sort's the array's shallow copy in descending order and returns the first n elements

Skip the second argument to get a single element(in the form of a array)

const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
+
maxN([1, 2, 3]); // [3]
+maxN([1, 2, 3], 2); // [3,2]
+maxN([1, 2, 3], 4); // [3,2,1]
+

minN

Returns the n minimum elements from the provided array. If n is greater than or equal to the provided array's length than return the original array(sorted in ascending order).

Sort's the array's shallow copy in ascending order and returns the first n elements

Skip the second argument to get a single element(in the form of a array)

const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
+
minN([1, 2, 3]); // [1]
+minN([1, 2, 3], 2); // [1,2]
+minN([1, 2, 3], 4); // [1,2,3]
 

\ No newline at end of file diff --git a/snippets/maxN.md b/snippets/maxN.md index e777b1949..efc9e5052 100644 --- a/snippets/maxN.md +++ b/snippets/maxN.md @@ -5,12 +5,12 @@ Returns the `n` maximum elements from the provided array. If `n` is greater than Sort's the array's shallow copy in descending order and returns the first n elements Skip the second argument to get a single element(in the form of a array) -``` js +```js const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n); ``` -``` js -maxN([1,2,3]); // [3] -maxN([1,2,3],2); // [3,2] -maxN([1,2,3],4); // [3,2,1] +```js +maxN([1, 2, 3]); // [3] +maxN([1, 2, 3], 2); // [3,2] +maxN([1, 2, 3], 4); // [3,2,1] ``` diff --git a/snippets/minN.md b/snippets/minN.md index d3a7bd13f..2cee5b091 100644 --- a/snippets/minN.md +++ b/snippets/minN.md @@ -5,12 +5,11 @@ Returns the `n` minimum elements from the provided array. If `n` is greater than Sort's the array's shallow copy in ascending order and returns the first n elements Skip the second argument to get a single element(in the form of a array) -``` js +```js const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n); - ``` -``` js -minN([1,2,3]); // [1] -minN([1,2,3],2); // [1,2] -minN([1,2,3],4); // [1,2,3] +```js +minN([1, 2, 3]); // [1] +minN([1, 2, 3], 2); // [1,2] +minN([1, 2, 3], 4); // [1,2,3] ``` diff --git a/tag_database b/tag_database index 540368f1d..a0e099b93 100644 --- a/tag_database +++ b/tag_database @@ -96,9 +96,11 @@ lowercaseKeys:object mapObject:array mask:string max:math +maxN:uncategorized median:math memoize:function min:math +minN:uncategorized negate:logic nthElement:array objectFromPairs:object