From 0aae721e8db2b7448cea8461cbe0217a524c5eeb Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Mon, 8 Jan 2018 20:10:56 +0000 Subject: [PATCH] Travis build: 1085 --- README.md | 59 ++++++++++++++++++++++--------------------------- docs/index.html | 16 +++++++------- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index fce180c51..b5e36be96 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ average(1, 2, 3); * [`isSorted`](#issorted) * [`join`](#join) * [`last`](#last) +* [`longestItem`](#longestitem) * [`mapObject`](#mapobject) * [`maxN`](#maxn) * [`minN`](#minn) @@ -339,15 +340,6 @@ average(1, 2, 3); -### _Uncategorized_ - -
-View contents - -* [`longestItem`](#longestitem) - -
- --- ## 🔌 Adapter @@ -1107,6 +1099,32 @@ last([1, 2, 3]); // 3
[⬆ Back to top](#table-of-contents) +### longestItem + +Takes any number of iterable objects or objects with a `length` property and returns the longest one. + +Use `Array.sort()` to sort all arguments by `length`, return the first (longest) one. + +```js +const longestItem = (...vals) => [...vals].sort((a, b) => b.length - a.length)[0]; +``` + +
+Examples + +```js +longestItem('this', 'is', 'a', 'testcase'); // 'testcase' +longestItem(...['a', 'ab', 'abc']); // 'abc' +longestItem(...['a', 'ab', 'abc'], 'abcd'); // 'abcd' +longestItem([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]); // [1, 2, 3, 4, 5] +longestItem([1, 2, 3], 'foobar'); // 'foobar' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### mapObject Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value. @@ -5266,29 +5284,6 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) ---- - ## _Uncategorized_ - -### longestItem - -Takes any number of iterable objects or objects with a `length` property and returns the longest one. - -Use `Array.sort()` to sort all arguments by `length`, return the first (longest) one. - -```js -const longestItem = (...vals) => [...vals].sort((a, b) => b.length - a.length)[0]; -``` - -```js -longestItem('this', 'is', 'a', 'testcase'); // 'testcase' -longestItem(...['a', 'ab', 'abc']); // 'abc' -longestItem(...['a', 'ab', 'abc'], 'abcd'); // 'abcd' -longestItem([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]); // [1, 2, 3, 4, 5] -longestItem([1, 2, 3], 'foobar'); // 'foobar' -``` - -
[⬆ back to top](#table-of-contents) - ## Collaborators diff --git a/docs/index.html b/docs/index.html index 5a1aa76e4..8e66ca74e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -40,7 +40,7 @@ },1700); } }, false); - }

logo 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);
+      }

logo 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 ]
@@ -173,6 +173,12 @@ Object.assig
 join(['pen', 'pineapple', 'apple', 'pen']); // "pen,pineapple,apple,pen"
 

last

Returns the last element in an array.

Use arr.length - 1 to compute the index of the last element of the given array and returning it.

const last = arr => arr[arr.length - 1];
 
last([1, 2, 3]); // 3
+

longestItem

Takes any number of iterable objects or objects with a length property and returns the longest one.

Use Array.sort() to sort all arguments by length, return the first (longest) one.

const longestItem = (...vals) => [...vals].sort((a, b) => b.length - a.length)[0];
+
longestItem('this', 'is', 'a', 'testcase'); // 'testcase'
+longestItem(...['a', 'ab', 'abc']); // 'abc'
+longestItem(...['a', 'ab', 'abc'], 'abcd'); // 'abcd'
+longestItem([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]); // [1, 2, 3, 4, 5]
+longestItem([1, 2, 3], 'foobar'); // 'foobar'
 

mapObject

Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.

Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new Array to store the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).

const mapObject = (arr, fn) =>
   (a => (
     (a = [arr, arr.map(fn)]), a[0].reduce((acc, val, ind) => ((acc[val] = a[1][ind]), acc), {})
@@ -1132,10 +1138,4 @@ console.log<
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

longestItem

Takes any number of iterable objects or objects with a length property and returns the longest one.

Use Array.sort() to sort all arguments by length, return the first (longest) one.

const longestItem = (...vals) => [...vals].sort((a, b) => b.length - a.length)[0];
-
longestItem('this', 'is', 'a', 'testcase'); // 'testcase'
-longestItem(...['a', 'ab', 'abc']); // 'abc'
-longestItem(...['a', 'ab', 'abc'], 'abcd'); // 'abcd'
-longestItem([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]); // [1, 2, 3, 4, 5]
-longestItem([1, 2, 3], 'foobar'); // 'foobar'
-
\ No newline at end of file +
\ No newline at end of file