diff --git a/CHANGELOG.md b/CHANGELOG.md index f5bf88a625..25454d7bd8 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -683,6 +683,11 @@ for the list of changes made since `v2.0.0-alpha.1`. are not `undefined` or have expected values. This change is introduced for consistency with ECMAScript standard library which does the same. +- **BREAKING**: `format`, `formatDistance` (previously `distanceInWords`) and + `formatDistanceStrict` (previously `distanceInWordsStrict`) now throw + `RangeError` if one the passed arguments is invalid. It reflects behavior of + `toISOString` and Intl API. See [#1032](https://github.com/date-fns/date-fns/pull/1032). + - **BREAKING**: all functions now implicitly convert arguments by following rules: | | date | number | string | boolean | @@ -712,8 +717,7 @@ for the list of changes made since `v2.0.0-alpha.1`. - `false` for functions that return booleans (expect `isValid`); - `Invalid Date` for functions that return dates; - - `NaN` for functions that return numbers; - - and `String('Invalid Date')` for functions that return strings. + - and `NaN` for functions that return numbers. See tests and PRs [#460](https://github.com/date-fns/date-fns/pull/460) and [#765](https://github.com/date-fns/date-fns/pull/765) for exact behavior. diff --git a/src/format/index.js b/src/format/index.js index 16b7be0779..daee232bdb 100644 --- a/src/format/index.js +++ b/src/format/index.js @@ -385,7 +385,7 @@ export default function format(dirtyDate, dirtyFormatStr, dirtyOptions) { var originalDate = toDate(dirtyDate) if (!isValid(originalDate)) { - return 'Invalid Date' + throw new RangeError('Invalid time value') } // Convert the date in system timezone to the same date in UTC+00:00 timezone. diff --git a/src/format/test.js b/src/format/test.js index 6a02f305dd..c8585411df 100644 --- a/src/format/test.js +++ b/src/format/test.js @@ -641,8 +641,11 @@ describe('format', function() { }) describe('edge cases', function() { - it("returns String('Invalid Date') if the date isn't valid", function() { - assert(format(new Date(NaN), 'MMMM d, yyyy') === 'Invalid Date') + it('throws RangeError if the time value is invalid', () => { + assert.throws( + format.bind(null, new Date(NaN), 'MMMM d, yyyy'), + RangeError + ) }) it('handles dates before 100 AD', function() { diff --git a/src/formatDistance/index.js b/src/formatDistance/index.js index 96bfda5fa9..ab6418ac69 100644 --- a/src/formatDistance/index.js +++ b/src/formatDistance/index.js @@ -134,7 +134,7 @@ export default function formatDistance(dirtyDate, dirtyBaseDate, dirtyOptions) { var comparison = compareAsc(dirtyDate, dirtyBaseDate) if (isNaN(comparison)) { - return 'Invalid Date' + throw new RangeError('Invalid time value') } var localizeOptions = cloneObject(options) diff --git a/src/formatDistance/test.js b/src/formatDistance/test.js index 9b2767fa28..604f0ea9c6 100644 --- a/src/formatDistance/test.js +++ b/src/formatDistance/test.js @@ -274,19 +274,25 @@ describe('formatDistance', function() { }) }) - it("returns String('Invalid Date') if the first date is `Invalid Date`", function() { - var result = formatDistance(new Date(NaN), new Date(1986, 3, 7, 10, 32, 0)) - assert(result === 'Invalid Date') + it('throws RangeError if the first date is `Invalid Date`', function() { + assert.throws( + formatDistance.bind(null, new Date(NaN), new Date(1986, 3, 7, 10, 32, 0)), + RangeError + ) }) - it("returns String('Invalid Date') if the second date is `Invalid Date`", function() { - var result = formatDistance(new Date(1986, 3, 4, 10, 32, 0), new Date(NaN)) - assert(result === 'Invalid Date') + it('throws RangeError if the second date is `Invalid Date`', function() { + assert.throws( + formatDistance.bind(null, new Date(1986, 3, 4, 10, 32, 0), new Date(NaN)), + RangeError + ) }) - it("returns String('Invalid Date') if the both dates are `Invalid Date`", function() { - var result = formatDistance(new Date(NaN), new Date(NaN)) - assert(result === 'Invalid Date') + it('throws RangeError if the both dates are `Invalid Date`', function() { + assert.throws( + formatDistance.bind(null, new Date(NaN), new Date(NaN)), + RangeError + ) }) it('throws TypeError exception if passed less than 2 arguments', function() { diff --git a/src/formatDistanceStrict/index.js b/src/formatDistanceStrict/index.js index f4c42e824e..a4ed44d46d 100644 --- a/src/formatDistanceStrict/index.js +++ b/src/formatDistanceStrict/index.js @@ -178,7 +178,7 @@ export default function formatDistanceStrict( var comparison = compareAsc(dirtyDate, dirtyBaseDate) if (isNaN(comparison)) { - return 'Invalid Date' + throw new RangeError('Invalid time value') } var localizeOptions = cloneObject(options) diff --git a/src/formatDistanceStrict/test.js b/src/formatDistanceStrict/test.js index ca7f7a1c16..b735913642 100644 --- a/src/formatDistanceStrict/test.js +++ b/src/formatDistanceStrict/test.js @@ -442,25 +442,33 @@ describe('formatDistanceStrict', function() { }) }) - it("returns String('Invalid Date') if the first date is `Invalid Date`", function() { - var result = formatDistanceStrict( - new Date(NaN), - new Date(1986, 3, 7, 10, 32, 0) + it('throws `RangeError` if the first date is `Invalid Date`', function() { + assert.throws( + formatDistanceStrict.bind( + null, + new Date(NaN), + new Date(1986, 3, 7, 10, 32, 0) + ), + RangeError ) - assert(result === 'Invalid Date') }) - it("returns String('Invalid Date') if the second date is `Invalid Date`", function() { - var result = formatDistanceStrict( - new Date(1986, 3, 4, 10, 32, 0), - new Date(NaN) + it('throws `RangeError` if the second date is `Invalid Date`', function() { + assert.throws( + formatDistanceStrict.bind( + null, + new Date(1986, 3, 4, 10, 32, 0), + new Date(NaN) + ), + RangeError ) - assert(result === 'Invalid Date') }) - it("returns String('Invalid Date') if the both dates are `Invalid Date`", function() { - var result = formatDistanceStrict(new Date(NaN), new Date(NaN)) - assert(result === 'Invalid Date') + it('throws `RangeError` if the both dates are `Invalid Date`', function() { + assert.throws( + formatDistanceStrict.bind(null, new Date(NaN), new Date(NaN)), + RangeError + ) }) it("throws `RangeError` if `options.roundingMethod` is not 'floor', 'ceil', 'round' or undefined", function() { diff --git a/src/formatRelative/index.js b/src/formatRelative/index.js index 1e0c935c51..8ebc0aa9b0 100644 --- a/src/formatRelative/index.js +++ b/src/formatRelative/index.js @@ -66,7 +66,7 @@ export default function formatRelative(dirtyDate, dirtyBaseDate, dirtyOptions) { var diff = differenceInCalendarDays(date, baseDate) if (isNaN(diff)) { - return 'Invalid Date' + throw new RangeError('Invalid time value') } var token diff --git a/src/formatRelative/test.js b/src/formatRelative/test.js index 1cde4fa480..92ec82bf13 100644 --- a/src/formatRelative/test.js +++ b/src/formatRelative/test.js @@ -60,19 +60,29 @@ describe('formatRelative', function() { }) describe('edge cases', function() { - it("returns String('Invalid Date') if the date isn't valid", function() { - assert(formatRelative(new Date(NaN), baseDate) === 'Invalid Date') + it("throws RangeError if the date isn't valid", function() { + assert.throws( + formatRelative.bind(null, new Date(NaN), baseDate), + RangeError + ) }) - it("returns String('Invalid Date') if the base date isn't valid", function() { - assert( - formatRelative(new Date(2017, 0 /* Jan */, 1), new Date(NaN)) === - 'Invalid Date' + it("throws RangeError if the base date isn't valid", function() { + assert.throws( + formatRelative.bind( + null, + new Date(2017, 0 /* Jan */, 1), + new Date(NaN) + ), + RangeError ) }) - it("returns String('Invalid Date') if both dates aren't valid", function() { - assert(formatRelative(new Date(NaN), new Date(NaN)) === 'Invalid Date') + it("throws RangeError if both dates aren't valid", function() { + assert.throws( + formatRelative.bind(null, new Date(NaN), new Date(NaN)), + RangeError + ) }) it('handles dates before 100 AD', function() { diff --git a/src/lightFormat/index.js b/src/lightFormat/index.js index d2e35d6208..7d8557d780 100644 --- a/src/lightFormat/index.js +++ b/src/lightFormat/index.js @@ -92,7 +92,7 @@ export default function lightFormat(dirtyDate, dirtyFormatStr) { var originalDate = toDate(dirtyDate) if (!isValid(originalDate)) { - return 'Invalid Date' + throw new RangeError('Invalid time value') } // Convert the date in system timezone to the same date in UTC+00:00 timezone. diff --git a/src/lightFormat/test.js b/src/lightFormat/test.js index 30cc5ee447..6ddfb35928 100644 --- a/src/lightFormat/test.js +++ b/src/lightFormat/test.js @@ -109,8 +109,11 @@ describe('lightFormat', () => { }) }) - it("returns String('Invalid Date') if the date isn't valid", () => { - assert(lightFormat(new Date(NaN), 'MMMM d, yyyy') === 'Invalid Date') + it("throws RangeError if the date isn't valid", () => { + assert.throws( + lightFormat.bind(null, new Date(NaN), 'MMMM d, yyyy'), + RangeError + ) }) it('implicitly converts `formatString`', () => {