Skip to content

trecenti/argumenter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Argumenter Build Status Build Status

A descriptive way to handle function arguments.

##By Arguments Type

var argumenter = require('argumenter');
function myFn(opt_fn, opt_options) {
  var handler = argumenter(arguments);

  handler
    .when([Function, Object], function (fn, options) {
      //do something with fn and options;
    }).when(Object, function (options) {
      //do something with options
    }).when(Function, function (fn) {
      //do something with fn
    });

  return handler.done();
}

##By Arguments Length

var argumenter = require('argumenter');
function myFn() {
  var handler = argumenter(arguments);

  handler
    .when(2, function (fn, options) {
      //do something with fn and options;
    }).when(1, function (any) {
      //do something with any
    }).when(0, function () {
      //do something
    });

  return handler.done();
}

##Passing Parameters Explicitly

var argumenter = require('argumenter');
function myFn(foo, bar) {
  var handler = argumenter(foo);

  handler
    .when(String, function(foo) {
      // handle string
    })
    .when(Number, function(foo) {
      // handle number
    });

  return handler.done();
}

##Returning the execution

var argumenter = require('argumenter');
function myFn() {
  var handler = argumenter(arguments);

  handler
    .when(2, function (a, b) {
      return a + b;
    }).when(1, function (a) {
      return a;
    }).when(0, function () {
      return 0;
    });

  return handler.done();
}

myFn(1, 1); // 2
myFn(1); // 1
myFn(); // 0

##Spreading Array Arguments

var argumenter = require('argumenter');
var context = {};

function myFn(array) {
  var handler = argumenter(arguments);

  handler.spread(0).spread(2)
    .when([Object, Function, Number, Array], function (obj, fn, number, array) {
      //do something with obj, fn, number and array
    });

  return handler.done();
}
myFn([{}, function () {}], 1, [[1,2]]); // will be handled as myFn({}, function() {}, 1, [1, 2])

##Context Binding

var argumenter = require('argumenter');
var context = {};

function myFn() {
  var handler = argumenter(arguments);

  handler
    .when(1, function (arg) {
      return this; // this === context
    });

  return handler.done(context);
}

Non-Strict Mode

This module supports non-strict usage. If you find it more convenient, you may pass the function itself to argumenter():

var argumenter = require('argumenter');
function myFn(opt_fn, opt_options) {
  var handler = argumenter(myFn);

  handler
    .when() // ...

  return handler.done();

Note: Attempting to pass a function to argumenter() while in strict mode will result in an exception thrown.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published