@@ -9,86 +9,123 @@ let isNaN = value => value != +value
9
9
let isInteger = val => isAbsent ( val ) || val === ( val | 0 )
10
10
11
11
export default function NumberSchema ( ) {
12
- if ( ! ( this instanceof NumberSchema ) )
13
- return new NumberSchema ( )
12
+ if ( ! ( this instanceof NumberSchema ) )
13
+ return new NumberSchema ( )
14
14
15
- MixedSchema . call ( this , { type : 'number' } )
15
+ MixedSchema . call ( this , { type : 'number' } )
16
16
17
- this . withMutation ( ( ) => {
18
- this . transform ( function ( value ) {
19
- if ( this . isType ( value ) ) return value
17
+ this . withMutation ( ( ) => {
18
+ this . transform ( function ( value ) {
19
+ if ( this . isType ( value ) ) return value
20
20
21
- let parsed = parseFloat ( value ) ;
22
- if ( this . isType ( parsed ) ) return parsed
21
+ let parsed = parseFloat ( value ) ;
22
+ if ( this . isType ( parsed ) ) return parsed
23
23
24
- return NaN ;
25
- } )
26
- } )
24
+ return NaN ;
25
+ } )
26
+ } )
27
27
}
28
28
29
29
inherits ( NumberSchema , MixedSchema , {
30
30
31
- _typeCheck ( value ) {
32
- if ( value instanceof Number )
33
- value = value . valueOf ( ) ;
34
-
35
- return typeof value === 'number' && ! isNaN ( value )
36
- } ,
37
-
38
- min ( min , msg ) {
39
- return this . test ( {
40
- name : 'min' ,
41
- exclusive : true ,
42
- params : { min } ,
43
- message : msg || locale . min ,
44
- test ( value ) {
45
- return isAbsent ( value ) || value >= this . resolve ( min )
46
- }
47
- } )
48
- } ,
49
-
50
- max ( max , msg ) {
51
- return this . test ( {
52
- name : 'max' ,
53
- exclusive : true ,
54
- params : { max } ,
55
- message : msg || locale . max ,
56
- test ( value ) {
57
- return isAbsent ( value ) || value <= this . resolve ( max )
58
- }
59
- } )
60
- } ,
61
-
62
- positive ( msg ) {
63
- return this . min ( 0 , msg || locale . positive )
64
- } ,
65
-
66
- negative ( msg ) {
67
- return this . max ( 0 , msg || locale . negative )
68
- } ,
69
-
70
- integer ( msg ) {
71
- msg = msg || locale . integer ;
72
-
73
- return this . test ( 'integer' , msg , isInteger )
74
- } ,
75
-
76
- truncate ( ) {
77
- return this . transform ( value =>
78
- ! isAbsent ( value ) ? ( value | 0 ) : value )
79
- } ,
80
-
81
- round ( method ) {
82
- var avail = [ 'ceil' , 'floor' , 'round' , 'trunc' ]
83
- method = ( method && method . toLowerCase ( ) ) || 'round'
84
-
85
- // this exists for symemtry with the new Math.trunc
86
- if ( method === 'trunc' )
87
- return this . truncate ( )
88
-
89
- if ( avail . indexOf ( method . toLowerCase ( ) ) === - 1 )
90
- throw new TypeError ( 'Only valid options for round() are: ' + avail . join ( ', ' ) )
91
-
92
- return this . transform ( value => ! isAbsent ( value ) ? Math [ method ] ( value ) : value )
93
- }
31
+ _typeCheck ( value ) {
32
+ if ( value instanceof Number )
33
+ value = value . valueOf ( ) ;
34
+
35
+ return typeof value === 'number' && ! isNaN ( value )
36
+ } ,
37
+
38
+ min ( min , msg ) {
39
+ return this . test ( {
40
+ name : 'min' ,
41
+ exclusive : true ,
42
+ params : { min } ,
43
+ message : msg || locale . min ,
44
+ test ( value ) {
45
+ return isAbsent ( value ) || value >= this . resolve ( min )
46
+ }
47
+ } )
48
+ } ,
49
+
50
+ max ( max , msg ) {
51
+ return this . test ( {
52
+ name : 'max' ,
53
+ exclusive : true ,
54
+ params : { max } ,
55
+ message : msg || locale . max ,
56
+ test ( value ) {
57
+ return isAbsent ( value ) || value <= this . resolve ( max )
58
+ }
59
+ } )
60
+ } ,
61
+
62
+ less ( less , msg ) {
63
+ return this . test ( {
64
+ name : 'less' ,
65
+ exclusive : true ,
66
+ params : { less } ,
67
+ message : msg || locale . less ,
68
+ test ( value ) {
69
+ return isAbsent ( value ) || value < this . resolve ( less )
70
+ }
71
+ } )
72
+ } ,
73
+
74
+
75
+ more ( more , msg ) {
76
+ return this . test ( {
77
+ name : 'more' ,
78
+ exclusive : true ,
79
+ params : { more } ,
80
+ message : msg || locale . more ,
81
+ test ( value ) {
82
+ return isAbsent ( value ) || value > this . resolve ( more )
83
+ }
84
+ } )
85
+ } ,
86
+
87
+ notEqual ( notEqual , msg ) {
88
+ return this . test ( {
89
+ name : 'notEqual' ,
90
+ exclusive : true ,
91
+ params : { notEqual } ,
92
+ message : msg || locale . notEqual ,
93
+ test ( value ) {
94
+ return isAbsent ( value ) || value !== this . resolve ( notEqual )
95
+ }
96
+ } )
97
+ } ,
98
+
99
+ positive ( msg ) {
100
+ return this . min ( 0 , msg || locale . positive )
101
+ } ,
102
+
103
+ negative ( msg ) {
104
+ return this . max ( 0 , msg || locale . negative )
105
+ } ,
106
+
107
+ integer ( msg ) {
108
+ msg = msg || locale . integer ;
109
+
110
+ return this . test ( 'integer' , msg , isInteger )
111
+ } ,
112
+
113
+ truncate ( ) {
114
+ return this . transform ( value =>
115
+ ! isAbsent ( value ) ? ( value | 0 ) : value )
116
+ } ,
117
+
118
+ round ( method ) {
119
+ var avail = [ 'ceil' , 'floor' , 'round' , 'trunc' ]
120
+ method = ( method && method . toLowerCase ( ) ) || 'round'
121
+
122
+ // this exists for symemtry with the new Math.trunc
123
+ if ( method === 'trunc' )
124
+ return this . truncate ( )
125
+
126
+ if ( avail . indexOf ( method . toLowerCase ( ) ) === - 1 )
127
+ throw new TypeError ( 'Only valid options for round() are: ' + avail . join ( ', ' ) )
128
+
129
+ return this . transform ( value => ! isAbsent ( value ) ? Math [ method ] ( value ) : value )
130
+ }
94
131
} )
0 commit comments