Skip to content

Commit

Permalink
! Return invalid date/time input values (null & undefined) as-is
Browse files Browse the repository at this point in the history
Instead of raising error

Issue reported in #535
  • Loading branch information
PikachuEXE committed May 8, 2019
1 parent 931c534 commit 869d168
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 15 additions & 5 deletions app/assets/javascripts/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@

// This function interpolates the all variables in the given message.
I18n.interpolate = function(message, options) {
if (message === null) {
if (message == null) {
return message;
}

Expand Down Expand Up @@ -833,8 +833,12 @@
//
I18n.parseDate = function(date) {
var matches, convertedDate, fraction;
// A date input of `null` or `undefined` will be returned as-is
if (date == null) {
return date;
}
// we have a date, so just return it.
if (typeof(date) == "object") {
if (typeof(date) === "object") {
return date;
}

Expand Down Expand Up @@ -980,12 +984,18 @@
, format = this.lookup(scope)
;

if (date.toString().match(/invalid/i)) {
return date.toString();
// A date input of `null` or `undefined` will be returned as-is
if (date == null) {
return date;
}

var date_string = date.toString()
if (date_string.match(/invalid/i)) {
return date_string;
}

if (!format) {
return date.toString();
return date_string;
}

return this.strftime(date, format);
Expand Down
8 changes: 7 additions & 1 deletion spec/js/localization.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe("Localization", function(){
expect(I18n.l("time.formats.long", "2009-11-29 15:07:59")).toEqual("Domingo, 29 de Novembro de 2009, 15:07 h");
});

it("return 'Invalid Date' or original value for invalid input", function(){
expect(I18n.l("time.formats.default", "")).toEqual("Invalid Date");
expect(I18n.l("time.formats.default", null)).toEqual(null);
expect(I18n.l("time.formats.default", undefined)).toEqual(undefined);
});

it("localizes date/time strings with placeholders", function(){
I18n.locale = "pt-BR";

Expand All @@ -45,4 +51,4 @@ describe("Localization", function(){
I18n.locale = "pt-BR";
expect(I18n.l("percentage", 123.45)).toEqual("123,45%");
});
});
});

0 comments on commit 869d168

Please # to comment.