Skip to content
Extract options and a callback function from arguments
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.editorconfig
.eslintrc
.gitattributes
.gitignore
.jscs.json
.travis.yml
LICENSE
README.md
appveyor.yml
bower.json
component.json
optback-cjs.js
optback.js
package.json
test.js

README.md

optback.js

Build Status Build status Coverage Status devDependency Status

Extract options and a callback function from arguments

optback(['foo', {bar: 'baz'}, function qux() {/* ... */}]);
/*
=> {
      options: {bar: 'baz'},
      callback: [Function: qux]
   }
*/

This module is useful to create a function which takes an option object and a callback function as its arguments, like Node's fs.readFile.

Installation

Package managers

npm NPM version

npm install --save optback

bower Bower version

bower install --save optback

Standalone

Download the script file directly.

API

optback(args)

args: Array or array-like object (e.g. arguments)
Return: Object

It returns an object in the form {options: [Object], callback: [Function]}:

  • options is the penultimate element of args, or {} if that is not an object.
  • callback is the last element of args.

It throws an error when the last element is not a function.

(function() {
  var argv = getCallback(arguments);
  argv.options; //=> {some: 'options'}
  argv.callback; //=> [Function: cb]
})('foo', {some: 'options'}, function cb() {/* ... */});

License

Copyright (c) 2014 Shinnosuke Watanabe

Licensed under the MIT License.

Something went wrong with that request. Please try again.