Skip to content

Commit

Permalink
Merge pull request #55 from centro/fix-date-attr-serialization
Browse files Browse the repository at this point in the history
[attrs] Don't rely on toJSON/string-parsing on DateAttr serialization
  • Loading branch information
peterwmwong committed Sep 26, 2018
2 parents c08d003 + f7dac6d commit 8b189e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 9 additions & 1 deletion dist/attrs.js
Expand Up @@ -96,6 +96,10 @@ var BooleanAttr = exports.BooleanAttr = {
}
};

function format2Digits(num) {
return num < 10 ? '0' + num : num;
}

var DateAttr = exports.DateAttr = {
coerce: function coerce(v) {
if (v == null || v instanceof Date) {
Expand All @@ -112,7 +116,11 @@ var DateAttr = exports.DateAttr = {
return parsers.parseDate(v);
},
serialize: function serialize(date) {
return date instanceof Date ? date.toJSON().replace(/T.*$/, '') : date;
if (date instanceof Date) {
return date.getFullYear() + '-' + format2Digits(date.getMonth() + 1) + '-' + format2Digits(date.getDate());
} else {
return date;
}
}
};

Expand Down
12 changes: 11 additions & 1 deletion src/attrs.js
Expand Up @@ -47,6 +47,10 @@ export var BooleanAttr = {
serialize(b) { return b; }
};

function format2Digits(num) {
return num < 10 ? `0${num}` : num;
}

export var DateAttr = {
coerce(v) {
if (v == null || v instanceof Date) { return v; }
Expand All @@ -60,7 +64,13 @@ export var DateAttr = {
},

serialize(date) {
return date instanceof Date ? date.toJSON().replace(/T.*$/, '') : date;
if (date instanceof Date) {
return date.getFullYear() + '-'
+ format2Digits(date.getMonth() + 1) + '-'
+ format2Digits(date.getDate());
} else {
return date;
}
}
};

Expand Down

0 comments on commit 8b189e8

Please sign in to comment.