]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
startOf and endOf are now mutable. #335 339/head
authorMatt Garrett <mattga@gmail.com>
Thu, 14 Jun 2012 14:02:49 +0000 (09:02 -0500)
committerMatt Garrett <mattga@gmail.com>
Thu, 14 Jun 2012 14:11:53 +0000 (09:11 -0500)
Adds new tests to assert the policy
that manipulation methods are mutable.

moment.js
test/moment/mutable.js [new file with mode: 0644]

index b8471be488173efc19ab7d462c1fa8cedb272981..b12232cd2fad41f21e0eb377218da1456a772962 100644 (file)
--- a/moment.js
+++ b/moment.js
         },
 
         startOf: function (val) {
-            var output = this.clone();
             // the following switch intentionally omits break keywords
             // to utilize falling through the cases.
             switch (val.replace(/s$/, '')) {
             case 'year':
-                output.month(0);
+                this.month(0);
                 /* falls through */
             case 'month':
-                output.date(1);
+                this.date(1);
                 /* falls through */
             case 'day':
-                output.hours(0);
+                this.hours(0);
                 /* falls through */
             case 'hour':
-                output.minutes(0);
+                this.minutes(0);
                 /* falls through */
             case 'minute':
-                output.seconds(0);
+                this.seconds(0);
                 /* falls through */
             case 'second':
-                output.milliseconds(0);
+                this.milliseconds(0);
                 /* falls through */
             }
-            return output;
+            return this;
         },
 
         endOf: function (val) {
         },
         
         sod: function () {
-            return this.startOf('day');
+            return this.clone().startOf('day');
         },
 
         eod: function () {
             // end of day = start of day plus 1 day, minus 1 millisecond
-            return this.endOf('day');
+            return this.clone().endOf('day');
         },
 
         zone : function () {
diff --git a/test/moment/mutable.js b/test/moment/mutable.js
new file mode 100644 (file)
index 0000000..e3c2c02
--- /dev/null
@@ -0,0 +1,51 @@
+var moment = require("../../moment");
+
+exports.mutable = {
+    "manipulation methods" : function(test) {
+        
+        var mutableMethods = {
+          'year':          function (m){ return m.year(2011); },
+          'month':         function (m){ return m.month(1); },
+          'date':          function (m){ return m.date(9); },
+          'hours':         function (m){ return m.hours(7); },
+          'minutes':       function (m){ return m.minutes(33); },
+          'seconds':       function (m){ return m.seconds(44); },
+          'milliseconds':  function (m){ return m.milliseconds(55); },
+          'day':           function (m){ return m.day(2); },
+          'startOf':       function (m){ return m.startOf('week') },
+          'endOf':         function (m){ return m.endOf('week') },
+          'add':           function (m){ return m.add('days', 1) },
+          'subtract':      function (m){ return m.subtract('years', 2) },
+          'local':         function (m){ return m.local() },
+          'utc':           function (m){ return m.utc() }
+        };
+
+        test.expect(14);
+
+        for (method in mutableMethods){
+          var d = new Date();
+          var d2 = mutableMethods[method](moment(d)).toDate();
+          test.equal(d, d2, method + "() should be mutable");
+        }
+
+        test.done();
+    },
+    "non mutable methods" : function(test) {
+        
+        var nonMutableMethods = {
+          'sod':       function (m){ return m.sod() },
+          'eod':       function (m){ return m.eod() }
+        };
+
+        test.expect(2);
+
+        for (method in nonMutableMethods){
+          var d = new Date();
+          var d2 = nonMutableMethods[method](moment(d)).toDate();
+          test.notEqual(d, d2, method + "() should not be mutable");
+        }
+
+        test.done();
+    }
+
+};