ArgueJS is a JavaScript library that allows you to delightfully extend your method's signatures with optional parameters, default values and type-checking.
example
Let's suppose we want to rewrite the well known method range from underscorejs.
Note that documentation says its method signature is range([start], stop, [step])
. With ArgueJS we could type just this way:
{ arguments = forvar i = argumentsstart; i < argumentsstop; i += argumentsstep console;}
>>> 0 1 2>>> 3 4>>> 0 2 4
Installation
ArgueJS is available for both node.js and the browser.
Node.js
Package is available through npm:
$ npm install arguejs
Browser
Include the ArgueJS browser build in your pages.
This will provide __
as a global object, or define
it if you are using AMD.
The latest version will be available for hot-linking at http://raw.github.com/zvictor/ArgueJS/master/argue.js.
If you prefer to host yourself, use the argue.js
file from the root of the github project.
Getting started
When writing your JavaScript methods with ArgueJS, have in mind that you will not use conventional parameters definition as you used before. Actually, all your methods should be defined without them.
Just at the beginning of your method scope,
you should pass an object defining your method signature into a call to __
and save its reference for later.
The signature of this method is Object __(Object signature, [Object upperArguments])
example:
{ var signature = name: String age: Number; arguments = ; // String name is now referenced by arguments.name // Number age is now referenced by arguments.age return arguments;}
>>> name 'John'>>> age 27
Propagating arguments
It is recommended that you explicitly pass your methods arguments through ArgueJS. It is not required, unless if running in strict mode or aiming compatibility of your code with future versions of JavaScript, then it is required.
To explicitly pass your methods arguments through ArgueJS,
just pass the arguments
variable after the signature
description object,
like this example does:
{ args = ; // ...
See how does the arguments inference works for more.
Type-checking
Type-checking ensures your arguments are what you expected, and throws errors when not.
example:
{ arguments = // ...
>>> Error: parameter 'born' waiting for a Date argument but received a String
Avoid type-checking
The primitive data type null
can be used to allow the argument to be of any type.
example:
{ arguments = // ... return argumentstitle;}
>>> 'Animal Farm: a Fairy Story'>>> 1984
The relation of ArgueJS with undefined
and null
values is detail explained at our Wiki page Null and Undefined types
Data types
- String
- Number
- Boolean
- Array
- Function
- Object
- Date
- RegExp
Special data types:
- "global" (or __.type.global)
- "Arguments" (or __.type.Arguments)
Optional parameters
Optional parameters are great to avoid a mess of conditional clauses at the beginning of your method.
To make a parameter optional, declare its type inside of an Array, like this: {name: [String]}
example:
{ arguments = // Array array is required // Boolean isSorted is optional // Function iterator is optional // ...
If no value is passed to an optional parameter, then its argument value will be undefined
.
To set a default value for your parameter, take a look at default values.
Default values
When writing methods, sometimes you want to override the value of an undefined argument by a default value. The syntax to do this is similar to optional parameters. That is because a parameter with default value is an optional parameter by definition.
To set a default value for a parameter declare its type and its default value inside of an Array,
like this: {name: [String, 'unknown']}
example:
{ arguments = // Array array is required // Boolean isSorted is optional and its default value is false // Function iterator is optional and its default value is the function declared above // ...
If you do not care about its type, but just want it to have a default value,
you should type your parameter as undefined
example:
arguments = ;
Utilities
Some JavaScript methods do not work intuitively when dealing with types. This is why we made available these utilities methods, to help you to better deal with them.
typeof
Method that gives us the String representation of the type of a given object.
Consider the following example, using the native typeof
method:
> { return typeof this;}> whichType // "boolean", right? No! whichType // "string", right? No! whichType // "number", right? No!; 'object' 'object' 'object'
Replace the function whichType
to use ArgueJS' __.typeof
and you will have the expected values:
{ return __;}
'Boolean' 'String' 'Number'
getType
The method __.getType
gives us the type class of the object we may want to inspect.
Why using String representations when we can access the type directly?
> __ === Objecttrue> [Function: Number]> constructor("myString") // Number("myString")NaN> __.getType(this)[Function: global]
belongs
The method __.belongs
tells us if a given instance belongs to the given type class.
No excuses to compare String representations anymore!
> __true> __true> __false
noConflict
Utility to recover the ownership over the __
variable.
var ArgueJS = __;// Now, __ makes reference to its old value, the one before you added ArgueJS
FAQ
How does the arguments inference works?
To automatically infer your method arguments, ArgueJS uses arguments.callee
internally.
It is a powerful and old resource but won't be supported in future JavaScript versions anymore,
and this is why strict mode disallows its use.
Nowadays it is present in all major browsers, although its use is not recommended in favor of better performance. See its documentation for even more.
Contributing
This project is on its very early stages and any help, suggestion or posted issue will be very appreciated.