Skip to content

Commit b0dd021

Browse files
committed
[changed] Split integer(), remove transform
integer() no longer adds a type coercion, and now only validates the input is an integer. the truncate() method has been added to coerce inputs to integers, via truncation (the same behavior as integer before)
1 parent 758ac51 commit b0dd021

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/number.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,25 @@ inherits(NumberSchema, MixedSchema, {
6969
},
7070

7171
integer(msg) {
72-
msg = msg || locale.integer
72+
msg = msg || locale.integer;
7373

74-
return this
75-
.transform(value => !isAbsent(value) ? (value | 0) : value)
76-
.test('integer', msg, isInteger)
74+
return this.test('integer', msg, isInteger)
75+
},
76+
77+
truncate() {
78+
return this.transform(value =>
79+
!isAbsent(value) ? (value | 0) : value)
7780
},
7881

7982
round(method) {
80-
var avail = ['ceil', 'floor', 'round']
83+
var avail = ['ceil', 'floor', 'round', 'trunc']
8184
method = (method && method.toLowerCase()) || 'round'
8285

83-
if( avail.indexOf(method.toLowerCase()) === -1 )
86+
// this exists for symemtry with the new Math.trunc
87+
if (method === 'trunc')
88+
return this.truncate()
89+
90+
if (avail.indexOf(method.toLowerCase()) === -1)
8491
throw new TypeError('Only valid options for round() are: ' + avail.join(', '))
8592

8693
return this.transform(value => !isAbsent(value) ? Math[method](value) : value)

0 commit comments

Comments
 (0)