Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix #2704 - isoWeekday(String) inconsistent with isoWeekday(Number) #3177

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/lib/units/day-of-week.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ function parseWeekday(input, locale) {
return null;
}

function parseIsoWeekday(input, locale) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think falling back to parseWeekday is a bit excessive. If its string go through localeParse. If we now have number do the modulus. Return null if any of those fail.

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('_');
Expand Down Expand Up @@ -231,10 +244,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;
Expand Down
19 changes: 19 additions & 0 deletions src/test/moment/weekday.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down