From b0dd02151078c72a7dd1c946bfebab92fe3753d6 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Fri, 24 Jun 2016 11:19:05 -0400 Subject: [PATCH] [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) --- src/number.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/number.js b/src/number.js index a49a09c98..a37d11526 100644 --- a/src/number.js +++ b/src/number.js @@ -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)