From: Tim Wood Date: Thu, 22 Dec 2011 17:38:59 +0000 (-0800) Subject: Adding `moment.fn.day` as a setter. X-Git-Tag: 1.3.0~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a29ed6794723d4eac7da9ac005611d17ac3a627;p=thirdparty%2Fmoment.git Adding `moment.fn.day` as a setter. See #101 --- diff --git a/moment.js b/moment.js index 3b426944c..d56a169c1 100644 --- a/moment.js +++ b/moment.js @@ -561,6 +561,12 @@ 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 }); } }; @@ -584,11 +590,6 @@ // 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(); diff --git a/sitesrc/js/unit-tests.js b/sitesrc/js/unit-tests.js index 9a2fa9c71..8f76abacb 100755 --- a/sitesrc/js/unit-tests.js +++ b/sitesrc/js/unit-tests.js @@ -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");