UNPKG

acl2

Version:

An Access Control List module based on memory, Redis, or MongoDB with Express middleware support

119 lines (101 loc) 2.68 kB
/* Design by Contract module (c) OptimalBits 2011. Roadmap: - Optional parameters. ['(string)', 'array'] - Variable number of parameters.['number','...'] api?: contract(arguments) .params('string', 'array', '...') .params('number') .end() */ const noop = {}; const _ = require("lodash"); noop.params = function () { return this; }; noop.end = function () {}; const contract = function (args) { if (contract.debug === true) { contract.fulfilled = false; contract.args = _.toArray(args); contract.checkedParams = []; return contract; } else { return noop; } }; contract.params = function () { this.fulfilled |= checkParams(this.args, _.toArray(arguments)); if (this.fulfilled) { return noop; } else { this.checkedParams.push(arguments); return this; } }; contract.end = function () { if (!this.fulfilled) { printParamsError(this.args, this.checkedParams); throw new Error("Broke parameter contract"); } }; const typeOf = function (obj) { return Array.isArray(obj) ? "array" : typeof obj; }; const checkParams = function (args, contract) { let fulfilled, types, type, i, j; if (args.length !== contract.length) { return false; } else { for (i = 0; i < args.length; i++) { try { types = contract[i].split("|"); } catch (e) { console.log(e, args); } if (args[i]) { type = typeOf(args[i]); fulfilled = false; for (j = 0; j < types.length; j++) { if (type === types[j]) { fulfilled = true; break; } } } if (fulfilled === false) { return false; } } return true; } }; const printParamsError = function (args, checkedParams) { let msg = "Parameter mismatch.\nInput:\n( ", type, i; _.each(args, function (input, key) { type = typeOf(input); if (key !== 0) { msg += ", "; } msg += input + ": " + type; }); msg += ")\nAccepted:\n"; for (i = 0; i < checkedParams.length; i++) { msg += "(" + argsToString(checkedParams[i]) + ")\n"; } console.log(msg); }; const argsToString = function (args) { let res = ""; _.each(args, function (arg, key) { if (key !== 0) { res += ", "; } res += arg; }); return res; }; exports = module.exports = contract;