Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
decree
Advanced tools
Readme
Decree is a declarative arguments-resolver. It saves you time and code when you need to do arguments validation and disambiguation in your APIs.
Simply declare the conditions your arguments should hold, such as their types, whether they are optional and their default values. Decree will take care of the rest, and provide you with clean and disambiguated arguments.
If the user provided an illegal combination of arguments, Decree will tell you where was the problem.
npm install decree
bower install decree
examples/amd
).Let's say you have a function which takes 4 arguments, some are optional. Inside the function you need to:
Without Decree:
/**
* Make a cup of coffee.
* @param {number} [sugars=1] - number of sugars. non-negative decimal number. defaults to 1.
* @param {string} [flavor='bitter'] - flavor of the coffee. defaults to 'bitter'.
* @param {string|integer} [size='large'] - size of cup. 'small', 'medium', 'large' or a positive integer.
* @param {function} callback - called when coffee is ready.
*/
function makeCoffee(sugars, flavor, size, callback){
// verify arguments:
// was sugars provided? if not, flavor = sugars? size = flavor? callback = size?
// but what if flavor was not provided...?
// what about the callback? maybe callback = size? callback = flavor?
// is 'callback' a function? if not, throw an exception?
// ...
// ...
// ...
// finally:
if (/* arguments are valid */){
// make coffee...
callback('Coffee is ready!');
} else {
throw Error('Invalid arguments!');
}
}
With Decree:
Simply decalare the properties of your arguments:
var decs = [{
name: 'sugars',
type: 'nn-number', // non-negative
optional: true,
default: 1
}, {
name: 'flavor',
type: 'string',
optional: true,
default: 'bitter'
}, {
name: 'size',
types: ['string', 'p-int'], // string or positive integer
optional: true,
default: 'large'
}, {
name: 'callback',
type: 'function'
}];
Let Decree do the rest:
/**
* Make a cup of coffee.
* @param {number} [sugars=1] - number of sugars. non-negative decimal number. defaults to 1.
* @param {string} [flavor='bitter'] - flavor of the coffee. defaults to 'bitter'.
* @param {string|integer} [size='large'] - size of cup. 'small', 'medium', 'large' or a positive integer.
* @param {function} callback - called when coffee is ready.
*/
function makeCoffee() {
decree(decs)(arguments, function(sugars, flavor, size, callback) {
// arguments are disambiguated and ready to be used.
// make coffee...
callback('Coffee is ready!');
});
};
Now use your function as usual:
makeCoffee(1.5, function(msg){
console.log(msg); // 'Coffee is ready!'
});
Decree needs to know what you expect. Simply build an array to describe your arguments expectations.
// declarations:
var decs = [{/* arg 1 */}, {/* arg 2 */}, {/* arg 3 */}, ...];
Each item in the array is an object which describes an argument. See declaration structure.
When finished declaring your expectations, use Decree to resolve an array of
arguments. Calling decree(...)
will construct a function which receives an
array of arguments and disambiguates it.
var judge = decree(decs); // `judge` is a function
The constructed judge function has the following signature:
function judge(args, callback, errCallback)
args {Array}
- The array of arguments to disambiguate.callback {Function}
- an optional callback function which is called with
the disambiguated arguments.errCallback {Function}
- an optional callback to handle arguments errors.
This function is called with the error, if there was an error disambiguating
the arguments array.Note:
errCallback
is omitted, and there is an error, an exception will be
thrown.callback
is omitted as well, judge
will return the array of
disambiguated arguments.var decree = require('decree');
var decs = [ /* declarations of foo's arguments */ ];
var judge = decree(decs);
function foo() {
// pass your function's arguments directly to decree:
judge(arguments, function(arg1, arg2, arg3, ...) {
// here you can be sure your arguments are of
// the correct types and values.
}, function(err){
// there was a problem with the provided arguments.
// log it, and throw an exception to the user.
console.log(err);
throw err;
});
}
// use foo as normal:
foo( ... );
When declaring an argument, tell Decree:
name {String}
: Optional. Will be used to identify the argument in error
messages.type {String}
/ types{Array[String]}
: Required.
See built-in types or custom types.optional {Boolean}
: Optional. Is this argument optional?
Defaults to false
.default
: Optional. If the argument is optional, this default value will
be assigned if no value is provided.{
name: ...,
types: [ ... ],
optional: ...,
default: ...
}
Note: If an optional argument has no default value, and that argument is
omitted by the user, decree will assign this argument undefined
.
When there is a problem with the arguments Decree can provide a detailed explanation of what went wrong. By default, an error object will be thrown, unless you provide a second callback which is called with the error.
var decree = require('decree');
// with an exception:
function foo() {
try {
decree(/* decs */)(arguments, function(/* args */) {
// ...
});
} catch (err) {
// if here, there was a problem with the arguments the user passed.
// 'err' contains the information you need
}
}
// or, with an error handling callback:
function foo() {
decree(/* decs */)(arguments, function(/* args */) {
// ...
}, function(err) {
// if here, there was a problem with the arguments the user passed.
// 'err' contains the information you need
});
}
Decree supports several argument types:
*
: Argument matches any type.array
function
hash
: Argument is a simple key-value object.string
regexp
: Argument is a regular expression.boolean
date
: Argument is a Date object.number
n-number
: Argument is a negative numberp-number
: Argument is a positive numbernn-number
: Argument is a non-negative numbernp-number
: Argument is a non-positive numberint
: Argument is an integern-int
: Argument is a negative integerp-int
: Argument is a positive integernn-int
: Argument is a non-negative integernp-int
: Argument is a non-positive integerRegister a custom type with:
decree.resigter(name, validator)
name {String}
- The name of the new type.validator {Function}
- A validation function. Receives a value and should
return true
or false
.Example:
var decree = require('decree');
// register a 'color' type:
decree.register('color',function(v){
return ["blue", "red", "green", "yellow"].indexOf(v) !== -1;
});
// use it:
var decs = [{
type: 'color',
optional: true,
default: "blue"
}];
FAQs
Declarative arguments-resolver
The npm package decree receives a total of 238 weekly downloads. As such, decree popularity was classified as not popular.
We found that decree 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.