Guest User

Untitled

a guest
Feb 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. const FORMAT_DISTANCE_LOCALE = {
  2. lessThanXSeconds: {
  3. one: "'less than a second'",
  4. other: "'less than 's' seconds'"
  5. },
  6.  
  7. xSeconds: {
  8. one: "s' second'",
  9. other: "s' seconds'"
  10. },
  11.  
  12. halfAMinute: 'half a minute',
  13.  
  14. lessThanXMinutes: {
  15. one: "'less than a minute'",
  16. other: "'less than 'm' minutes'"
  17. },
  18.  
  19. xMinutes: {
  20. one: "m' minute'",
  21. other: "m' minutes'"
  22. },
  23.  
  24. aboutXHours: {
  25. one: "'about 'h' hour'",
  26. other: "'about 'h' hours'"
  27. },
  28.  
  29. xHours: {
  30. one: "h' hour'",
  31. other: "h' hours'"
  32. },
  33.  
  34. xDays: {
  35. one: "d' day'",
  36. other: '{{count}} days'
  37. },
  38.  
  39. aboutXMonths: {
  40. one: "'about 'M' month'",
  41. other: "'about 'M' months'"
  42. },
  43.  
  44. xMonths: {
  45. one: "M' month'",
  46. other: "M' months'"
  47. },
  48.  
  49. aboutXYears: {
  50. one: "'about 'y' year'",
  51. other: 'about {{count}} years'
  52. },
  53.  
  54. xYears: {
  55. one: "y' year'",
  56. other: "y' years'"
  57. },
  58.  
  59. overXYears: {
  60. one: "'over 'y' year'",
  61. other: "'over 'y' years'"
  62. },
  63.  
  64. almostXYears: {
  65. one: "'almost 'y' year'",
  66. other: "'almost 'y' years'"
  67. }
  68. }
  69.  
  70. const format = (token, duration, options) => {
  71. options = options || {}
  72.  
  73. let result
  74.  
  75. if (typeof formatDistanceLocale[token] === 'string') {
  76. result = formatDistanceLocale[token]
  77. } else if (count === 1) {
  78. result = duration.toFormat(formatDistanceLocale[token].one)
  79. } else {
  80. result = duration.toFormat(formatDistanceLocale[token].other)
  81. }
  82.  
  83. if (options.addSuffix) {
  84. if (options.comparison > 0) {
  85. return `in ${result}`
  86. } else {
  87. return `${result} ago`
  88. }
  89. }
  90.  
  91. return result
  92. }
  93.  
  94. export default formatDistance = (duration, options = { includeSeconds: false, addSuffix: false }) => {
  95. const seconds = Math.abs(duration.as('seconds'));
  96. const minutes = Math.abs(duration.as('minutes'));
  97. const days = Math.abs(duration.as('days'));
  98. const months = Math.abs(duration.as('months'));
  99. const years = Math.abs(duration.as('years'));
  100. const localizeOptions = {...options, comparison: duration.as('milliseconds') > 0 ? 1 : 0};
  101.  
  102. // 0 up to 2 mins
  103. if (minutes < 2) {
  104. if (options.includeSeconds) {
  105. if (seconds < 5) {
  106. return format('lessThanXSeconds', Duration.fromObject({seconds: 5}), localizeOptions)
  107. } else if (seconds < 10) {
  108. return format('lessThanXSeconds', Duration.fromObject({seconds: 10}), localizeOptions)
  109. } else if (seconds < 20) {
  110. return format('lessThanXSeconds', Duration.fromObject({seconds: 20}), localizeOptions)
  111. } else if (seconds < 40) {
  112. return format('halfAMinute', null, localizeOptions)
  113. } else if (seconds < 60) {
  114. return format('lessThanXMinutes', Duration.fromObject({minutes: 1}), localizeOptions)
  115. } else {
  116. return format('xMinutes', Duration.fromObject({minutes: 1}), localizeOptions)
  117. }
  118. } else {
  119. if (minutes === 0) {
  120. return format('lessThanXMinutes', Duration.fromObject({minutes: 1}), localizeOptions)
  121. } else {
  122. return format('xMinutes', Duration.fromObject({minutes}), localizeOptions)
  123. }
  124. }
  125.  
  126. // 2 mins up to 0.75 hrs
  127. } else if (minutes < 45) {
  128. return format('xMinutes', Duration.fromObject({minutes}), localizeOptions)
  129.  
  130. // 0.75 hrs up to 1.5 hrs
  131. } else if (minutes < 90) {
  132. return format('aboutXHours', Duration.fromObject({hours: 1}), localizeOptions)
  133.  
  134. // 1.5 hrs up to 24 hrs
  135. } else if (hours < 24) {
  136. return format('aboutXHours', Duration.fromObject({hours}), localizeOptions)
  137.  
  138. // 1 day up to 1.75 days
  139. } else if (days < 1.75) {
  140. return format('xDays', Duration.fromObject({days: 1}), localizeOptions)
  141.  
  142. // 1.75 days up to 30 days
  143. } else if (months < 1) {
  144. return format('xDays', Duration.fromObject({days}), localizeOptions)
  145.  
  146. // 1 month up to 2 months
  147. } else if (months < 2) {
  148. return format('aboutXMonths', Duration.fromObject({months}), localizeOptions)
  149. }
  150.  
  151. // 2 months up to 12 months
  152. if (months < 12) {
  153. return format('xMonths', Duration.fromObject({months}), localizeOptions)
  154.  
  155. // 1 year up to max Date
  156. } else {
  157. const monthsSinceStartOfYear = months % 12
  158.  
  159. // N years up to 1 years 3 months
  160. if (monthsSinceStartOfYear < 3) {
  161. return format('aboutXYears', Duration.fromObject({years}), localizeOptions)
  162.  
  163. // N years 3 months up to N years 9 months
  164. } else if (monthsSinceStartOfYear < 9) {
  165. return format('overXYears', Duration.fromObject({years}), localizeOptions)
  166.  
  167. // N years 9 months up to N year 12 months
  168. } else {
  169. return format('almostXYears', Duration.fromObject({years: years + 1}), localizeOptions)
  170. }
  171. }
  172. }
Add Comment
Please, Sign In to add comment