A function for mixing up the order of another function's arguments.
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.gitignore
.travis.yml
LICENSE.md
README.md
component.json
index.js
package.json

README.md

shuffler Build Status

shuffler lets you swap around the order of a function's arguments by index.

Installation

Using NPM:

$ npm install shuffler

Or as a component:

$ component install hughsk/shuffler

Usage

shuffler takes a function as its first argument and returns a second function to determine the new argument order. Take this example:

var shuffle = require('shuffler')

function args() {
  console.log(Array.prototype.slice.call(arguments))
};

args('a', 'b', 'c', 'd') // ['a', 'b', 'c', 'd']

args = shuffle(args)(3, 2, 1, 0)
args('a', 'b', 'c', 'd') // ['d', 'c', 'b', 'a']

You can pass a falsey non-numerical value to mute it, i.e. that argument will always be null:

args('a', 'b', 'c', 'd') // ['a', 'b', 'c', 'd']

args = shuffle(args)(3, 2, false, 0)
args('a', 'b', 'c', 'd') // ['d', 'c', null, 'a']

Or a truthy non-numerical value to just use the default argument:

args('a', 'b', 'c', 'd') // ['a', 'b', 'c', 'd']

args = shuffle(args)(3, 2, true, 3)
args('a', 'b', 'c', 'd') // ['d', 'c', 'c', 'd']

By default, any extra arguments will still be used. You can disable this by passing the carryon option as false.

args('a', 'b', 'c', 'd') // ['a', 'b', 'c', 'd']

args = shuffle(args)(1, 1)
args('a', 'b', 'c', 'd') // ['b', 'b', 'c', 'd']

args = shuffle(args, { carryon: false })(1, 1)
args('a', 'b', 'c', 'd') // ['b', 'b']