Skip to content

Instantly share code, notes, and snippets.

@stefanfrede
Created June 10, 2016 05:35
Show Gist options
  • Save stefanfrede/de20ff12028a1f2cde9b1aace76d47de to your computer and use it in GitHub Desktop.
Save stefanfrede/de20ff12028a1f2cde9b1aace76d47de to your computer and use it in GitHub Desktop.
leftGather gathers excess arguments into it from the left.
/**
* 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