utils.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import {Query} from 'history';
  2. import isArray from 'lodash/isArray';
  3. import isObject from 'lodash/isObject';
  4. import isString from 'lodash/isString';
  5. import isUndefined from 'lodash/isUndefined';
  6. import {Project} from 'app/types';
  7. import {EventTag} from 'app/types/event';
  8. import {appendTagCondition} from 'app/utils/queryString';
  9. function arrayIsEqual(arr?: any[], other?: any[], deep?: boolean): boolean {
  10. // if the other array is a falsy value, return
  11. if (!arr && !other) {
  12. return true;
  13. }
  14. if (!arr || !other) {
  15. return false;
  16. }
  17. // compare lengths - can save a lot of time
  18. if (arr.length !== other.length) {
  19. return false;
  20. }
  21. return arr.every((val, idx) => valueIsEqual(val, other[idx], deep));
  22. }
  23. export function valueIsEqual(value?: any, other?: any, deep?: boolean): boolean {
  24. if (value === other) {
  25. return true;
  26. } else if (isArray(value) || isArray(other)) {
  27. if (arrayIsEqual(value, other, deep)) {
  28. return true;
  29. }
  30. } else if (isObject(value) || isObject(other)) {
  31. if (objectMatchesSubset(value, other, deep)) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. function objectMatchesSubset(obj?: object, other?: object, deep?: boolean): boolean {
  38. let k: string;
  39. if (obj === other) {
  40. return true;
  41. }
  42. if (!obj || !other) {
  43. return false;
  44. }
  45. if (deep !== true) {
  46. for (k in other) {
  47. if (obj[k] !== other[k]) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. for (k in other) {
  54. if (!valueIsEqual(obj[k], other[k], deep)) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. export function intcomma(x: number): string {
  61. return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  62. }
  63. export function sortArray<T>(arr: Array<T>, score_fn: (entry: T) => string): Array<T> {
  64. arr.sort((a, b) => {
  65. const a_score = score_fn(a),
  66. b_score = score_fn(b);
  67. for (let i = 0; i < a_score.length; i++) {
  68. if (a_score[i] > b_score[i]) {
  69. return 1;
  70. }
  71. if (a_score[i] < b_score[i]) {
  72. return -1;
  73. }
  74. }
  75. return 0;
  76. });
  77. return arr;
  78. }
  79. export function objectIsEmpty(obj = {}): boolean {
  80. for (const prop in obj) {
  81. if (obj.hasOwnProperty(prop)) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. export function trim(str: string): string {
  88. return str.replace(/^\s+|\s+$/g, '');
  89. }
  90. /**
  91. * Replaces slug special chars with a space
  92. */
  93. export function explodeSlug(slug: string): string {
  94. return trim(slug.replace(/[-_]+/g, ' '));
  95. }
  96. export function defined<T>(item: T): item is Exclude<T, null | undefined> {
  97. return !isUndefined(item) && item !== null;
  98. }
  99. export function nl2br(str: string): string {
  100. return str.replace(/(?:\r\n|\r|\n)/g, '<br />');
  101. }
  102. /**
  103. * This function has a critical security impact, make sure to check all usages before changing this function.
  104. * In some parts of our code we rely on that this only really is a string starting with http(s).
  105. */
  106. export function isUrl(str: any): boolean {
  107. return (
  108. !!str &&
  109. isString(str) &&
  110. (str.indexOf('http://') === 0 || str.indexOf('https://') === 0)
  111. );
  112. }
  113. export function escape(str: string): string {
  114. return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  115. }
  116. export function percent(value: number, totalValue: number): number {
  117. // prevent division by zero
  118. if (totalValue === 0) {
  119. return 0;
  120. }
  121. return (value / totalValue) * 100;
  122. }
  123. export function toTitleCase(str: string): string {
  124. return str.replace(
  125. /\w\S*/g,
  126. txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
  127. );
  128. }
  129. /**
  130. * Note the difference between *a-bytes (base 10) vs *i-bytes (base 2), which
  131. * means that:
  132. * - 1000 megabytes is equal to 1 gigabyte
  133. * - 1024 mebibytes is equal to 1 gibibytes
  134. *
  135. * We will use base 10 throughout billing for attachments. This function formats
  136. * quota/usage values for display.
  137. *
  138. * For storage/memory/file sizes, please take a look at formatBytesBase2
  139. */
  140. export function formatBytesBase10(bytes: number, u: number = 0) {
  141. const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  142. const threshold = 1000;
  143. while (bytes >= threshold) {
  144. bytes /= threshold;
  145. u += 1;
  146. }
  147. return bytes.toLocaleString(undefined, {maximumFractionDigits: 2}) + ' ' + units[u];
  148. }
  149. /**
  150. * Note the difference between *a-bytes (base 10) vs *i-bytes (base 2), which
  151. * means that:
  152. * - 1000 megabytes is equal to 1 gigabyte
  153. * - 1024 mebibytes is equal to 1 gibibytes
  154. *
  155. * We will use base 2 to display storage/memory/file sizes as that is commonly
  156. * used by Windows or RAM or CPU cache sizes, and it is more familiar to the user
  157. *
  158. * For billing-related code around attachments. please take a look at
  159. * formatBytesBase10
  160. */
  161. export function formatBytesBase2(bytes: number): string {
  162. const units = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  163. const thresh = 1024;
  164. if (bytes < thresh) {
  165. return bytes + ' B';
  166. }
  167. let u = -1;
  168. do {
  169. bytes /= thresh;
  170. ++u;
  171. } while (bytes >= thresh);
  172. return bytes.toFixed(1) + ' ' + units[u];
  173. }
  174. export function getShortCommitHash(hash: string): string {
  175. if (hash.match(/^[a-f0-9]{40}$/)) {
  176. hash = hash.substr(0, 7);
  177. }
  178. return hash;
  179. }
  180. export function parseRepo<T>(repo: T): T {
  181. if (typeof repo === 'string') {
  182. const re = /(?:github\.com|bitbucket\.org)\/([^\/]+\/[^\/]+)/i;
  183. const match = repo.match(re);
  184. const parsedRepo = match ? match[1] : repo;
  185. return parsedRepo as any;
  186. }
  187. return repo;
  188. }
  189. /**
  190. * Converts a multi-line textarea input value into an array,
  191. * eliminating empty lines
  192. */
  193. export function extractMultilineFields(value: string): Array<string> {
  194. return value
  195. .split('\n')
  196. .map(f => trim(f))
  197. .filter(f => f !== '');
  198. }
  199. /**
  200. * If the value is of type Array, converts it to type string, keeping the line breaks, if there is any
  201. */
  202. export function convertMultilineFieldValue<T extends string | Array<string>>(
  203. value: T
  204. ): string {
  205. if (Array.isArray(value)) {
  206. return value.join('\n');
  207. }
  208. if (typeof value === 'string') {
  209. return value.split('\n').join('\n');
  210. }
  211. return '';
  212. }
  213. function projectDisplayCompare(a: Project, b: Project): number {
  214. if (a.isBookmarked !== b.isBookmarked) {
  215. return a.isBookmarked ? -1 : 1;
  216. }
  217. return a.slug.localeCompare(b.slug);
  218. }
  219. // Sort a list of projects by bookmarkedness, then by id
  220. export function sortProjects(projects: Array<Project>): Array<Project> {
  221. return projects.sort(projectDisplayCompare);
  222. }
  223. // build actorIds
  224. export const buildUserId = id => `user:${id}`;
  225. export const buildTeamId = id => `team:${id}`;
  226. /**
  227. * Removes the organization / project scope prefix on feature names.
  228. */
  229. export function descopeFeatureName<T>(feature: T): T | string {
  230. if (typeof feature !== 'string') {
  231. return feature;
  232. }
  233. const results = feature.match(/(?:^(?:projects|organizations):)?(.*)/);
  234. if (results && results.length > 0) {
  235. return results.pop()!;
  236. }
  237. return feature;
  238. }
  239. export function isWebpackChunkLoadingError(error: Error): boolean {
  240. return (
  241. error &&
  242. typeof error.message === 'string' &&
  243. error.message.toLowerCase().includes('loading chunk')
  244. );
  245. }
  246. export function deepFreeze<T>(object: T) {
  247. // Retrieve the property names defined on object
  248. const propNames = Object.getOwnPropertyNames(object);
  249. // Freeze properties before freezing self
  250. for (const name of propNames) {
  251. const value = object[name];
  252. object[name] = value && typeof value === 'object' ? deepFreeze(value) : value;
  253. }
  254. return Object.freeze(object);
  255. }
  256. export type OmitHtmlDivProps<P extends object> = Omit<
  257. React.HTMLProps<HTMLDivElement>,
  258. keyof P
  259. > &
  260. P;
  261. export function generateQueryWithTag(prevQuery: Query, tag: EventTag): Query {
  262. const query = {...prevQuery};
  263. // some tags are dedicated query strings since other parts of the app consumes this,
  264. // for example, the global selection header.
  265. switch (tag.key) {
  266. case 'environment':
  267. query.environment = tag.value;
  268. break;
  269. case 'project':
  270. query.project = tag.value;
  271. break;
  272. default:
  273. query.query = appendTagCondition(query.query, tag.key, tag.value);
  274. }
  275. return query;
  276. }
  277. export const isFunction = (value: any): value is Function => typeof value === 'function';
  278. // NOTE: only escapes a " if it's not already escaped
  279. export function escapeDoubleQuotes(str) {
  280. return str.replace(/\\([\s\S])|(")/g, '\\$1$2');
  281. }