UNPKG

3.94 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/builtin/interopRequireDefault");
4
5exports.__esModule = true;
6exports.default = NumberSchema;
7
8var _inherits = _interopRequireDefault(require("./util/inherits"));
9
10var _mixed = _interopRequireDefault(require("./mixed"));
11
12var _locale = require("./locale");
13
14var _isAbsent = _interopRequireDefault(require("./util/isAbsent"));
15
16var isNaN = function isNaN(value) {
17 return value != +value;
18};
19
20var isInteger = function isInteger(val) {
21 return (0, _isAbsent.default)(val) || val === (val | 0);
22};
23
24function NumberSchema() {
25 var _this = this;
26
27 if (!(this instanceof NumberSchema)) return new NumberSchema();
28
29 _mixed.default.call(this, {
30 type: 'number'
31 });
32
33 this.withMutation(function () {
34 _this.transform(function (value) {
35 if (this.isType(value)) return value;
36 var parsed = parseFloat(value);
37 if (this.isType(parsed)) return parsed;
38 return NaN;
39 });
40 });
41}
42
43(0, _inherits.default)(NumberSchema, _mixed.default, {
44 _typeCheck: function _typeCheck(value) {
45 if (value instanceof Number) value = value.valueOf();
46 return typeof value === 'number' && !isNaN(value);
47 },
48 min: function min(_min, message) {
49 if (message === void 0) {
50 message = _locale.number.min;
51 }
52
53 return this.test({
54 message: message,
55 name: 'min',
56 exclusive: true,
57 params: {
58 min: _min
59 },
60 test: function test(value) {
61 return (0, _isAbsent.default)(value) || value >= this.resolve(_min);
62 }
63 });
64 },
65 max: function max(_max, message) {
66 if (message === void 0) {
67 message = _locale.number.max;
68 }
69
70 return this.test({
71 message: message,
72 name: 'max',
73 exclusive: true,
74 params: {
75 max: _max
76 },
77 test: function test(value) {
78 return (0, _isAbsent.default)(value) || value <= this.resolve(_max);
79 }
80 });
81 },
82 lessThan: function lessThan(less, message) {
83 if (message === void 0) {
84 message = _locale.number.lessThan;
85 }
86
87 return this.test({
88 message: message,
89 name: 'max',
90 exclusive: true,
91 params: {
92 less: less
93 },
94 test: function test(value) {
95 return (0, _isAbsent.default)(value) || value < this.resolve(less);
96 }
97 });
98 },
99 moreThan: function moreThan(more, message) {
100 if (message === void 0) {
101 message = _locale.number.moreThan;
102 }
103
104 return this.test({
105 message: message,
106 name: 'min',
107 exclusive: true,
108 params: {
109 more: more
110 },
111 test: function test(value) {
112 return (0, _isAbsent.default)(value) || value > this.resolve(more);
113 }
114 });
115 },
116 positive: function positive(msg) {
117 if (msg === void 0) {
118 msg = _locale.number.positive;
119 }
120
121 return this.min(0, msg);
122 },
123 negative: function negative(msg) {
124 if (msg === void 0) {
125 msg = _locale.number.negative;
126 }
127
128 return this.max(0, msg);
129 },
130 integer: function integer(message) {
131 if (message === void 0) {
132 message = _locale.number.integer;
133 }
134
135 return this.test({
136 name: 'integer',
137 message: message,
138 test: isInteger
139 });
140 },
141 truncate: function truncate() {
142 return this.transform(function (value) {
143 return !(0, _isAbsent.default)(value) ? value | 0 : value;
144 });
145 },
146 round: function round(method) {
147 var avail = ['ceil', 'floor', 'round', 'trunc'];
148 method = method && method.toLowerCase() || 'round'; // this exists for symemtry with the new Math.trunc
149
150 if (method === 'trunc') return this.truncate();
151 if (avail.indexOf(method.toLowerCase()) === -1) throw new TypeError('Only valid options for round() are: ' + avail.join(', '));
152 return this.transform(function (value) {
153 return !(0, _isAbsent.default)(value) ? Math[method](value) : value;
154 });
155 }
156});
157module.exports = exports["default"];
\No newline at end of file