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