Skip to content

Commit

Permalink
[feature] Add localeSort param to Locale weekday methods (#4553)
Browse files Browse the repository at this point in the history
* Add localeSort param to Locale weekday methods

* Add concrete test case
  • Loading branch information
marwahaha committed Jan 21, 2019
1 parent bb484bc commit 3b80f6a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/lib/units/day-of-week.js
Expand Up @@ -96,25 +96,28 @@ function parseIsoWeekday(input, locale) {
}

// LOCALES
function shiftWeekdays (ws, n) {
return ws.slice(n, 7).concat(ws.slice(0, n));
}

export var defaultLocaleWeekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_');
export function localeWeekdays (m, format) {
if (!m) {
return isArray(this._weekdays) ? this._weekdays :
this._weekdays['standalone'];
}
return isArray(this._weekdays) ? this._weekdays[m.day()] :
this._weekdays[this._weekdays.isFormat.test(format) ? 'format' : 'standalone'][m.day()];
var weekdays = isArray(this._weekdays) ? this._weekdays :
this._weekdays[(m && m !== true && this._weekdays.isFormat.test(format)) ? 'format' : 'standalone'];
return (m === true) ? shiftWeekdays(weekdays, this._week.dow)
: (m) ? weekdays[m.day()] : weekdays;
}

export var defaultLocaleWeekdaysShort = 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_');
export function localeWeekdaysShort (m) {
return (m) ? this._weekdaysShort[m.day()] : this._weekdaysShort;
return (m === true) ? shiftWeekdays(this._weekdaysShort, this._week.dow)
: (m) ? this._weekdaysShort[m.day()] : this._weekdaysShort;
}

export var defaultLocaleWeekdaysMin = 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_');
export function localeWeekdaysMin (m) {
return (m) ? this._weekdaysMin[m.day()] : this._weekdaysMin;
return (m === true) ? shiftWeekdays(this._weekdaysMin, this._week.dow)
: (m) ? this._weekdaysMin[m.day()] : this._weekdaysMin;
}

function handleStrictParse(weekdayName, format, strict) {
Expand Down
1 change: 0 additions & 1 deletion src/locale/en-au.js
Expand Up @@ -56,4 +56,3 @@ export default moment.defineLocale('en-au', {
doy : 4 // The week that contains Jan 4th is the first week of the year.
}
});

4 changes: 3 additions & 1 deletion src/locale/uk.js
Expand Up @@ -35,6 +35,9 @@ function weekdaysCaseReplace(m, format) {
'genitive': 'неділі_понеділка_вівторка_середи_четверга_п’ятниці_суботи'.split('_')
};

if (m === true) {
return weekdays['nominative'].slice(1, 7).concat(weekdays['nominative'].slice(0, 1));
}
if (!m) {
return weekdays['nominative'];
}
Expand Down Expand Up @@ -141,4 +144,3 @@ export default moment.defineLocale('uk', {
doy : 7 // The week that contains Jan 7th is the first week of the year.
}
});

20 changes: 19 additions & 1 deletion src/test/helpers/common-locale.js
Expand Up @@ -154,5 +154,23 @@ export function defineCommonLocaleTests(locale, options) {
assert.equal(moment().localeData().weekdaysShort().length, 7, 'weekdaysShort should return 7 days');
assert.equal(moment().localeData().weekdaysMin().length, 7, 'monthsShort should return 7 days');
});
}

test('localeData weekdays can localeSort', function (assert) {
var weekdays = moment().localeData().weekdays();
var weekdaysShort = moment().localeData().weekdaysShort();
var weekdaysMin = moment().localeData().weekdaysMin();
var shift = moment().localeData()._week.dow;
assert.deepEqual(
moment().localeData().weekdays(true),
weekdays.slice(shift, 7).concat(weekdays.slice(0, shift)),
'weekdays should localeSort');
assert.deepEqual(
moment().localeData().weekdaysShort(true),
weekdaysShort.slice(shift, 7).concat(weekdaysShort.slice(0, shift)),
'weekdaysShort should localeSort');
assert.deepEqual(
moment().localeData().weekdaysMin(true),
weekdaysMin.slice(shift, 7).concat(weekdaysMin.slice(0, shift)),
'weekdaysMin should localeSort');
});
}
28 changes: 28 additions & 0 deletions src/test/locale/en-au.js
Expand Up @@ -208,3 +208,31 @@ test('weeks year starting sunday formatted', function (assert) {
assert.equal(moment([2012, 0, 9]).format('w ww wo'), '2 02 2nd', 'Jan 9 2012 should be week 2');
assert.equal(moment([2012, 0, 15]).format('w ww wo'), '2 02 2nd', 'Jan 15 2012 should be week 2');
});

// Concrete test for Locale#weekdaysMin
test('Weekdays sort by locale', function (assert) {
assert.deepEqual(
moment().localeData('en-au').weekdays(),
'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
'weekdays start on Sunday');
assert.deepEqual(
moment().localeData('en-au').weekdays(true),
'Monday_Tuesday_Wednesday_Thursday_Friday_Saturday_Sunday'.split('_'),
'locale-sorted weekdays start on Monday');
assert.deepEqual(
moment().localeData('en-au').weekdaysShort(),
'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
'weekdaysShort start on Sunday');
assert.deepEqual(
moment().localeData('en-au').weekdaysShort(true),
'Mon_Tue_Wed_Thu_Fri_Sat_Sun'.split('_'),
'locale-sorted weekdaysShort start on Monday');
assert.deepEqual(
moment().localeData('en-au').weekdaysMin(),
'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'),
'weekdaysMin start on Sunday');
assert.deepEqual(
moment().localeData('en-au').weekdaysMin(true),
'Mo_Tu_We_Th_Fr_Sa_Su'.split('_'),
'locale-sorted weekdaysMin start on Monday');
});
1 change: 0 additions & 1 deletion src/test/locale/en-gb.js
Expand Up @@ -208,4 +208,3 @@ test('weeks year starting sunday formatted', function (assert) {
assert.equal(moment([2012, 0, 9]).format('w ww wo'), '2 02 2nd', 'Jan 9 2012 should be week 2');
assert.equal(moment([2012, 0, 15]).format('w ww wo'), '2 02 2nd', 'Jan 15 2012 should be week 2');
});

0 comments on commit 3b80f6a

Please sign in to comment.