9da6723af8ed06785b4d69c85d950dd75558ffcf616d3c36491eaa142733e7508f451534a16a2ee1929ebd53c96715c318dcab48b0ea5b18e95f252f71bab0 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import isNumber from '../utils/is-number';
  2. import { getLocale } from './locales';
  3. import { createUTC } from '../create/utc';
  4. function get(format, index, field, setter) {
  5. var locale = getLocale(),
  6. utc = createUTC().set(setter, index);
  7. return locale[field](utc, format);
  8. }
  9. function listMonthsImpl(format, index, field) {
  10. if (isNumber(format)) {
  11. index = format;
  12. format = undefined;
  13. }
  14. format = format || '';
  15. if (index != null) {
  16. return get(format, index, field, 'month');
  17. }
  18. var i,
  19. out = [];
  20. for (i = 0; i < 12; i++) {
  21. out[i] = get(format, i, field, 'month');
  22. }
  23. return out;
  24. }
  25. // ()
  26. // (5)
  27. // (fmt, 5)
  28. // (fmt)
  29. // (true)
  30. // (true, 5)
  31. // (true, fmt, 5)
  32. // (true, fmt)
  33. function listWeekdaysImpl(localeSorted, format, index, field) {
  34. if (typeof localeSorted === 'boolean') {
  35. if (isNumber(format)) {
  36. index = format;
  37. format = undefined;
  38. }
  39. format = format || '';
  40. } else {
  41. format = localeSorted;
  42. index = format;
  43. localeSorted = false;
  44. if (isNumber(format)) {
  45. index = format;
  46. format = undefined;
  47. }
  48. format = format || '';
  49. }
  50. var locale = getLocale(),
  51. shift = localeSorted ? locale._week.dow : 0,
  52. i,
  53. out = [];
  54. if (index != null) {
  55. return get(format, (index + shift) % 7, field, 'day');
  56. }
  57. for (i = 0; i < 7; i++) {
  58. out[i] = get(format, (i + shift) % 7, field, 'day');
  59. }
  60. return out;
  61. }
  62. export function listMonths(format, index) {
  63. return listMonthsImpl(format, index, 'months');
  64. }
  65. export function listMonthsShort(format, index) {
  66. return listMonthsImpl(format, index, 'monthsShort');
  67. }
  68. export function listWeekdays(localeSorted, format, index) {
  69. return listWeekdaysImpl(localeSorted, format, index, 'weekdays');
  70. }
  71. export function listWeekdaysShort(localeSorted, format, index) {
  72. return listWeekdaysImpl(localeSorted, format, index, 'weekdaysShort');
  73. }
  74. export function listWeekdaysMin(localeSorted, format, index) {
  75. return listWeekdaysImpl(localeSorted, format, index, 'weekdaysMin');
  76. }