From fefcf91924d1662fd50f6deb6cb4adda36b4a69a Mon Sep 17 00:00:00 2001 From: Lesha Koss Date: Tue, 4 Apr 2017 20:23:40 +0600 Subject: [PATCH] `toDate` implementation fix --- src/toDate/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/toDate/index.js b/src/toDate/index.js index e779f5ece6..6412049819 100644 --- a/src/toDate/index.js +++ b/src/toDate/index.js @@ -62,6 +62,7 @@ var patterns = { * @param {Options} [options] - the object with options. See [Options]{@link docs/Options} * @param {0|1|2} [options.additionalDigits=2] - the additional number of digits in the extended year format * @returns {Date} the parsed date in the local time zone + * @throws {RangeError} additionalDigits must be 0, 1 or 2 * * @example * // Convert string '2014-02-11T11:30:30' to date: @@ -75,6 +76,13 @@ var patterns = { * //=> Fri Apr 11 2014 00:00:00 */ export default function toDate (argument, dirtyOptions) { + var options = dirtyOptions || {} + + var additionalDigits = options.additionalDigits === undefined ? DEFAULT_ADDITIONAL_DIGITS : Number(options.additionalDigits) + if (additionalDigits !== 2 && additionalDigits !== 1 && additionalDigits !== 0) { + throw new RangeError('additionalDigits must be 0, 1 or 2') + } + // Clone the date if (isDate(argument)) { // Prevent the date to lose the milliseconds when passed to new Date() in IE10 @@ -83,14 +91,6 @@ export default function toDate (argument, dirtyOptions) { return new Date(argument) } - var options = dirtyOptions || {} - var additionalDigits = options.additionalDigits - if (additionalDigits == null) { - additionalDigits = DEFAULT_ADDITIONAL_DIGITS - } else { - additionalDigits = Number(additionalDigits) - } - var dateStrings = splitDateString(argument) var parseYearResult = parseYear(dateStrings.date, additionalDigits)