este-library-oldschool
Version:
Library for github.com/steida/este.git
81 lines (74 loc) • 2.23 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
/**
@fileoverview Validate number or string number range.
*/
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.superClass_ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
goog.provide('este.validators.Range');
goog.provide('este.validators.range');
goog.require('este.validators.Base');
goog.require('este.validators.min');
goog.require('este.validators.max');
/**
@param {number} min
@param {number} max
@param {function(): string=} getMsg
@constructor
@extends {este.validators.Base}
*/
este.validators.Range = function(min1, max1, getMsg) {
this.min = min1;
this.max = max1;
este.validators.Range.superClass_.constructor.call(this, getMsg);
}
extend(este.validators.Range, superClass);
/**
@type {number}
@protected
*/
este.validators.Range.prototype.min = 0;
/**
@type {number}
@protected
*/
este.validators.Range.prototype.max = 0;
/**
@override
*/
este.validators.Range.prototype.validate = function() {
var isStringOrNumber, ref, value;
isStringOrNumber = (ref = typeof this.value) === 'string' || ref === 'number';
goog.asserts.assert(isStringOrNumber, 'Expected string or number.');
value = this.value;
if (goog.isString(value)) {
value = goog.string.toNumber(value);
}
value = /** @type {number} */(value);
if (!goog.math.isFiniteNumber(value)) {
return false;
}
return (this.min <= value && value <= this.max);
};
/**
@override
*/
este.validators.Range.prototype.getMsg = function() {
/**
@desc Range validator message.
*/
return este.validators.Range.MSG_VALIDATOR_RANGE = goog.getMsg('Please enter a value between {$min} and {$max}.', {
'min': this.min,
'max': this.max
});
};
/**
@param {number} min
@param {number} max
@param {function(): string=} getMsg
@return {function(): este.validators.Range}
*/
este.validators.range = function(min, max, getMsg) {
return function() {
return new este.validators.Range(min, max, getMsg);
};
};