]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add localeSorted argument to weekday listers
authorIskren Chernev <iskren.chernev@gmail.com>
Tue, 12 Apr 2016 11:35:05 +0000 (04:35 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 16 Apr 2016 05:51:21 +0000 (22:51 -0700)
Fixes #2927, redo #3103

src/lib/locale/lists.js
src/test/moment/listers.js

index ea0161ab9038effd9a277e10073f35b24f1bfbfd..4fced6ec57ce4b4ec3b1f2d2fb5d5833d255892d 100644 (file)
@@ -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');
 }
index 242ae3402102cbfefcca85eecb4385fbd06bbab9..5088326685204bdab9040d8ce521e0da76579a79 100644 (file)
@@ -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) {