From: Tim Wood Date: Thu, 10 May 2012 15:39:36 +0000 (-0700) Subject: First pass at isValid X-Git-Tag: 1.7.0~28^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=776103bdb266d5abbbfb5900f8ffb08b86f163b2;p=thirdparty%2Fmoment.git First pass at isValid --- diff --git a/moment.js b/moment.js index f71f206f0..09a243dc1 100644 --- a/moment.js +++ b/moment.js @@ -69,6 +69,8 @@ function Moment(date, isUTC) { this._d = date; this._isUTC = !!isUTC; + this._a = date.__origin || 0; + date.__origin = null; } function absRound(number) { @@ -171,8 +173,12 @@ // the array should mirror the parameters below // note: all values past the year are optional and will default to the lowest possible value. // [year, month, day , hour, minute, second, millisecond] - function dateFromArray(input) { - return new Date(input[0], input[1] || 0, input[2] || 1, input[3] || 0, input[4] || 0, input[5] || 0, input[6] || 0); + function dateFromArray(input, asUTC) { + input[2] = input[2] || 1; + var date = asUTC ? new Date(Date.UTC.apply({}, input)) : + new Date(input[0], input[1] || 0, input[2], input[3] || 0, input[4] || 0, input[5] || 0, input[6] || 0); + date.__origin = input; + return date; } // format date using native date object @@ -724,6 +730,30 @@ return this._d; }, + toArray : function () { + var m = this; + return [ + m.year(), + m.month(), + m.date(), + m.hours(), + m.minutes(), + m.seconds(), + m.milliseconds() + ]; + }, + + isValid : function () { + var i = 0, + toArray = this.toArray(); + for (; this._a && i < 7; i++) { + if ((this._a[i] || 0) !== toArray[i]) { + return false; + } + } + return !isNaN(this._d.getTime()); + }, + utc : function () { this._isUTC = true; return this; diff --git a/test/moment/is_valid.js b/test/moment/is_valid.js new file mode 100644 index 000000000..e9275b33b --- /dev/null +++ b/test/moment/is_valid.js @@ -0,0 +1,48 @@ +var moment = require("../../moment"); + +exports.is_valid = { + "array bad month" : function(test) { + test.expect(2); + + test.equal(moment([2010, -1]).isValid(), false, 'month -1'); + test.equal(moment([2100, 12]).isValid(), false, 'month 12'); + + test.done(); + }, + + "array good month" : function(test) { + test.expect(12); + + for (var i = 0; i < 12; i++) { + test.equal(moment([2010, i]).isValid(), true, 'month ' + i); + } + + test.done(); + }, + + "array bad date" : function(test) { + test.expect(2); + + test.equal(moment([2010, 0, 0]).isValid(), false, 'date 0'); + test.equal(moment([2100, 0, 32]).isValid(), false, 'date 32'); + + test.done(); + }, + + "array bad date leap year" : function(test) { + test.expect(4); + + test.equal(moment([2010, 1, 29]).isValid(), false, '2010 feb 29'); + test.equal(moment([2100, 1, 29]).isValid(), false, '2100 feb 29'); + test.equal(moment([2008, 1, 30]).isValid(), false, '2008 feb 30'); + test.equal(moment([2000, 1, 30]).isValid(), false, '2000 feb 30'); + test.done(); + }, + + "string nonsensical" : function(test) { + test.expect(1); + + test.equal(moment('fail').isValid(), false, 'string "fail"'); + test.done(); + } +};