Skip to content

Instantly share code, notes, and snippets.

@yjaaidi
Created November 17, 2015 15:38
Show Gist options
  • Save yjaaidi/b254f7e07235dd26d9e9 to your computer and use it in GitHub Desktop.
Save yjaaidi/b254f7e07235dd26d9e9 to your computer and use it in GitHub Desktop.
// Generated by CoffeeScript 1.4.0
(function() {
var NamedParameters, exports;
NamedParameters = (function() {
var _assert, _clone;
function NamedParameters(m) {
this.map = _clone(m);
this.coerce_all = false;
}
NamedParameters.prototype.values = function() {
return this.map;
};
NamedParameters.prototype["default"] = function(name, value) {
if (typeof name === 'object' && !(value != null)) {
this.defaults(name);
} else {
if (this.map[name] == null) {
this.map[name] = value;
}
}
return this;
};
NamedParameters.prototype.defaults = function(map) {
var n, v;
for (n in map) {
v = map[n];
this["default"](n, v);
}
return this;
};
NamedParameters.prototype.coerce = function(name, type, whennull) {
var n, v, value;
if (whennull == null) {
whennull = null;
}
if (typeof name === 'boolean') {
this.coerce_all = name;
} else if (typeof name === 'object') {
for (n in name) {
v = name[n];
this.coerce(n, v);
}
} else {
value = this.map[name];
switch (type) {
case 'integer':
if (value != null) {
this.map[name] = Math.floor(Number(value));
} else {
this.map[name] = Math.floor(Number(whennull));
}
break;
case 'number':
if (value != null) {
this.map[name] = Number(value);
} else {
this.map[name] = Number(whennull);
}
break;
case 'string':
if (value != null) {
this.map[name] = String(value);
} else {
this.map[name] = String(whennull);
}
break;
case 'boolean':
if (typeof value === 'string') {
v = value.toLowerCase();
this.map[name] = v === 'true' || v === 't' || v === 'yes' || v === 'y' || v === '1' || v === 'on';
} else if (typeof value === 'function') {
this.map[name] = value();
} else {
this.map[name] = value ? true : false;
}
break;
case 'array':
if (value != null) {
if (!(value instanceof Array)) {
this.map[name] = [value];
}
} else if (whennull != null) {
if (!(whennull instanceof Array)) {
this.map[name] = [whennull];
}
}
break;
default:
throw "I don't know how to coerce parameter \"" + name + "\" into the unrecognized type \"" + type + "\".";
}
}
return this;
};
NamedParameters.prototype.require = function(name, validation, message) {
var value;
if (validation == null) {
validation = null;
}
if (message == null) {
message = null;
}
switch (typeof validation) {
case 'string':
switch (validation.toLowerCase()) {
case 'non empty string':
case 'non-empty string':
case 'non blank string':
case 'non-blank string':
if (this.coerce_all) {
this.coerce(name, 'string');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-empty String. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'string') && value.length > 0, message);
break;
case 'non empty array':
case 'non-empty array':
if (this.coerce_all) {
this.coerce(name, 'array');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-empty Array. Found \"" + value + "\".";
}
_assert((value != null) && (value instanceof Array) && value.length > 0, message);
break;
case 'number':
case 'numeric':
if (this.coerce_all) {
this.coerce(name, 'number');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a numeric value. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number'), message);
break;
case 'positive number':
case 'positive':
if (this.coerce_all) {
this.coerce(name, 'number');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a positive number. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && value > 0, message);
break;
case 'negative number':
case 'negative':
if (this.coerce_all) {
this.coerce(name, 'number');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a negative number. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && value < 0, message);
break;
case 'non-negative number':
case 'non negative number':
case 'non negative':
case 'non-negative':
if (this.coerce_all) {
this.coerce(name, 'number');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-negative number. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && value >= 0, message);
break;
case 'non-positive number':
case 'non positive number':
case 'non-positive':
case 'non positive':
if (this.coerce_all) {
this.coerce(name, 'number');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-positive number. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && value <= 0, message);
break;
case 'non-zero number':
case 'non zero number':
case 'non-zero':
case 'non zero':
if (this.coerce_all) {
this.coerce(name, 'number');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-zero number. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && value !== 0, message);
break;
case 'integer':
if (this.coerce_all) {
this.coerce(name, 'integer');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be an integer. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && Math.floor(value) === value, message);
break;
case 'positive integer':
if (this.coerce_all) {
this.coerce(name, 'integer');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a positive integer. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && Math.floor(value) === value && value > 0, message);
break;
case 'negative integer':
if (this.coerce_all) {
this.coerce(name, 'integer');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a negative integer. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && Math.floor(value) === value && value < 0, message);
break;
case 'non-negative integer':
if (this.coerce_all) {
this.coerce(name, 'integer');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-negative integer. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && Math.floor(value) === value && value >= 0, message);
break;
case 'non-positive integer':
if (this.coerce_all) {
this.coerce(name, 'integer');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-positive integer. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && Math.floor(value) === value && value <= 0, message);
break;
case 'non-zero integer':
if (this.coerce_all) {
this.coerce(name, 'integer');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-zero integer. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'number') && Math.floor(value) === value && value !== 0, message);
break;
case 'string':
if (this.coerce_all) {
this.coerce(name, 'string');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-null string. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'string'), message);
break;
case 'object':
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-null Object. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'object'), message);
break;
case 'array':
if (this.coerce_all) {
this.coerce(name, 'array');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-null Array. Found \"" + value + "\".";
}
_assert((value != null) && (value instanceof Array), message);
break;
case 'boolean':
if (this.coerce_all) {
this.coerce(name, 'boolean');
}
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be a non-null boolean. Found \"" + value + "\".";
}
_assert((value != null) && (typeof value === 'boolean'), message);
break;
case 'not null':
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be non-null value. Found (\"" + value + "\").";
}
_assert(value != null, message);
break;
default:
throw "Unrecognized validator string \"" + validation + "\".";
}
break;
case 'function':
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter (\"" + value + "\") to pass validation " + validation + ".";
}
_assert(validation(name), message);
break;
case 'object':
if (validation != null) {
throw "Unrecognized validator object \"" + validation + "\".";
} else {
value = this.map[name];
if (message == null) {
message = "Expected " + name + " parameter to be non-null value. Found (\"" + value + "\").";
}
_assert(value != null, message);
}
break;
default:
throw "Unrecognized validator string \"" + validation + "\".";
}
return this;
};
NamedParameters.prototype.demand = function(name, validation, message) {
return this.require(name, validation, message);
};
_clone = function(obj) {
var clone, n, v;
clone = {};
for (n in obj) {
v = obj[n];
if (obj.hasOwnProperty(n)) {
clone[n] = v;
}
}
return clone;
};
_assert = function(bool, message) {
if (message == null) {
message = "Invalid parameter";
}
if (!bool) {
throw message;
}
};
return NamedParameters;
})();
exports = exports != null ? exports : this;
exports.NamedParameters = NamedParameters;
exports.parse = function(map) {
return new NamedParameters(map);
};
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment