Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error when I18n.strftime() takes an invalid date #383

Merged
merged 1 commit into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Nothing
- [JS] Throw an error when `I18n.strftime()` takes an invalid date ([#383](https://github.com/fnando/i18n-js/pull/383))



## [3.0.0.rc12] - 2015-12-30
Expand Down
4 changes: 4 additions & 0 deletions app/assets/javascripts/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,10 @@

options = this.prepareOptions(options, DATE);

if (isNaN(date.getTime())) {
throw new Error('I18n.strftime() requires a valid date object, but received an invalid date.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Error work in every JS environment?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Error constructor is one of the global objects that all the JS environments (ancient browsers, Node.js, Electron etc.) have.

}

var weekDay = date.getDay()
, day = date.getDate()
, year = date.getFullYear()
Expand Down
7 changes: 7 additions & 0 deletions spec/js/dates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,11 @@ describe("Dates", function(){
date = new Date(2009, 3, 26, 7, 35, 44);
expect(I18n.strftime(date, "%p")).toEqual("de:AM");
});

it("fails to format invalid date", function(){
var date = new Date('foo');
expect(function() {
I18n.strftime(date, "%a");
}).toThrow('I18n.strftime() requires a valid date object, but received an invalid date.');
});
});