]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
String setters for moment#day and moment#month 690/head
authorquietmint <quietmint@example.local>
Sat, 23 Mar 2013 22:43:42 +0000 (18:43 -0400)
committerquietmint <quietmint@example.local>
Sat, 30 Mar 2013 19:38:49 +0000 (15:38 -0400)
moment.js
test/moment/getters_setters.js
test/moment/weeks.js

index 4bf5e83690bf1e307f8daed6da922844da2c32a9..2fa35468af02298cd724ee95ec97062d2b4bb533 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -57,7 +57,7 @@
         parseTimezoneChunker = /([\+\-]|\d\d)/gi,
 
         // getter and setter names
-        proxyGettersAndSetters = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
+        proxyGettersAndSetters = 'Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
         unitMillisecondFactors = {
             'Milliseconds' : 1,
             'Seconds' : 1e3,
         },
 
         monthsParse : function (monthName) {
-            var i, mom, regex, output;
+            var i, mom, regex;
 
             if (!this._monthsParse) {
                 this._monthsParse = [];
             return this._weekdaysMin[m.day()];
         },
 
+        weekdaysParse : function (weekdayName) {
+            var i, mom, regex;
+
+            if (!this._weekdaysParse) {
+                this._weekdaysParse = [];
+            }
+
+            for (i = 0; i < 7; i++) {
+                // make the regex if we don't have it already
+                if (!this._weekdaysParse[i]) {
+                    mom = moment([2000, 1]).day(i);
+                    regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, '');
+                    this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i');
+                }
+                // test the regex
+                if (this._weekdaysParse[i].test(weekdayName)) {
+                    return i;
+                }
+            }
+        },
+
         _longDateFormat : {
             LT : "h:mm A",
             L : "MM/DD/YYYY",
 
         day : function (input) {
             var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
-            return input == null ? day :
-                this.add({ d : input - day });
+            if (input != null) {
+                if (typeof input === 'string') {
+                    input = this.lang().weekdaysParse(input);
+                    if (typeof input !== 'number') {
+                        return this;
+                    }
+                }
+                return this.add({ d : input - day });
+            } else {
+                return day;
+            }
+        },
+
+        month : function (input) {
+            var utc = this._isUTC ? 'UTC' : '';
+            if (input != null) {
+                if (typeof input === 'string') {
+                    input = this.lang().monthsParse(input);
+                    if (typeof input !== 'number') {
+                        return this;
+                    }
+                }
+                this._d['set' + utc + 'Month'](input);
+                return this;
+            } else {
+                return this._d['get' + utc + 'Month']();
+            }
         },
 
         startOf: function (units) {
 
     // add plural methods
     moment.fn.days = moment.fn.day;
+    moment.fn.months = moment.fn.month;
     moment.fn.weeks = moment.fn.week;
     moment.fn.isoWeeks = moment.fn.isoWeek;
 
index da46ac78e0160177319088d0d21d1db870ad5e7e..80954595262a6e15ecaee544ce8c12efe8fc8ce5 100644 (file)
@@ -81,6 +81,20 @@ exports.getters_setters = {
         test.equal(a.milliseconds(), 9, 'milliseconds');
         test.done();
     },
+    
+    "setters strings" : function(test) {
+        test.expect(7);
+
+        var a = moment().lang('en');
+        test.equal(a.day(0).day('Wednesday').day(), 3, 'day full name');
+        test.equal(a.day(0).day('Wed').day(), 3, 'day short name');
+        test.equal(a.day(0).day('We').day(), 3, 'day minimal name');
+        test.equal(a.day(0).day('invalid').day(), 0, 'invalid day name');
+        test.equal(a.month(0).month('April').month(), 3, 'month full name');
+        test.equal(a.month(0).month('Apr').month(), 3, 'month short name');
+        test.equal(a.month(0).month('invalid').month(), 0, 'invalid month name');
+        test.done();
+    },
 
     "setters - falsey values" : function(test) {
         test.expect(1);
index 2cbc1bb7e1ebbe27aee7c3892e4070e2a4d27f92..1b8b38399ce74c5a533b8cdfb65ac2d235f65103 100644 (file)
@@ -1,6 +1,6 @@
 var moment = require("../../moment");
 
-exports.utc = {
+exports.weeks = {
     setUp : function (cb) {
         moment.lang('en');
         cb();