string.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const is = require('./is.js');
  3. /**
  4. * Truncates given string to the maximum characters count
  5. *
  6. * @param str An object that contains serializable values
  7. * @param max Maximum number of characters in truncated string (0 = unlimited)
  8. * @returns string Encoded
  9. */
  10. function truncate(str, max = 0) {
  11. if (typeof str !== 'string' || max === 0) {
  12. return str;
  13. }
  14. return str.length <= max ? str : `${str.slice(0, max)}...`;
  15. }
  16. /**
  17. * This is basically just `trim_line` from
  18. * https://github.com/getsentry/sentry/blob/master/src/sentry/lang/javascript/processor.py#L67
  19. *
  20. * @param str An object that contains serializable values
  21. * @param max Maximum number of characters in truncated string
  22. * @returns string Encoded
  23. */
  24. function snipLine(line, colno) {
  25. let newLine = line;
  26. const lineLength = newLine.length;
  27. if (lineLength <= 150) {
  28. return newLine;
  29. }
  30. if (colno > lineLength) {
  31. // eslint-disable-next-line no-param-reassign
  32. colno = lineLength;
  33. }
  34. let start = Math.max(colno - 60, 0);
  35. if (start < 5) {
  36. start = 0;
  37. }
  38. let end = Math.min(start + 140, lineLength);
  39. if (end > lineLength - 5) {
  40. end = lineLength;
  41. }
  42. if (end === lineLength) {
  43. start = Math.max(end - 140, 0);
  44. }
  45. newLine = newLine.slice(start, end);
  46. if (start > 0) {
  47. newLine = `'{snip} ${newLine}`;
  48. }
  49. if (end < lineLength) {
  50. newLine += ' {snip}';
  51. }
  52. return newLine;
  53. }
  54. /**
  55. * Join values in array
  56. * @param input array of values to be joined together
  57. * @param delimiter string to be placed in-between values
  58. * @returns Joined values
  59. */
  60. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  61. function safeJoin(input, delimiter) {
  62. if (!Array.isArray(input)) {
  63. return '';
  64. }
  65. const output = [];
  66. // eslint-disable-next-line @typescript-eslint/prefer-for-of
  67. for (let i = 0; i < input.length; i++) {
  68. const value = input[i];
  69. try {
  70. // This is a hack to fix a Vue3-specific bug that causes an infinite loop of
  71. // console warnings. This happens when a Vue template is rendered with
  72. // an undeclared variable, which we try to stringify, ultimately causing
  73. // Vue to issue another warning which repeats indefinitely.
  74. // see: https://github.com/getsentry/sentry-javascript/pull/8981
  75. if (is.isVueViewModel(value)) {
  76. output.push('[VueViewModel]');
  77. } else {
  78. output.push(String(value));
  79. }
  80. } catch (e) {
  81. output.push('[value cannot be serialized]');
  82. }
  83. }
  84. return output.join(delimiter);
  85. }
  86. /**
  87. * Checks if the given value matches a regex or string
  88. *
  89. * @param value The string to test
  90. * @param pattern Either a regex or a string against which `value` will be matched
  91. * @param requireExactStringMatch If true, `value` must match `pattern` exactly. If false, `value` will match
  92. * `pattern` if it contains `pattern`. Only applies to string-type patterns.
  93. */
  94. function isMatchingPattern(
  95. value,
  96. pattern,
  97. requireExactStringMatch = false,
  98. ) {
  99. if (!is.isString(value)) {
  100. return false;
  101. }
  102. if (is.isRegExp(pattern)) {
  103. return pattern.test(value);
  104. }
  105. if (is.isString(pattern)) {
  106. return requireExactStringMatch ? value === pattern : value.includes(pattern);
  107. }
  108. return false;
  109. }
  110. /**
  111. * Test the given string against an array of strings and regexes. By default, string matching is done on a
  112. * substring-inclusion basis rather than a strict equality basis
  113. *
  114. * @param testString The string to test
  115. * @param patterns The patterns against which to test the string
  116. * @param requireExactStringMatch If true, `testString` must match one of the given string patterns exactly in order to
  117. * count. If false, `testString` will match a string pattern if it contains that pattern.
  118. * @returns
  119. */
  120. function stringMatchesSomePattern(
  121. testString,
  122. patterns = [],
  123. requireExactStringMatch = false,
  124. ) {
  125. return patterns.some(pattern => isMatchingPattern(testString, pattern, requireExactStringMatch));
  126. }
  127. exports.isMatchingPattern = isMatchingPattern;
  128. exports.safeJoin = safeJoin;
  129. exports.snipLine = snipLine;
  130. exports.stringMatchesSomePattern = stringMatchesSomePattern;
  131. exports.truncate = truncate;
  132. //# sourceMappingURL=string.js.map