Arguments Accepted

In stead of forcing the user of your JavaScript library to "properly" address your functions (per documentation that you still havent written), Arguments Accepted will make it so that your lib can just accept all sane formats. The user thereof can then query your stuff with the stuff he already has in stead of having to massage his argument data into shape first.

Single argument

myLibrary = function (e) { /* accepts dom references in the form of: objects, id (as a string), array of id, arrays of objects, arrays of id's mixed with objects, multiple arguments (as if an array), */ e = ( arguments.length > 1 ) ? [].slice.call(arguments) : (Array.isArray(e)) ? e : [e]; for ( a in e ){ myObj = (typeof e[a] === "string") ? document.getElementById(e[a]) : e[a]; /* do your something with myObj */ } }

To use this library you copy lines 11 and 12 into your function possibly including the comment above it.



Accepts element id
myLibrary('foo')
Accepts objects
myObject = document.getElementById('myElement'); myLibrary( myObject )
Accepts arrays of id's
idArray = [ 'foo' , 'foo2' ]; myLibrary( idArray )
Accepts mixed arrays
myObject = document.getElementById('myElement'); idArray = [ 'foo' , 'foo2' , myObject ]; myLibrary( idArray );
Accepts multiple arguments as id's
myLibrary( 'foo' , 'foo2' )
Accepts multiple arguments as objects
myObject = document.getElementById('myElement'); myOtherObject = document.getElementById('myOtherElement'); myLibrary( myObject , myOtherObject );
Accepts mixed arguments
myObject = document.getElementById('myElement'); myLibrary( 'foo' , 'foo2' , myObject )

Here is a jsfiddle: http://jsfiddle.net/gaby_de_wilde/sDWp7/

Many arguments

With more than one type of argument we dont need support for a series of arguments.

myLibrary = function ( e , myAction ) { /* accepts dom references in the form of: objects, id (as a string), array of id, arrays of objects, arrays of id's mixed with objects */ e = (Array.isArray(e)) ? e : [e]; for ( a in e ){ myObj = (typeof e[a] === "string") ? document.getElementById(e[a]) : e[a]; /* do your something with myObj and myAction */ } }

For example, in the source of this document you find something like:

highlightCode( [ 'codeblockb','codeblockc','codeblockd','codeblocke', 'codeblockf','codeblockg','codeblockh' ] , 'restfull' );

Before Arguments Accepted my Code Higlighter only took one div id, now it also takes arrays and objects and arrays of objects mixed with id's etc.

http://code-highlighter.go-here.nl/code-highlighter-beta.js