Skip to content

Commit

Permalink
[changed] Split integer(), remove transform
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jquense committed Jun 24, 2016
1 parent 758ac51 commit b0dd021
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,25 @@ inherits(NumberSchema, MixedSchema, {
},

integer(msg) {
msg = msg || locale.integer
msg = msg || locale.integer;

return this
.transform(value => !isAbsent(value) ? (value | 0) : value)
.test('integer', msg, isInteger)
return this.test('integer', msg, isInteger)
},

truncate() {
return this.transform(value =>
!isAbsent(value) ? (value | 0) : value)
},

round(method) {
var avail = ['ceil', 'floor', 'round']
var avail = ['ceil', 'floor', 'round', 'trunc']
method = (method && method.toLowerCase()) || 'round'

if( avail.indexOf(method.toLowerCase()) === -1 )
// this exists for symemtry with the new Math.trunc
if (method === 'trunc')
return this.truncate()

if (avail.indexOf(method.toLowerCase()) === -1)
throw new TypeError('Only valid options for round() are: ' + avail.join(', '))

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

0 comments on commit b0dd021

Please sign in to comment.