Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 78910f0866
Fetching contributors…

Cannot retrieve contributors at this time

42 lines (32 sloc) 1.091 kb

ArgTypes

Filters incoming arguments so that they match specified types and can be optional.

Examples

var fn = argtypes(
    'b', 'f',
    function( useThing, cb ){

    }
);
var fn = argtypes(
    [ '?b', true ], [ '?f', function(){} ], [ '?as', [ 1, 2, 3 ] ]
    , function( aBool, aFn, aAry ){

} )

fn()                         // arguments = [ true,  function(){}, [   1,   2,   3 ] ]
fn( false, ['a', 'b', 'c'] ) // arguments = [ false, function(){}, [ 'a', 'b', 'c' ] ]
fn( new Foo(), 'foo' )       // arguments = [ true,  FOO instance, 'foo'             ]

Usage

  • ? -- value is optional

    Followed by (one or more of):

    • . -- Any value is allowed
      • a -- Array
      • b -- Boolean
      • f -- Function
      • i -- Integer
      • n -- Numeric
      • s -- String
      • o -- Object ( {} )
      • C -- Class instance. Definition: [ 'C', Foo ] or [ '?C', Foo, new Foo( 'a', 'b', 'c' ) ] If the default value is used in this case, a new object is created as: new Foo( fooObj ) and it is up to the constructor to handle this case properly.
Jump to Line
Something went wrong with that request. Please try again.