index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { t } from '../localizedFormat/utils';
  2. export default (function (o, c, dayjs) {
  3. // locale needed later
  4. var proto = c.prototype;
  5. var getLocalePart = function getLocalePart(part) {
  6. return part && (part.indexOf ? part : part.s);
  7. };
  8. var getShort = function getShort(ins, target, full, num, localeOrder) {
  9. var locale = ins.name ? ins : ins.$locale();
  10. var targetLocale = getLocalePart(locale[target]);
  11. var fullLocale = getLocalePart(locale[full]);
  12. var result = targetLocale || fullLocale.map(function (f) {
  13. return f.slice(0, num);
  14. });
  15. if (!localeOrder) return result;
  16. var weekStart = locale.weekStart;
  17. return result.map(function (_, index) {
  18. return result[(index + (weekStart || 0)) % 7];
  19. });
  20. };
  21. var getDayjsLocaleObject = function getDayjsLocaleObject() {
  22. return dayjs.Ls[dayjs.locale()];
  23. };
  24. var getLongDateFormat = function getLongDateFormat(l, format) {
  25. return l.formats[format] || t(l.formats[format.toUpperCase()]);
  26. };
  27. var localeData = function localeData() {
  28. var _this = this;
  29. return {
  30. months: function months(instance) {
  31. return instance ? instance.format('MMMM') : getShort(_this, 'months');
  32. },
  33. monthsShort: function monthsShort(instance) {
  34. return instance ? instance.format('MMM') : getShort(_this, 'monthsShort', 'months', 3);
  35. },
  36. firstDayOfWeek: function firstDayOfWeek() {
  37. return _this.$locale().weekStart || 0;
  38. },
  39. weekdays: function weekdays(instance) {
  40. return instance ? instance.format('dddd') : getShort(_this, 'weekdays');
  41. },
  42. weekdaysMin: function weekdaysMin(instance) {
  43. return instance ? instance.format('dd') : getShort(_this, 'weekdaysMin', 'weekdays', 2);
  44. },
  45. weekdaysShort: function weekdaysShort(instance) {
  46. return instance ? instance.format('ddd') : getShort(_this, 'weekdaysShort', 'weekdays', 3);
  47. },
  48. longDateFormat: function longDateFormat(format) {
  49. return getLongDateFormat(_this.$locale(), format);
  50. },
  51. meridiem: this.$locale().meridiem,
  52. ordinal: this.$locale().ordinal
  53. };
  54. };
  55. proto.localeData = function () {
  56. return localeData.bind(this)();
  57. };
  58. dayjs.localeData = function () {
  59. var localeObject = getDayjsLocaleObject();
  60. return {
  61. firstDayOfWeek: function firstDayOfWeek() {
  62. return localeObject.weekStart || 0;
  63. },
  64. weekdays: function weekdays() {
  65. return dayjs.weekdays();
  66. },
  67. weekdaysShort: function weekdaysShort() {
  68. return dayjs.weekdaysShort();
  69. },
  70. weekdaysMin: function weekdaysMin() {
  71. return dayjs.weekdaysMin();
  72. },
  73. months: function months() {
  74. return dayjs.months();
  75. },
  76. monthsShort: function monthsShort() {
  77. return dayjs.monthsShort();
  78. },
  79. longDateFormat: function longDateFormat(format) {
  80. return getLongDateFormat(localeObject, format);
  81. },
  82. meridiem: localeObject.meridiem,
  83. ordinal: localeObject.ordinal
  84. };
  85. };
  86. dayjs.months = function () {
  87. return getShort(getDayjsLocaleObject(), 'months');
  88. };
  89. dayjs.monthsShort = function () {
  90. return getShort(getDayjsLocaleObject(), 'monthsShort', 'months', 3);
  91. };
  92. dayjs.weekdays = function (localeOrder) {
  93. return getShort(getDayjsLocaleObject(), 'weekdays', null, null, localeOrder);
  94. };
  95. dayjs.weekdaysShort = function (localeOrder) {
  96. return getShort(getDayjsLocaleObject(), 'weekdaysShort', 'weekdays', 3, localeOrder);
  97. };
  98. dayjs.weekdaysMin = function (localeOrder) {
  99. return getShort(getDayjsLocaleObject(), 'weekdaysMin', 'weekdays', 2, localeOrder);
  100. };
  101. });