Skip to content

Commit

Permalink
Clarify when format throws a RangeError and fix the test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
marnusw committed Feb 24, 2022
1 parent 925fd85 commit 33f803f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
### v1.3.0 (23 February 2022)

- [ENHANCEMENT] Native ESM support via `exports` configuration in `package.json`
- [ENHANCEMENT] Native ESM support via `exports` configuration in `package.json` (#133, #150)
- [DOCS] Clarify the use of ESM vs CommonJS import paths
- [DOCS] Clarify when `format` throws a `RangeError` and fix the test for it (#152)

### v1.2.2 (21 December 2021)

Expand Down Expand Up @@ -32,7 +33,7 @@

### v1.1.5 (27 July 2021)

- [BUGFIX] Fixing parsing IANA strings (#129)
- [BUGFIX] Fixed parsing IANA strings (#129)

### v1.1.4 (13 April 2021)

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ zone (if provided, see below) rather than the system time zone.
Since a JavaScript `Date` instance cannot convey the time zone information to the `format` function
it is necessary to pass the `timeZone` value as an option on the third argument of `format`.

Similar to `date-fns/format`, when an invalid date or time zone is used a `RangeError` is thrown.
Similar to `date-fns/format`, when an invalid date is used a `RangeError` is thrown. When an invalid
time zone is provided _and included in the output_, i.e. with time zone tokens in the format
string, it will also throw a `RangeError`.

To format a date showing time for a specific time zone other than the system time zone, the
`format` function can be combined with `utcToZonedTime`. This is what `formatInTimeZone` does
Expand Down
9 changes: 4 additions & 5 deletions src/format/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,10 @@ describe('format', function () {
it('throws RangeError if the time zone is invalid and included in the output', () => {
var result = format(new Date(2021, 11, 20), 'MMMM d, yyyy', { timeZone: 'bad/timeZone' })
assert.equal(result, 'December 20, 2021')
try {
format(new Date(), 'MMMM d, yyyy zzz', { timeZone: 'bad/timeZone' })
} catch (error) {
assert.deepEqual(error, new Error('Invalid time zone specified: bad/timeZone'))
}
assert.throws(
() => format(new Date(), 'MMMM d, yyyy zzz', { timeZone: 'bad/timeZone' }),
/RangeError: Invalid time zone specified: bad\/timeZone$/
)
})

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

0 comments on commit 33f803f

Please sign in to comment.