{"_id":"decree","_rev":"408023","name":"decree","description":"Declarative arguments-resolver","dist-tags":{"latest":"0.0.6"},"maintainers":[{"name":"eyalar","email":"eyalarubas@gmail.com"}],"time":{"modified":"2021-08-04T04:56:50.000Z","created":"2014-07-06T17:08:24.141Z","0.0.6":"2014-10-14T15:04:15.121Z","0.0.4":"2014-10-14T13:56:01.384Z","0.0.3":"2014-10-05T15:20:16.754Z","0.0.2":"2014-09-03T12:02:19.831Z","0.0.1":"2014-07-06T17:08:24.141Z"},"users":{},"author":{"name":"Eyal Arubas","email":"eyalarubas@gmail.com"},"repository":{"type":"git","url":"https://github.com/EyalAr/Decree.git"},"versions":{"0.0.6":{"name":"decree","version":"0.0.6","description":"Declarative arguments-resolver","main":"decree.js","scripts":{"test":"make test","prepublish":"make dist"},"repository":{"type":"git","url":"https://github.com/EyalAr/Decree.git"},"keywords":["declarative","arguments","resolve","disambiguate","match"],"author":{"name":"Eyal Arubas","email":"eyalarubas@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/EyalAr/Decree/issues"},"homepage":"https://github.com/EyalAr/Decree","devDependencies":{"coveralls":"^2.11.1","fume":"0.0.0","istanbul":"^0.3.2","mkdirp":"^0.5.0","mocha":"^1.20.1","mocha-lcov-reporter":"0.0.1","requirejs":"^2.1.15","should":"^4.0.4"},"dependencies":{"lodash-node":"^2.4.1"},"_id":"decree@0.0.6","_shasum":"47efef0ed600d726ea8640345dc4961d0fa54cdd","_from":".","_npmVersion":"1.4.10","_npmUser":{"name":"eyalar","email":"eyalarubas@gmail.com"},"maintainers":[{"name":"eyalar","email":"eyalarubas@gmail.com"}],"dist":{"shasum":"47efef0ed600d726ea8640345dc4961d0fa54cdd","size":11954,"noattachment":false,"key":"/decree/-/decree-0.0.6.tgz","tarball":"http://registry.cnpm.dingdandao.com/decree/download/decree-0.0.6.tgz"},"directories":{},"publish_time":1413299055121,"_cnpm_publish_time":1413299055121,"_hasShrinkwrap":false},"0.0.4":{"name":"decree","version":"0.0.4","description":"Declarative arguments-resolver","main":"decree.js","scripts":{"test":"make test","prepublish":"make dist"},"repository":{"type":"git","url":"https://github.com/EyalAr/Decree.git"},"keywords":["declarative","arguments","resolve","disambiguate","match"],"author":{"name":"Eyal Arubas","email":"eyalarubas@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/EyalAr/Decree/issues"},"homepage":"https://github.com/EyalAr/Decree","devDependencies":{"coveralls":"^2.11.1","fume":"0.0.0","istanbul":"^0.3.2","mkdirp":"^0.5.0","mocha":"^1.20.1","mocha-lcov-reporter":"0.0.1","requirejs":"^2.1.15","should":"^4.0.4"},"dependencies":{"lodash-node":"^2.4.1"},"_id":"decree@0.0.4","_shasum":"c168db98d81427b6ab53e6be469937f2eea5ccb0","_from":".","_npmVersion":"1.4.9","_npmUser":{"name":"eyalar","email":"eyalarubas@gmail.com"},"maintainers":[{"name":"eyalar","email":"eyalarubas@gmail.com"}],"dist":{"shasum":"c168db98d81427b6ab53e6be469937f2eea5ccb0","size":11901,"noattachment":false,"key":"/decree/-/decree-0.0.4.tgz","tarball":"http://registry.cnpm.dingdandao.com/decree/download/decree-0.0.4.tgz"},"directories":{},"publish_time":1413294961384,"_cnpm_publish_time":1413294961384,"_hasShrinkwrap":false},"0.0.3":{"name":"decree","version":"0.0.3","description":"Declarative arguments-resolver","main":"index.js","scripts":{"test":"mocha --recursive --reporter spec tests","coverage":"istanbul cover ./node_modules/mocha/bin/_mocha -- --recursive --reporter spec tests","travis":"istanbul cover --report lcovonly ./node_modules/.bin/_mocha -- --recursive --reporter spec --bail tests && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"},"repository":{"type":"git","url":"https://github.com/EyalAr/Decree.git"},"keywords":["declarative","arguments","resolve","disambiguate","match"],"author":{"name":"Eyal Arubas","email":"eyalarubas@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/EyalAr/Decree/issues"},"homepage":"https://github.com/EyalAr/Decree","devDependencies":{"coveralls":"^2.11.1","istanbul":"^0.3.2","mkdirp":"^0.5.0","mocha":"^1.20.1","mocha-lcov-reporter":"0.0.1","should":"^4.0.4"},"dependencies":{"clone":"^0.1.18"},"_id":"decree@0.0.3","_shasum":"ac42a4c0c5e2adafc408e1cf1b51cfaf3860a132","_from":".","_npmVersion":"1.4.9","_npmUser":{"name":"eyalar","email":"eyalarubas@gmail.com"},"maintainers":[{"name":"eyalar","email":"eyalarubas@gmail.com"}],"dist":{"shasum":"ac42a4c0c5e2adafc408e1cf1b51cfaf3860a132","size":9695,"noattachment":false,"key":"/decree/-/decree-0.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/decree/download/decree-0.0.3.tgz"},"directories":{},"publish_time":1412522416754,"_cnpm_publish_time":1412522416754,"_hasShrinkwrap":false},"0.0.2":{"name":"decree","version":"0.0.2","description":"Declarative arguments-resolver","main":"index.js","scripts":{"test":"mocha --reporter spec --recursive tests/"},"repository":{"type":"git","url":"https://github.com/EyalAr/Decree.git"},"keywords":["declarative","arguments","resolve","disambiguate","match"],"author":{"name":"Eyal Arubas","email":"eyalarubas@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/EyalAr/Decree/issues"},"homepage":"https://github.com/EyalAr/Decree","devDependencies":{"mocha":"^1.20.1","should":"^4.0.4"},"dependencies":{"clone":"^0.1.18"},"_id":"decree@0.0.2","_shasum":"653b1061dbd295b23584c13865f136c616d99d3c","_from":".","_npmVersion":"1.4.10","_npmUser":{"name":"eyalar","email":"eyalarubas@gmail.com"},"maintainers":[{"name":"eyalar","email":"eyalarubas@gmail.com"}],"dist":{"shasum":"653b1061dbd295b23584c13865f136c616d99d3c","size":5711,"noattachment":false,"key":"/decree/-/decree-0.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/decree/download/decree-0.0.2.tgz"},"directories":{},"publish_time":1409745739831,"_cnpm_publish_time":1409745739831,"_hasShrinkwrap":false},"0.0.1":{"name":"decree","version":"0.0.1","description":"Declarative arguments-resolver","main":"index.js","scripts":{"test":"mocha --reporter spec --recursive tests/"},"repository":{"type":"git","url":"https://github.com/EyalAr/Decree.git"},"keywords":["declarative","arguments","resolve","disambiguate","match"],"author":{"name":"Eyal Arubas","email":"eyalarubas@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/EyalAr/Decree/issues"},"homepage":"https://github.com/EyalAr/Decree","devDependencies":{"mocha":"^1.20.1","should":"^4.0.4"},"_id":"decree@0.0.1","_shasum":"48b17ebf5c31385c5a3bb1eb9de9fbd1f903c6f5","_from":".","_npmVersion":"1.4.10","_npmUser":{"name":"eyalar","email":"eyalarubas@gmail.com"},"maintainers":[{"name":"eyalar","email":"eyalarubas@gmail.com"}],"dist":{"shasum":"48b17ebf5c31385c5a3bb1eb9de9fbd1f903c6f5","size":5511,"noattachment":false,"key":"/decree/-/decree-0.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/decree/download/decree-0.0.1.tgz"},"directories":{},"publish_time":1404666504141,"_cnpm_publish_time":1404666504141,"_hasShrinkwrap":false}},"readme":"[![Version](http://img.shields.io/npm/v/decree.svg)](https://www.npmjs.org/package/decree)\n[![Build Status](https://api.travis-ci.org/EyalAr/Decree.svg?branch=master)](https://travis-ci.org/EyalAr/Decree)\n[![Coverage Status](https://img.shields.io/coveralls/EyalAr/Decree/master.svg)](https://coveralls.io/r/EyalAr/Decree)\n\n# Declarative arguments-resolver\n\n0. [Overview](#overview)\n    0. [Install](#install)\n    0. [Example](#example)\n0. [How to use](#how-to-use)\n    0. [Declaration structure](#declaration-structure)\n    0. [Errors](#errors)\n    0. [Built-in types](#built-in-types)\n    0. [Custom types](#custom-types)\n\n## Overview\n\nDecree is a declarative arguments-resolver. It saves you time and code when you\nneed to do arguments validation and disambiguation in your APIs.\n\nSimply declare the conditions your arguments should hold, such as their types,\nwhether they are optional and their default values. Decree will take care of the\nrest, and provide you with clean and disambiguated arguments.\n\nIf the user provided an illegal combination of arguments, Decree will tell you\nwhere was the problem.\n\n### Install\n\n0. **To use in Node, install with npm:** `npm install decree`\n0. **To use in the browser, install with bower:** `bower install decree`  \n   On the browser Decree loads as an AMD module (see\n   [`examples/amd`](examples/amd)).\n\n### Example\n\nLet's say you have a function which takes 4 arguments, some are optional. Inside\nthe function you need to:\n\n0. Verify the types of the arguments.\n0. Detect which of the arguments were provided and which were omitted.\n0. Disambiguate and assign default values to omitted arguments.\n\n**Without Decree:**\n\n```Javascript\n/**\n * Make a cup of coffee.\n * @param {number} [sugars=1] - number of sugars. non-negative decimal number. defaults to 1.\n * @param {string} [flavor='bitter'] - flavor of the coffee. defaults to 'bitter'.\n * @param {string|integer} [size='large'] - size of cup. 'small', 'medium', 'large' or a positive integer.\n * @param {function} callback - called when coffee is ready.\n */\nfunction makeCoffee(sugars, flavor, size, callback){\n    // verify arguments:\n    // was sugars provided? if not, flavor = sugars? size = flavor? callback = size?\n    // but what if flavor was not provided...?\n    // what about the callback? maybe callback = size? callback = flavor?\n    // is 'callback' a function? if not, throw an exception?\n    // ...\n    // ...\n    // ...\n    // finally:\n    if (/* arguments are valid */){\n        // make coffee...\n        callback('Coffee is ready!');\n    } else {\n        throw Error('Invalid arguments!');\n    }\n}\n```\n\n**With Decree:**\n\nSimply decalare the properties of your arguments:\n\n```Javascript\nvar decs = [{\n    name: 'sugars',\n    type: 'nn-number', // non-negative\n    optional: true,\n    default: 1\n}, {\n    name: 'flavor',\n    type: 'string',\n    optional: true,\n    default: 'bitter'\n}, {\n    name: 'size',\n    types: ['string', 'p-int'], // string or positive integer\n    optional: true,\n    default: 'large'\n}, {\n    name: 'callback',\n    type: 'function'\n}];\n```\n\nLet Decree do the rest:\n\n```Javascript\n/**\n * Make a cup of coffee.\n * @param {number} [sugars=1] - number of sugars. non-negative decimal number. defaults to 1.\n * @param {string} [flavor='bitter'] - flavor of the coffee. defaults to 'bitter'.\n * @param {string|integer} [size='large'] - size of cup. 'small', 'medium', 'large' or a positive integer.\n * @param {function} callback - called when coffee is ready.\n */\nfunction makeCoffee() {\n    decree(decs)(arguments, function(sugars, flavor, size, callback) {\n        // arguments are disambiguated and ready to be used.\n        // make coffee...\n        callback('Coffee is ready!');\n    });\n};\n```\n\nNow use your function as usual:\n\n```Javascript\nmakeCoffee(1.5, function(msg){\n    console.log(msg); // 'Coffee is ready!'\n});\n```\n\n## How to use\n\nDecree needs to know what you expect. Simply build an array to describe your\narguments expectations.\n\n```Javascript\n// declarations:\nvar decs = [{/* arg 1 */}, {/* arg 2 */}, {/* arg 3 */}, ...];\n```\n\nEach item in the array is an object which describes an argument. See\n[declaration structure](#declaration-structure).\n\nWhen finished declaring your expectations, use Decree to resolve an array of\narguments. Calling `decree(...)` will construct a function which receives an\narray of arguments and disambiguates it.\n\n```Javascript\nvar judge = decree(decs); // `judge` is a function\n```\n\nThe constructed *judge* function has the following signature:\n\n`function judge(args, callback, errCallback)`\n\n0. `args {Array}` - The array of arguments to disambiguate.\n0. `callback {Function}` - an optional callback function which is called with\n   the disambiguated arguments.\n0. `errCallback {Function}` - an optional callback to handle arguments errors.\n   This function is called with the error, if there was an error disambiguating\n   the arguments array.\n\n**Note:**\n\n* If `errCallback` is omitted, and there is an error, an exception will be\n  thrown.\n* If `callback` is omitted as well, `judge` will return the array of\n  disambiguated arguments.\n\n```Javascript\nvar decree = require('decree');\nvar decs = [ /* declarations of foo's arguments */ ];\nvar judge = decree(decs);\n\nfunction foo() {\n    // pass your function's arguments directly to decree:\n    judge(arguments, function(arg1, arg2, arg3, ...) {\n        // here you can be sure your arguments are of\n        // the correct types and values.\n    }, function(err){\n        // there was a problem with the provided arguments.\n        // log it, and throw an exception to the user.\n        console.log(err);\n        throw err;\n    });\n}\n\n// use foo as normal:\nfoo( ... );\n```\n\n### Declaration structure\n\nWhen declaring an argument, tell Decree:\n\n0. `name {String}`: **Optional**. Will be used to identify the argument in error\n   messages.\n0. `type {String}` / `types{Array[String]}`: **Required**.\n   See [built-in types](#built-in-types) or [custom types](#custom-types).\n0. `optional {Boolean}`: **Optional**. Is this argument optional?\n   Defaults to `false`.\n0. `default`: **Optional**. If the argument is optional, this default value will\n   be assigned if no value is provided.\n\n```Javascript\n{\n    name: ...,\n    types: [ ... ],\n    optional: ...,\n    default: ...\n}\n```\n\n**Note:** If an optional argument has no default value, and that argument is\nomitted by the user, decree will assign this argument `undefined`.\n\n### Errors\n\nWhen there is a problem with the arguments Decree can provide a detailed\nexplanation of what went wrong. By default, an error object will be thrown,\nunless you provide a second callback which is called with the error.\n\n```Javascript\nvar decree = require('decree');\n\n// with an exception:\nfunction foo() {\n    try {\n        decree(/* decs */)(arguments, function(/* args */) {\n            // ...\n        });\n    } catch (err) {\n        // if here, there was a problem with the arguments the user passed.\n        // 'err' contains the information you need\n    }\n}\n\n// or, with an error handling callback:\nfunction foo() {\n    decree(/* decs */)(arguments, function(/* args */) {\n        // ...\n    }, function(err) {\n        // if here, there was a problem with the arguments the user passed.\n        // 'err' contains the information you need\n    });\n}\n```\n\n### Built-in types\n\nDecree supports several argument types:\n\n- `*`: Argument matches any type.\n- `array`\n- `function`\n- `hash`: Argument is a simple key-value object.\n- `string`\n- `regexp`: Argument is a regular expression.\n- `boolean`\n- `date`: Argument is a Date object.\n- `number`\n- `n-number`: Argument is a negative number\n- `p-number`: Argument is a positive number\n- `nn-number`: Argument is a non-negative number\n- `np-number`: Argument is a non-positive number\n- `int`: Argument is an integer\n- `n-int`: Argument is a negative integer\n- `p-int`: Argument is a positive integer\n- `nn-int`: Argument is a non-negative integer\n- `np-int`: Argument is a non-positive integer\n\n### Custom types\n\nRegister a custom type with:\n\n`decree.resigter(name, validator)`\n\n0. `name {String}` - The name of the new type.\n0. `validator {Function}` - A validation function. Receives a value and should\n   return `true` or `false`.\n\n**Example:**\n\n```Javascript\nvar decree = require('decree');\n\n// register a 'color' type:\ndecree.register('color',function(v){\n    return [\"blue\", \"red\", \"green\", \"yellow\"].indexOf(v) !== -1;\n});\n\n// use it:\nvar decs = [{\n    type: 'color',\n    optional: true,\n    default: \"blue\"\n}];\n```\n","_attachments":{},"homepage":"https://github.com/EyalAr/Decree","bugs":{"url":"https://github.com/EyalAr/Decree/issues"},"license":"MIT"}