index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. export default (function (o, c, d) {
  2. var LT = 'h:mm A';
  3. var L = 'MM/DD/YYYY';
  4. var calendarFormat = {
  5. lastDay: "[Yesterday at] " + LT,
  6. sameDay: "[Today at] " + LT,
  7. nextDay: "[Tomorrow at] " + LT,
  8. nextWeek: "dddd [at] " + LT,
  9. lastWeek: "[Last] dddd [at] " + LT,
  10. sameElse: L
  11. };
  12. var proto = c.prototype;
  13. proto.calendar = function (referenceTime, formats) {
  14. var format = formats || this.$locale().calendar || calendarFormat;
  15. var referenceStartOfDay = d(referenceTime || undefined).startOf('d');
  16. var diff = this.diff(referenceStartOfDay, 'd', true);
  17. var sameElse = 'sameElse';
  18. /* eslint-disable no-nested-ternary */
  19. var retVal = diff < -6 ? sameElse : diff < -1 ? 'lastWeek' : diff < 0 ? 'lastDay' : diff < 1 ? 'sameDay' : diff < 2 ? 'nextDay' : diff < 7 ? 'nextWeek' : sameElse;
  20. /* eslint-enable no-nested-ternary */
  21. var currentFormat = format[retVal] || calendarFormat[retVal];
  22. if (typeof currentFormat === 'function') {
  23. return currentFormat.call(this, d());
  24. }
  25. return this.format(currentFormat);
  26. };
  27. });