JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 47
  • Score
    100M100P100Q68105F
  • License

Handles given Arguments object - return separatly last argument (commonly callback) and other arguments as Array. Useful in node-style callback flow. Used in `hybridify`

Package Exports

  • handle-arguments

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (handle-arguments) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm mit license build status coverage status deps status

Handles given Arguments object - return separatly last argument (commonly callback) and other arguments as Array. Useful in node-style callback flow. Used by hybridify

Install

npm install handle-arguments
npm test

Please upgrade to v2.0.x

In v1.0.3 was introduced breaking change. Sorry for the v1.0.4 version...

  • if last argument isnt function, default cb is empty function.
  • and default fn .toString().length is 0 and is named defaultHanleArgumentsCallback

API

For more use-cases see the tests

handleArguments

  • <argsObject> {Arguments} Arguments object
  • return {Object} with properties callback, cb and arguments with args alias
    • callback {Function} last argument if function, else empty function given
    • cb {Function} alias of callback
    • arguments {Array} all arguments without last
    • args {Array} alias of arguments

Example:

var handleArguments = require('handle-arguments');

function fixture() {
  var argz = handleArguments(arguments);
  return argz;
}

console.log(fixture(1, 2, 3));
//=> {callback: [Function: defaultHandleArgumentsCallback],
// arguments: [1, 2, 3], args: [1, 2, 3]}
//=> [Function: defaultHandleArgumentsCallback] is empty function (noop)

console.log(fixture(1, 2, function cb() {}));
//=> {callback: [Function: cb], arguments: [1, 2], args: [1, 2]}

instead of commonly used pattern

function fixture() {
  var args = [].slice.call(arguments);
  var len = args.length
  var callback = args[len - 1];

  if (typeof callback === 'function') {
    args = args.slice(0, -1)
    callback.apply(undefined, [null].concat(args))
  }
  return args;
}

Or more real world examples callback-and-promise, thenify, thenify-all and a lot more...

Author

Charlike Mike Reagent

License MIT license

Copyright (c) 2015 Charlike Mike Reagent, contributors.
Released under the MIT license.


Powered and automated by kdf, January 28, 2015