Args.js

More of this var args = Args([ {elements: Args.ARRAY | Args.Required}, {qualifier: Args.FUNCTION | Args.Optional}, {node: Args.DOM_EL | Args.Optional} {regex: Args.STRING | Args.Optional}, {index: Args.INT | Args.Optional, _default: 0} ], arguments); Less of this // set a default value for qualifier qualifier = qualifier || 0; if ( jQuery.isFunction( qualifier ) ) { // treat qualifier as function } else if ( qualifier.nodeType ) { // treat qualifier as a DOM node } else if ( typeof qualifier === "string" ) { // treat qualifier as a regular expression } else { // treat qualifier as an index } Based on actual code in jQuery

Example

function addBook() { var args = Args([ {title: Args.STRING | Args.Required}, {description: Args.STRING | Args.Optional}, {rating: Args.INT | Args.Optional} ], arguments); args.title === "Frankenstein"; args.description === undefined; args.rating === 5; } addBook("Frankenstein", 5);

Download

Args.js lets you easily create functions with optional parameters, default parameters, parameter groups and named parameters. It checks types and will trigger exceptions if a function is called incorrectly.Clearly and safely specify the arguments your function takes. Have flexibility without writing spaghetti code.

Full (12.0k) Minified (4.1k)

Or install with NPM:

npm install args-js

Or Bower:

bower install args-js

Source

<script src=...>

Features

Optional & Required Arguments

var args = Args([ {title: Args.STRING | Args.Required}, {description: Args.STRING | Args.Optional}, {rating: Args.INT | Args.Optional} ], arguments);

Default Arguments

var args = Args([ {description: Args.STRING | Args.Optional, _default: "Description missing"}, {rating: Args.INT | Args.Optional, _default: 3} ], arguments);

Custom Types

var args = Args([ {title: Args.STRING | Args.Required}, {book: Args.OBJECT | Args.Required, _type: MyBook} ], arguments);

Argument Groups

function myFunc() { var args = Args([ {title: Args.STRING | Args.Required}, [ {description: Args.STRING}, {myBook: Args.OBJECT, _type: MyBook} ] ], arguments); } myFunc("Wuthering Heights", wutherBookObject); // or myFunc("Wuthering Heights", "A book about love and rain.");

Named Arguments

function myFunc() { var args = Args([ {title: Args.STRING | Args.Required}, {description: Args.STRING | Args.Optional}, {rating: Args.INT | Args.Optional} ], arguments); } myFunc("Frankenstein", "A book about monsters", 5); // or myFunc("Frankenstein", { description: "A book about monsters", rating: 5 }); // or myFunc({ title: "Frankenstein", description: "A book about monsters", rating: 5 });

Mixed Order Arguments

function addBook() { var args = Args([ {author: Args.OBJECT | Args.Optional, _type: MyAuthor}, {title: Args.STRING | Args.Required}, {rating: Args.INT | Args.Optional}, {callback: Args.FUNCTION | Args.Required} ], arguments); } // you can now call addBook(new MyAuthor("Mary Shelley"), "Frankenstein", 5, function(book) { /* ... */ }); // or addBook("Frankenstein", function(book) { /* ... */ });

Custom Checks

var args = Args([ {author: Args.OBJECT | Args.Required, _check: function(author) { return author.name === "Mary Shelley"; } }, {title: Args.STRING | Args.Required} ], arguments);

Argument Precedence

function addBook() { var args = Args([ {author: Args.STRING | Args.Required}, {title: Args.STRING | Args.Required} ], arguments); } addBook("Frankenstein", {author: "Mary Shelley"}); // args.author === "Mary Shelley" // args.title === "Frankenstein"

Required

Optional

function addBook() { var args = Args([ {author: Args.STRING | Args.Optional}, {title: Args.STRING | Args.Required} ], arguments); } addBook("Frankenstein"); // args.author === undefined // args.title === "Frankenstein"

Types

Args.ANY Args.STRING Args.FUNCTION Args.INT Args.FLOAT Args.ARRAY_BUFFER Args.OBJECT Args.DATE Args.BOOL Args.DOM_EL Args.ARRAY

See Also

args-js ‐ which does default values, casting and type hinting.

arg-err ‐ a lightweight library to check for type errors.

argspecjs ‐ handles optional arguments in any position in the argument list and allows custom property checkers.

converter ‐ to find out the exchange rate etn to usd.

Strict.js ‐ A fuller type checking library for writing object definitions as well as function definitions.

over.js ‐ A different approach to the same problem, this is an elegant library for function overloading.

Credits