formatters.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import {Release} from '@sentry/release-parser';
  2. import round from 'lodash/round';
  3. import {t, tn} from 'app/locale';
  4. import {CommitAuthor, User} from 'app/types';
  5. export function userDisplayName(user: User | CommitAuthor, includeEmail = true): string {
  6. let displayName = String(user?.name ?? t('Unknown author')).trim();
  7. if (displayName.length <= 0) {
  8. displayName = t('Unknown author');
  9. }
  10. const email = String(user?.email ?? '').trim();
  11. if (email.length > 0 && email !== displayName && includeEmail) {
  12. displayName += ' (' + email + ')';
  13. }
  14. return displayName;
  15. }
  16. export const formatVersion = (rawVersion: string, withPackage = false) => {
  17. try {
  18. const parsedVersion = new Release(rawVersion);
  19. const versionToDisplay = parsedVersion.describe();
  20. if (versionToDisplay.length) {
  21. return `${versionToDisplay}${
  22. withPackage && parsedVersion.package ? `, ${parsedVersion.package}` : ''
  23. }`;
  24. }
  25. return rawVersion;
  26. } catch {
  27. return rawVersion;
  28. }
  29. };
  30. function roundWithFixed(
  31. value: number,
  32. fixedDigits: number
  33. ): {label: string; result: number} {
  34. const label = value.toFixed(fixedDigits);
  35. const result = fixedDigits <= 0 ? Math.round(value) : value;
  36. return {label, result};
  37. }
  38. // in milliseconds
  39. export const MONTH = 2629800000;
  40. export const WEEK = 604800000;
  41. export const DAY = 86400000;
  42. export const HOUR = 3600000;
  43. export const MINUTE = 60000;
  44. export const SECOND = 1000;
  45. export function getDuration(
  46. seconds: number,
  47. fixedDigits: number = 0,
  48. abbreviation: boolean = false,
  49. extraShort: boolean = false
  50. ): string {
  51. const value = Math.abs(seconds * 1000);
  52. if (value >= MONTH && !extraShort) {
  53. const {label, result} = roundWithFixed(value / MONTH, fixedDigits);
  54. return `${label}${
  55. abbreviation ? tn('mo', 'mos', result) : ` ${tn('month', 'months', result)}`
  56. }`;
  57. }
  58. if (value >= WEEK) {
  59. const {label, result} = roundWithFixed(value / WEEK, fixedDigits);
  60. if (extraShort) {
  61. return `${label}${t('w')}`;
  62. }
  63. if (abbreviation) {
  64. return `${label}${t('wk')}`;
  65. }
  66. return `${label} ${tn('week', 'weeks', result)}`;
  67. }
  68. if (value >= 172800000) {
  69. const {label, result} = roundWithFixed(value / DAY, fixedDigits);
  70. return `${label}${
  71. abbreviation || extraShort ? t('d') : ` ${tn('day', 'days', result)}`
  72. }`;
  73. }
  74. if (value >= 7200000) {
  75. const {label, result} = roundWithFixed(value / HOUR, fixedDigits);
  76. if (extraShort) {
  77. return `${label}${t('h')}`;
  78. }
  79. if (abbreviation) {
  80. return `${label}${t('hr')}`;
  81. }
  82. return `${label} ${tn('hour', 'hours', result)}`;
  83. }
  84. if (value >= 120000) {
  85. const {label, result} = roundWithFixed(value / MINUTE, fixedDigits);
  86. if (extraShort) {
  87. return `${label}${t('m')}`;
  88. }
  89. if (abbreviation) {
  90. return `${label}${t('min')}`;
  91. }
  92. return `${label} ${tn('minute', 'minutes', result)}`;
  93. }
  94. if (value >= SECOND) {
  95. const {label, result} = roundWithFixed(value / SECOND, fixedDigits);
  96. if (extraShort || abbreviation) {
  97. return `${label}${t('s')}`;
  98. }
  99. return `${label} ${tn('second', 'seconds', result)}`;
  100. }
  101. const {label} = roundWithFixed(value, fixedDigits);
  102. return label + t('ms');
  103. }
  104. export function getExactDuration(seconds: number, abbreviation: boolean = false) {
  105. const convertDuration = (secs: number, abbr: boolean) => {
  106. const value = round(Math.abs(secs * 1000));
  107. const divideBy = (time: number) => {
  108. return {quotient: Math.floor(value / time), remainder: value % time};
  109. };
  110. if (value >= WEEK) {
  111. const {quotient, remainder} = divideBy(WEEK);
  112. return `${quotient}${
  113. abbr ? t('wk') : ` ${tn('week', 'weeks', quotient)}`
  114. } ${convertDuration(remainder / 1000, abbr)}`;
  115. }
  116. if (value >= DAY) {
  117. const {quotient, remainder} = divideBy(DAY);
  118. return `${quotient}${
  119. abbr ? t('d') : ` ${tn('day', 'days', quotient)}`
  120. } ${convertDuration(remainder / 1000, abbr)}`;
  121. }
  122. if (value >= HOUR) {
  123. const {quotient, remainder} = divideBy(HOUR);
  124. return `${quotient}${
  125. abbr ? t('hr') : ` ${tn('hour', 'hours', quotient)}`
  126. } ${convertDuration(remainder / 1000, abbr)}`;
  127. }
  128. if (value >= MINUTE) {
  129. const {quotient, remainder} = divideBy(MINUTE);
  130. return `${quotient}${
  131. abbr ? t('min') : ` ${tn('minute', 'minutes', quotient)}`
  132. } ${convertDuration(remainder / 1000, abbr)}`;
  133. }
  134. if (value >= SECOND) {
  135. const {quotient, remainder} = divideBy(SECOND);
  136. return `${quotient}${
  137. abbr ? t('s') : ` ${tn('second', 'seconds', quotient)}`
  138. } ${convertDuration(remainder / 1000, abbr)}`;
  139. }
  140. if (value === 0) {
  141. return '';
  142. }
  143. return `${value}${abbr ? t('ms') : ` ${tn('millisecond', 'milliseconds', value)}`}`;
  144. };
  145. const result = convertDuration(seconds, abbreviation).trim();
  146. if (result.length) {
  147. return result;
  148. }
  149. return `0${abbreviation ? t('ms') : ` ${t('milliseconds')}`}`;
  150. }
  151. export function formatFloat(number: number, places: number) {
  152. const multi = Math.pow(10, places);
  153. return parseInt((number * multi).toString(), 10) / multi;
  154. }
  155. /**
  156. * Format a value between 0 and 1 as a percentage
  157. */
  158. export function formatPercentage(value: number, places: number = 2) {
  159. if (value === 0) {
  160. return '0%';
  161. }
  162. return (value * 100).toFixed(places) + '%';
  163. }
  164. const numberFormats = [
  165. [1000000000, 'b'],
  166. [1000000, 'm'],
  167. [1000, 'k'],
  168. ] as const;
  169. export function formatAbbreviatedNumber(number: number | string) {
  170. number = Number(number);
  171. let lookup: typeof numberFormats[number];
  172. // eslint-disable-next-line no-cond-assign
  173. for (let i = 0; (lookup = numberFormats[i]); i++) {
  174. const [suffixNum, suffix] = lookup;
  175. const shortValue = Math.floor(number / suffixNum);
  176. const fitsBound = number % suffixNum;
  177. if (shortValue <= 0) {
  178. continue;
  179. }
  180. return shortValue / 10 > 1 || !fitsBound
  181. ? `${shortValue}${suffix}`
  182. : `${formatFloat(number / suffixNum, 1)}${suffix}`;
  183. }
  184. return number.toLocaleString();
  185. }