UNPKG

jaywalk

Version:
68 lines 2.22 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var any_1 = require('./any'); var utils_1 = require('../utils'); var Number = (function (_super) { __extends(Number, _super); function Number(options) { if (options === void 0) { options = {}; } _super.call(this, options); this.type = 'Number'; if (options.min != null) { this.min = options.min; } if (options.max != null) { this.max = options.max; } this._tests.push(isNumber); this._tests.push(toMinTest(this.min)); this._tests.push(toMaxTest(this.max)); } Number.prototype._isType = function (value, path, context) { return utils_1.wrapIsType(this, value, path, context, _super.prototype._isType, function (value) { if (typeof value === 'number') { return 1; } throw context.error(path, 'Number', 'type', 'Number', value); }); }; return Number; }(any_1.Any)); exports.Number = Number; function isNumber(value, path, context, next) { if (typeof value !== 'number' || !isFinite(value)) { throw context.error(path, 'Number', 'type', 'Number', value); } return next(value); } function toMinTest(min) { if (min == null) { return utils_1.toNext; } var minValue = utils_1.toValue(min); return function (value, path, context, next) { var min = minValue(path, context); if (value < min) { throw context.error(path, 'Number', 'min', min, value); } return next(value); }; } function toMaxTest(max) { if (max == null) { return utils_1.toNext; } var maxValue = utils_1.toValue(max); return function (value, path, context, next) { var max = maxValue(path, context); if (value > max) { throw context.error(path, 'Number', 'max', max, value); } return next(value); }; } //# sourceMappingURL=number.js.map