From: Iskren Chernev Date: Tue, 12 Apr 2016 11:35:05 +0000 (-0700) Subject: Add localeSorted argument to weekday listers X-Git-Tag: 2.13.0~29^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08b2661a9e8384ff0eeabbf4c4f339164e0bcc92;p=thirdparty%2Fmoment.git Add localeSorted argument to weekday listers Fixes #2927, redo #3103 --- diff --git a/src/lib/locale/lists.js b/src/lib/locale/lists.js index ea0161ab9..4fced6ec5 100644 --- a/src/lib/locale/lists.js +++ b/src/lib/locale/lists.js @@ -7,7 +7,7 @@ function get (format, index, field, setter) { return locale[field](utc, format); } -function list (format, index, field, count, setter) { +function listMonthsImpl (format, index, field) { if (typeof format === 'number') { index = format; format = undefined; @@ -16,33 +16,77 @@ function list (format, index, field, count, setter) { format = format || ''; if (index != null) { - return get(format, index, field, setter); + return get(format, index, field, 'month'); } var i; var out = []; - for (i = 0; i < count; i++) { - out[i] = get(format, i, field, setter); + for (i = 0; i < 12; i++) { + out[i] = get(format, i, field, 'month'); + } + return out; +} + +// () +// (5) +// (fmt, 5) +// (fmt) +// (true) +// (true, 5) +// (true, fmt, 5) +// (true, fmt) +function listWeekdaysImpl (localeSorted, format, index, field) { + if (typeof localeSorted === 'boolean') { + if (typeof format === 'number') { + index = format; + format = undefined; + } + + format = format || ''; + } else { + format = localeSorted; + index = format; + localeSorted = false; + + if (typeof format === 'number') { + index = format; + format = undefined; + } + + format = format || ''; + } + + var locale = getLocale(), + shift = localeSorted ? locale._week.dow : 0; + + if (index != null) { + return get(format, (index + shift) % 7, field, 'day'); + } + + var i; + var out = []; + for (i = 0; i < 7; i++) { + out[i] = get(format, (i + shift) % 7, field, 'day'); } return out; } export function listMonths (format, index) { - return list(format, index, 'months', 12, 'month'); + return listMonthsImpl(format, index, 'months'); } export function listMonthsShort (format, index) { - return list(format, index, 'monthsShort', 12, 'month'); + return listMonthsImpl(format, index, 'monthsShort'); } -export function listWeekdays (format, index) { - return list(format, index, 'weekdays', 7, 'day'); +export function listWeekdays (localeSorted, format, index) { + return listWeekdaysImpl(localeSorted, format, index, 'weekdays'); } -export function listWeekdaysShort (format, index) { - return list(format, index, 'weekdaysShort', 7, 'day'); +export function listWeekdaysShort (localeSorted, format, index) { + return listWeekdaysImpl(localeSorted, format, index, 'weekdaysShort'); } -export function listWeekdaysMin (format, index) { - return list(format, index, 'weekdaysMin', 7, 'day'); +export function listWeekdaysMin (localeSorted, format, index) { + return listWeekdaysImpl(localeSorted, format, index, 'weekdaysMin'); } diff --git a/src/test/moment/listers.js b/src/test/moment/listers.js index 242ae3402..508832668 100644 --- a/src/test/moment/listers.js +++ b/src/test/moment/listers.js @@ -29,14 +29,22 @@ test('localized', function (assert) { monthsShort = 'on_tw_th_fo_fi_si_se_ei_ni_te_el_tw'.split('_'), weekdays = 'one_two_three_four_five_six_seven'.split('_'), weekdaysShort = 'on_tw_th_fo_fi_si_se'.split('_'), - weekdaysMin = '1_2_3_4_5_6_7'.split('_'); + weekdaysMin = '1_2_3_4_5_6_7'.split('_'), + weekdaysLocale = 'four_five_six_seven_one_two_three'.split('_'), + weekdaysShortLocale = 'fo_fi_si_se_on_tw_th'.split('_'), + weekdaysMinLocale = '4_5_6_7_1_2_3'.split('_'), + week = { + dow : 3, + doy : 6 + }; moment.locale('numerologists', { months : months, monthsShort : monthsShort, weekdays : weekdays, weekdaysShort: weekdaysShort, - weekdaysMin: weekdaysMin + weekdaysMin: weekdaysMin, + week : week }); assert.deepEqual(moment.months(), months); @@ -56,6 +64,18 @@ test('localized', function (assert) { assert.equal(moment.weekdays(2), 'three'); assert.equal(moment.weekdaysShort(2), 'th'); assert.equal(moment.weekdaysMin(2), '3'); + + assert.deepEqual(moment.weekdays(true), weekdaysLocale); + assert.deepEqual(moment.weekdaysShort(true), weekdaysShortLocale); + assert.deepEqual(moment.weekdaysMin(true), weekdaysMinLocale); + + assert.equal(moment.weekdays(true, 0), 'four'); + assert.equal(moment.weekdaysShort(true, 0), 'fo'); + assert.equal(moment.weekdaysMin(true, 0), '4'); + + assert.equal(moment.weekdays(false, 2), 'three'); + assert.equal(moment.weekdaysShort(false, 2), 'th'); + assert.equal(moment.weekdaysMin(false, 2), '3'); }); test('with functions', function (assert) {