Skip to content

Commit

Permalink
Merge pull request #136 from AmpersandJS/fix-null-dates
Browse files Browse the repository at this point in the history
Don’t coerce a date type set with `null` to be 1970.
  • Loading branch information
bear committed Feb 11, 2015
2 parents c159906 + 19bd0d0 commit 8264fff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,9 @@ var dataTypes = {
date: {
set: function (newVal) {
var newType;
if (!_.isDate(newVal)) {
if (newVal == null) {
newType = typeof null;
} else if (!_.isDate(newVal)) {
try {
var dateVal = new Date(newVal).valueOf();
if (isNaN(dateVal)) {
Expand All @@ -604,12 +606,14 @@ var dataTypes = {
newType = 'date';
newVal = newVal.valueOf();
}

return {
val: newVal,
type: newType
};
},
get: function (val) {
if (val == null) { return val; }
return new Date(val);
},
default: function () {
Expand Down
32 changes: 32 additions & 0 deletions test/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,38 @@ test("#99 #101 - string dates can be parsed", function(t) {
t.end();
});

test('#128 don\'t coerce null date as 0', function (t) {
var Day = State.extend({
props: {
theDate: 'date'
}
});

var day = new Day({ theDate: null });
t.notOk(day.theDate, 'date should not be set if null');
t.equal(day.theDate, null);

day = new Day({ theDate: undefined });
t.notOk(day.theDate, 'date should not be set if undefined');
t.equal(day.theDate, undefined);

var Day2 = State.extend({
props: {
theDate: {
type: 'date',
required: true,
allowNull: false
}
}
});

t.throws(function () {
new Day2({ theDate: null });
}, /cannot be null/, 'if allowNull:false, and required:true should still throw');

t.end();
});

test('#68, #110 mixin props should not be deleted', function (t) {
var SelectedMixin = {
session : {
Expand Down

0 comments on commit 8264fff

Please sign in to comment.