]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Adding `moment.fn.day` as a setter.
authorTim Wood <washwithcare@gmail.com>
Thu, 22 Dec 2011 17:38:59 +0000 (09:38 -0800)
committerTim Wood <washwithcare@gmail.com>
Thu, 22 Dec 2011 17:55:13 +0000 (09:55 -0800)
See #101

moment.js
sitesrc/js/unit-tests.js

index 3b426944cecc2dc06eeadd8124ca8e33152b48eb..d56a169c16bc60c471d0085578e62b9ac0b604ec 100644 (file)
--- a/moment.js
+++ b/moment.js
 
         isDST : function () {
             return this.zone() !== moment([this.year()]).zone();
+        },
+
+        day : function (input) {
+            var day = this._d.getDay();
+            return input == null ? day :
+                this.add({ d : input - day });
         }
     };
 
     // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear')
     makeShortcut('year', 'FullYear');
 
-    // add shortcut for day (no setter)
-    moment.fn.day = function () {
-        return this._d.getDay();
-    };
-
     // add shortcut for timezone offset (no setter)
     moment.fn.zone = function () {
         return this._d.getTimezoneOffset();
index 9a2fa9c7190d77c4dd7e280c3dfafff108aa8947..8f76abacb56c53db4a2551575a541c6a03c12a26 100755 (executable)
@@ -357,6 +357,35 @@ test("chaining setters", 7, function() {
     equal(a.seconds(), 8, 'second');
 });
 
+test("day setter", 18, function() {
+    var a = moment([2011, 0, 15]);
+    equal(moment(a).day(0).date(), 9, 'set from saturday to sunday');
+    equal(moment(a).day(6).date(), 15, 'set from saturday to saturday');
+    equal(moment(a).day(3).date(), 12, 'set from saturday to wednesday');
+
+    a = moment([2011, 0, 9]);
+    equal(moment(a).day(0).date(), 9, 'set from sunday to sunday');
+    equal(moment(a).day(6).date(), 15, 'set from sunday to saturday');
+    equal(moment(a).day(3).date(), 12, 'set from sunday to wednesday');
+
+    a = moment([2011, 0, 12]);
+    equal(moment(a).day(0).date(), 9, 'set from wednesday to sunday');
+    equal(moment(a).day(6).date(), 15, 'set from wednesday to saturday');
+    equal(moment(a).day(3).date(), 12, 'set from wednesday to wednesday');
+
+    equal(moment(a).day(-7).date(), 2, 'set from wednesday to last sunday');
+    equal(moment(a).day(-1).date(), 8, 'set from wednesday to last saturday');
+    equal(moment(a).day(-4).date(), 5, 'set from wednesday to last wednesday');
+
+    equal(moment(a).day(7).date(), 16, 'set from wednesday to next sunday');
+    equal(moment(a).day(13).date(), 22, 'set from wednesday to next saturday');
+    equal(moment(a).day(10).date(), 19, 'set from wednesday to next wednesday');
+
+    equal(moment(a).day(14).date(), 23, 'set from wednesday to second next sunday');
+    equal(moment(a).day(20).date(), 29, 'set from wednesday to second next saturday');
+    equal(moment(a).day(17).date(), 26, 'set from wednesday to second next wednesday');
+});
+
 
 module("format");