index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import * as C from '../../constant';
  2. export default (function (o, c, d) {
  3. o = o || {};
  4. var proto = c.prototype;
  5. var relObj = {
  6. future: 'in %s',
  7. past: '%s ago',
  8. s: 'a few seconds',
  9. m: 'a minute',
  10. mm: '%d minutes',
  11. h: 'an hour',
  12. hh: '%d hours',
  13. d: 'a day',
  14. dd: '%d days',
  15. M: 'a month',
  16. MM: '%d months',
  17. y: 'a year',
  18. yy: '%d years'
  19. };
  20. d.en.relativeTime = relObj;
  21. proto.fromToBase = function (input, withoutSuffix, instance, isFrom, postFormat) {
  22. var loc = instance.$locale().relativeTime || relObj;
  23. var T = o.thresholds || [{
  24. l: 's',
  25. r: 44,
  26. d: C.S
  27. }, {
  28. l: 'm',
  29. r: 89
  30. }, {
  31. l: 'mm',
  32. r: 44,
  33. d: C.MIN
  34. }, {
  35. l: 'h',
  36. r: 89
  37. }, {
  38. l: 'hh',
  39. r: 21,
  40. d: C.H
  41. }, {
  42. l: 'd',
  43. r: 35
  44. }, {
  45. l: 'dd',
  46. r: 25,
  47. d: C.D
  48. }, {
  49. l: 'M',
  50. r: 45
  51. }, {
  52. l: 'MM',
  53. r: 10,
  54. d: C.M
  55. }, {
  56. l: 'y',
  57. r: 17
  58. }, {
  59. l: 'yy',
  60. d: C.Y
  61. }];
  62. var Tl = T.length;
  63. var result;
  64. var out;
  65. var isFuture;
  66. for (var i = 0; i < Tl; i += 1) {
  67. var t = T[i];
  68. if (t.d) {
  69. result = isFrom ? d(input).diff(instance, t.d, true) : instance.diff(input, t.d, true);
  70. }
  71. var abs = (o.rounding || Math.round)(Math.abs(result));
  72. isFuture = result > 0;
  73. if (abs <= t.r || !t.r) {
  74. if (abs <= 1 && i > 0) t = T[i - 1]; // 1 minutes -> a minute, 0 seconds -> 0 second
  75. var format = loc[t.l];
  76. if (postFormat) {
  77. abs = postFormat("" + abs);
  78. }
  79. if (typeof format === 'string') {
  80. out = format.replace('%d', abs);
  81. } else {
  82. out = format(abs, withoutSuffix, t.l, isFuture);
  83. }
  84. break;
  85. }
  86. }
  87. if (withoutSuffix) return out;
  88. var pastOrFuture = isFuture ? loc.future : loc.past;
  89. if (typeof pastOrFuture === 'function') {
  90. return pastOrFuture(out);
  91. }
  92. return pastOrFuture.replace('%s', out);
  93. };
  94. function fromTo(input, withoutSuffix, instance, isFrom) {
  95. return proto.fromToBase(input, withoutSuffix, instance, isFrom);
  96. }
  97. proto.to = function (input, withoutSuffix) {
  98. return fromTo(input, withoutSuffix, this, true);
  99. };
  100. proto.from = function (input, withoutSuffix) {
  101. return fromTo(input, withoutSuffix, this);
  102. };
  103. var makeNow = function makeNow(thisDay) {
  104. return thisDay.$u ? d.utc() : d();
  105. };
  106. proto.toNow = function (withoutSuffix) {
  107. return this.to(makeNow(this), withoutSuffix);
  108. };
  109. proto.fromNow = function (withoutSuffix) {
  110. return this.from(makeNow(this), withoutSuffix);
  111. };
  112. });