Skip to content

hughsk/shuffler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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']

About

A function for mixing up the order of another function's arguments.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published