argtypes

JavaScript function arguments type parser

ArgTypes

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

var fn = argtypes(
    'b', 'f',
    function( useThingcb ){
    
    }
);
var fn = argtypes(
    [ '?b', true ], [ '?f', function(){} ], [ '?a', [ 1, 2, 3 ] ]
    , function( aBoolfAnaAry ){
 
} )
 
fn();                        // arguments = [ true,  function(){}, [   1,   2,   3 ] ] 
fn( false, ['a', 'b', 'c'] ) // arguments = [ false, function(){}, [ 'a', 'b', 'c' ] ] 
  • ? -- value is optional

Followed by:

  • . -- 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.