Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
ArgueJS is a library that allows you to delightfully extend your methods's signatures with optional parameters, default values and type-checking.
ArgueJS is a JavaScript library that allows you to delightfully extend your method's signatures with optional parameters, default values and type-checking.
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:
function range(){
arguments = __({start: [Number, 0], stop: Number, step: [Number, 1]})
for(var i = arguments.start; i < arguments.stop; i += arguments.step)
console.log(i);
}
>>> range(3)
0
1
2
>>> range(3, 5)
3
4
>>> range(0, 5, 2)
0
2
4
ArgueJS is available for both node.js and the browser.
###Node.js
Package is available through npm:
$ npm install arguejs
Include the ArgueJS browser build in your pages.
<script src="argue.js" type="text/javascript"></script>
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.
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:
function person(){
var signature = {name: String, age: Number};
arguments = __(signature);
// String name is now referenced by arguments.name
// Number age is now referenced by arguments.age
return arguments;
}
>>> person('John', 27).name
'John'
>>> person('John', 27).age
27
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:
function person(){
args = __({name: String, age: Number}, arguments);
// ...
See how does the arguments inference works for more.
Type-checking ensures your arguments are what you expected, and throws errors when not.
example:
function age(){
arguments = __({born: Date})
// ...
>>> age('01/10/1988')
Error: parameter 'born' waiting for a Date argument but received a String
The primitive data type null
can be used to allow the argument to be of any type.
example:
function book(){
arguments = __({title: null})
// ...
return arguments.title;
}
>>> book('Animal Farm: a Fairy Story')
'Animal Farm: a Fairy Story'
>>> book(1984)
1984
The relation of ArgueJS with undefined
and null
values is detail explained at our Wiki page Null and Undefined types
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:
function unique(){
arguments = __({array: Array, isSorted: [Boolean], iterator: [Function]})
// 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.
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:
function unique(){
arguments = __({array: Array, isSorted: [Boolean, false], iterator: [Function, function(element){
return element;
}]})
// 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 = __({name: [undefined, 'unknown']});
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.
Method that gives us the String representation of the type of a given object.
Consider the following example, using the native typeof
method:
> function whichType() {
return typeof this;
}
> [
whichType.call(false), // "boolean", right? No!
whichType.call("hello"), // "string", right? No!
whichType.call(123), // "number", right? No!
];
[ 'object', 'object', 'object' ]
Replace the function whichType
to use ArgueJS' __.typeof
and you will have the expected values:
function whichType() {
return __.typeof( this );
}
[ 'Boolean', 'String', 'Number' ]
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?
> __.getType({key:"value"}) === Object
true
> constructor = __.getType(7)
[Function: Number]
> constructor("myString") // Number("myString")
NaN
> __.getType(this)
[Function: global]
The method __.belongs
tells us if a given instance belongs to the given type class.
No excuses to compare String representations anymore!
> __.belongs({key:"value"}, Object)
true
> __.belongs(this, __.type.global)
true
> __.belongs("value", Number)
false
Utility to recover the ownership over the __
variable.
var ArgueJS = __.noConflict();
// Now, __ makes reference to its old value, the one before you added ArgueJS
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.
This project is on its very early stages and any help, suggestion or posted issue will be very appreciated.
v0.2.3
undefined
as a non passed argument. (#12)FAQs
ArgueJS is a library that allows you to delightfully extend your methods's signatures with optional parameters, default values and type-checking.
The npm package arguejs receives a total of 4,350 weekly downloads. As such, arguejs popularity was classified as popular.
We found that arguejs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.