Created
June 10, 2016 05:35
-
-
Save stefanfrede/de20ff12028a1f2cde9b1aace76d47de to your computer and use it in GitHub Desktop.
leftGather gathers excess arguments into it from the left.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gathering arguments from the right is easy in ES6 with the spread operator | |
*/ | |
const [first, ...butFirst] = ['why', 'hello', 'there', 'little', 'droid']; | |
first | |
//=> 'why' | |
butFirst | |
//=> ["hello","there","little","droid"] | |
/** | |
* With leftGather, we have to supply the length of the array we wish to use as | |
* the result. | |
*/ | |
const leftGather = (outputArrayLength) => { | |
return function (inputArray) { | |
return [inputArray.slice(0, inputArray.length - outputArrayLength + 1)].concat( | |
inputArray.slice(inputArray.length - outputArrayLength + 1) | |
); | |
}; | |
}; | |
const [butLast, last] = leftGather(2)(['why', 'hello', 'there', 'little', 'droid']); | |
butLast | |
//=> ["why","hello","there","little"] | |
last | |
//=> 'droid' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment