Skip to content

tunnckoCore/bind-arguments

Repository files navigation

Bind context (optional) and multiple arguments to function.

code climate standard code style travis build status coverage status dependency status

Install

npm i bind-arguments --save

Usage

For more use-cases see the tests

Params

  • <fn> {Function} function for binding
  • <args> {Array} arguments to bind to given fn
  • [ctx] {Object} optional context for the return function
  • returns {Function} bound function

Example

var bindArguments = require('bind-arguments')

var args = ['foo', 'bar']
var ctx = {qux: 'baz'}
var boundFn = bindArguments(function (a, b, c) {
  return [a, b, c, this.qux]
}, args, ctx)
boundFn() //=> ['foo', 'bar', undefined, 'baz']

var boundFn2 = bindArguments.call({qux: 'roo'}, function (a, b) {
  return [a, b, this.qux]
}, args)
boundFn2() //=> ['foo', 'bar', 'roo']

Related

  • array-slice: Array-slice method. Slices array from the start index up to, but not including, the end index.
  • bind-context: Bind context to the given function and preserves her name. Or set new name of the given function. It also handles .toString correctly.
  • handle-arguments: Handles given Arguments object - return separatly last argument (commonly callback) and other arguments as Array. Useful in node-style callback flow.
  • manage-arguments: Prevents arguments leakage - managing arguments. From Optimization killers by Petka Antonov.
  • sliced: A faster Node.js alternative to Array.prototype.slice.call(arguments)
  • useware: Accept Arguments object or multiple arguments that can be any value, including deeply nested arrays. Flatten the arguments/values then filter them to be only functions. Useful for .use methods.
  • useware-context: Much like useware but with the ability to bind given context to each function.

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

tunnckocore.tk keybase tunnckocore tunnckoCore npm tunnckoCore twitter tunnckoCore github