Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ---------- extends ----------
- Object.prototype.extends = function (parent) {
- this.prototype = Object.create(parent.prototype);
- this.prototype.constructor = this;
- }
- // ---------- is Number ----------
- Number.prototype.validateIsNumber = function (value, variableName, minValue, maxValue) {
- if (typeof (value) != 'number') {
- throw new Error(variableName + " should be a number!");
- }
- }
- // ---------- is Integer ----------
- Number.prototype.isInteger = function isInteger(x) {
- return ((x ^ 0) === x);
- }
- // ---------- validate integer range ----------
- Number.prototype.validateIntegerRange = function (value, variableName, minValue, maxValue) {
- if (typeof (value) != 'number') {
- throw new Error(variableName + " should be a number!");
- }
- if (value !== parseInt(value, 10)) {
- throw new Error(variableName + " should be integer!");
- }
- if (value < minValue || value > maxValue) {
- throw new Error(variableName + " should be integer in the range [" + minValue + "..." + maxValue + "]!");
- }
- }
- // ---------- validate non-empty string ----------
- String.prototype.validateNonEmptyString = function (value, variableName) {
- if (typeof (value) != 'string' || !value) {
- throw new Error(variableName + " should be non-empty string!");
- }
- }
- // ---------- validate boolean ----------
- Boolean.prototype.validateBoolean = function (value, variableName) {
- if (typeof (value) != 'boolean') {
- throw new Error(variableName + " should be a boolean value!");
- }
- }
- // ---------- validate non-empty object ----------
- Object.prototype.validateNonEmptyObject = function (value, className, variableName) {
- if (! (value instanceof className)) {
- throw new Error(variableName + " should be non-empty " + className.prototype.constructor.name + "!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment