]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fixes #2704 - isoWeekday(String) inconsistent with isoWeekday(Number)
authorNick Vanderhoven <nickvdh@gmail.com>
Wed, 11 May 2016 05:41:16 +0000 (07:41 +0200)
committerIskren Chernev <iskren.chernev@gmail.com>
Tue, 14 Jun 2016 09:25:23 +0000 (02:25 -0700)
src/lib/units/day-of-week.js
src/test/moment/weekday.js

index e8e957866c32d371ec1b53afd2e534d8599ad568..3fb405a44e69cccc1ba732547d679b166eff715f 100644 (file)
@@ -88,6 +88,19 @@ function parseWeekday(input, locale) {
     return null;
 }
 
+function parseIsoWeekday(input, locale) {
+    if (typeof input !== 'string' || !isNaN(input)) {
+        return parseWeekday(input, locale);
+    }
+
+    input = locale.weekdaysParse(input);
+    if (typeof input === 'number') {
+        return input % 7 ? input : input + 7;
+    }
+
+    return null;
+}
+
 // LOCALES
 
 export var defaultLocaleWeekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_');
@@ -237,10 +250,17 @@ export function getSetISODayOfWeek (input) {
     if (!this.isValid()) {
         return input != null ? this : NaN;
     }
+
     // behaves the same as moment#day except
     // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6)
     // as a setter, sunday should belong to the previous week.
-    return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7);
+
+    if (input != null) {
+        var weekday = parseIsoWeekday(input, this.localeData());
+        return this.day(this.day() % 7 ? weekday : weekday - 7);
+    } else {
+        return this.day() || 7;
+    }
 }
 
 export var defaultWeekdaysRegex = matchWord;
index ae796f589ba965934f67c42d38a44b3a4e82a684..db62405998ebd1ab0dfa6be746d5c17131d5c6e4 100644 (file)
@@ -53,6 +53,25 @@ test('iso weekday setter', function (assert) {
     assert.equal(moment(a).isoWeekday(14).date(), 23, 'set from sun to next sun');
 });
 
+test('iso weekday setter with day name', function (assert) {
+    moment.locale('en');
+
+    var a = moment([2011, 0, 10]);
+    assert.equal(moment(a).isoWeekday('Monday').date(),   10, 'set from mon to mon');
+    assert.equal(moment(a).isoWeekday('Thursday').date(), 13, 'set from mon to thu');
+    assert.equal(moment(a).isoWeekday('Sunday').date(),   16, 'set from mon to sun');
+
+    a = moment([2011, 0, 13]);
+    assert.equal(moment(a).isoWeekday('Monday').date(),   10, 'set from thu to mon');
+    assert.equal(moment(a).isoWeekday('Thursday').date(), 13, 'set from thu to thu');
+    assert.equal(moment(a).isoWeekday('Sunday').date(),   16, 'set from thu to sun');
+
+    a = moment([2011, 0, 16]);
+    assert.equal(moment(a).isoWeekday('Monday').date(),   10, 'set from sun to mon');
+    assert.equal(moment(a).isoWeekday('Thursday').date(), 13, 'set from sun to thu');
+    assert.equal(moment(a).isoWeekday('Sunday').date(),   16, 'set from sun to sun');
+});
+
 test('weekday first day of week Sunday (dow 0)', function (assert) {
     moment.locale('dow: 0,doy: 6', {week: {dow: 0, doy: 6}});
     assert.equal(moment([1985, 1,  3]).weekday(), 0, 'Feb  3 1985 is Sunday    -- 0th day');