Skip to content

Commit

Permalink
Remove exception in format (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
leshakoss committed Mar 28, 2017
1 parent 525dbae commit 4d9ef37
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 23 deletions.
18 changes: 0 additions & 18 deletions CHANGELOG.md
Expand Up @@ -262,24 +262,6 @@ This change log follows the format documented in [Keep a CHANGELOG].
//=> 1
```

- **BREAKING**: now `format` throws an exception if the passed date is `Invalid Date`.

```javascript
// Before v2.0.0
var result = format(date, 'YYYY-MM-DD')
if (result === 'Invalid Date') {
// ...
}

// v2.0.0 onward
try {
var result = format(date, 'YYYY-MM-DD')
// ...
} catch (e) {
// ...
}
```

- **BREAKING**: `format` now assumes that the provided locale uses UTC-versions of `Date` methods,
e.g. `getUTCMonth` instead of `getMonth`. All included locales are converted.
This change affects only those who uses custom locales.
Expand Down
3 changes: 1 addition & 2 deletions src/format/index.js
Expand Up @@ -72,7 +72,6 @@ import getUTCISOYear from './_lib/getUTCISOYear/index.js'
* @param {Options} [options] - the object with options. See [Options]{@link docs/Options}
* @param {Locale} [options.locale=enLocale] - the locale object. See [Locale]{@link docs/Locale}
* @returns {String} the formatted date string
* @throws {Error} The date must be valid
*
* @example
* // Represent 11 February 2014 in middle-endian format:
Expand Down Expand Up @@ -110,7 +109,7 @@ export default function format (dirtyDate, dirtyFormatStr, dirtyOptions) {
var originalDate = toDate(dirtyDate, options)

if (!isValid(originalDate, options)) {
throw new Error('Date is invalid')
return 'Invalid Date'
}

// Convert the date in system timezone to the same date in UTC+00:00 timezone.
Expand Down
5 changes: 2 additions & 3 deletions src/format/test.js
Expand Up @@ -315,9 +315,8 @@ describe('format', function () {
})

describe('edge cases', function () {
it('throws an exception if the date isn\'t valid', function () {
var block = format.bind(null, new Date(NaN), 'MMMM D, YYYY')
assert.throws(block)
it('returns String(\'Invalid Date\') if the date isn\'t valid', function () {
assert(format(new Date(NaN), 'MMMM D, YYYY') === 'Invalid Date')
})

it('handles dates before 100 AD', function () {
Expand Down

0 comments on commit 4d9ef37

Please sign in to comment.