cb3dcd067cffa1b2a65ca9e7461b6a6f307ff5a31255b4f27b09efedea2761785fe5627f829a16a31c6348a6d66da8be4994bbab1d79b5ed934259eb5358f1 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { createDuration } from './create';
  2. var round = Math.round,
  3. thresholds = {
  4. ss: 44, // a few seconds to seconds
  5. s: 45, // seconds to minute
  6. m: 45, // minutes to hour
  7. h: 22, // hours to day
  8. d: 26, // days to month/week
  9. w: null, // weeks to month
  10. M: 11, // months to year
  11. };
  12. // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
  13. function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
  14. return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
  15. }
  16. function relativeTime(posNegDuration, withoutSuffix, thresholds, locale) {
  17. var duration = createDuration(posNegDuration).abs(),
  18. seconds = round(duration.as('s')),
  19. minutes = round(duration.as('m')),
  20. hours = round(duration.as('h')),
  21. days = round(duration.as('d')),
  22. months = round(duration.as('M')),
  23. weeks = round(duration.as('w')),
  24. years = round(duration.as('y')),
  25. a =
  26. (seconds <= thresholds.ss && ['s', seconds]) ||
  27. (seconds < thresholds.s && ['ss', seconds]) ||
  28. (minutes <= 1 && ['m']) ||
  29. (minutes < thresholds.m && ['mm', minutes]) ||
  30. (hours <= 1 && ['h']) ||
  31. (hours < thresholds.h && ['hh', hours]) ||
  32. (days <= 1 && ['d']) ||
  33. (days < thresholds.d && ['dd', days]);
  34. if (thresholds.w != null) {
  35. a =
  36. a ||
  37. (weeks <= 1 && ['w']) ||
  38. (weeks < thresholds.w && ['ww', weeks]);
  39. }
  40. a = a ||
  41. (months <= 1 && ['M']) ||
  42. (months < thresholds.M && ['MM', months]) ||
  43. (years <= 1 && ['y']) || ['yy', years];
  44. a[2] = withoutSuffix;
  45. a[3] = +posNegDuration > 0;
  46. a[4] = locale;
  47. return substituteTimeAgo.apply(null, a);
  48. }
  49. // This function allows you to set the rounding function for relative time strings
  50. export function getSetRelativeTimeRounding(roundingFunction) {
  51. if (roundingFunction === undefined) {
  52. return round;
  53. }
  54. if (typeof roundingFunction === 'function') {
  55. round = roundingFunction;
  56. return true;
  57. }
  58. return false;
  59. }
  60. // This function allows you to set a threshold for relative time strings
  61. export function getSetRelativeTimeThreshold(threshold, limit) {
  62. if (thresholds[threshold] === undefined) {
  63. return false;
  64. }
  65. if (limit === undefined) {
  66. return thresholds[threshold];
  67. }
  68. thresholds[threshold] = limit;
  69. if (threshold === 's') {
  70. thresholds.ss = limit - 1;
  71. }
  72. return true;
  73. }
  74. export function humanize(argWithSuffix, argThresholds) {
  75. if (!this.isValid()) {
  76. return this.localeData().invalidDate();
  77. }
  78. var withSuffix = false,
  79. th = thresholds,
  80. locale,
  81. output;
  82. if (typeof argWithSuffix === 'object') {
  83. argThresholds = argWithSuffix;
  84. argWithSuffix = false;
  85. }
  86. if (typeof argWithSuffix === 'boolean') {
  87. withSuffix = argWithSuffix;
  88. }
  89. if (typeof argThresholds === 'object') {
  90. th = Object.assign({}, thresholds, argThresholds);
  91. if (argThresholds.s != null && argThresholds.ss == null) {
  92. th.ss = argThresholds.s - 1;
  93. }
  94. }
  95. locale = this.localeData();
  96. output = relativeTime(this, !withSuffix, th, locale);
  97. if (withSuffix) {
  98. output = locale.pastFuture(+this, output);
  99. }
  100. return locale.postformat(output);
  101. }