argtypes

JavaScript function arguments type parser

Usage no npm install needed!

<script type="module">
  import argtypes from 'https://cdn.skypack.dev/argtypes';
</script>

README

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(){} ], [ '?a', [ 1, 2, 3 ] ]
    , function( aBool, fAn, aAry ){

} )

fn();                        // arguments = [ true,  function(){}, [   1,   2,   3 ] ]
fn( false, ['a', 'b', 'c'] ) // arguments = [ false, function(){}, [ 'a', 'b', 'c' ] ]

Usage

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