]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
First pass at isValid
authorTim Wood <washwithcare@gmail.com>
Thu, 10 May 2012 15:39:36 +0000 (08:39 -0700)
committerTim Wood <washwithcare@gmail.com>
Thu, 10 May 2012 15:39:36 +0000 (08:39 -0700)
moment.js
test/moment/is_valid.js [new file with mode: 0644]

index f71f206f08ddb94f5267968d2c472e55bfc417f8..09a243dc10d941e7280dd5a5397ff8ce0ca75769 100644 (file)
--- 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) {
     // 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
             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 (file)
index 0000000..e9275b3
--- /dev/null
@@ -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();
+    }
+};