/ shared / logger / node_modules / @sentry / utils / esm / string.js
string.js
  1  import { isString, isRegExp } from './is.js';
  2  
  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  /**
 18   * This is basically just `trim_line` from
 19   * https://github.com/getsentry/sentry/blob/master/src/sentry/lang/javascript/processor.py#L67
 20   *
 21   * @param str An object that contains serializable values
 22   * @param max Maximum number of characters in truncated string
 23   * @returns string Encoded
 24   */
 25  function snipLine(line, colno) {
 26    let newLine = line;
 27    const lineLength = newLine.length;
 28    if (lineLength <= 150) {
 29      return newLine;
 30    }
 31    if (colno > lineLength) {
 32      // eslint-disable-next-line no-param-reassign
 33      colno = lineLength;
 34    }
 35  
 36    let start = Math.max(colno - 60, 0);
 37    if (start < 5) {
 38      start = 0;
 39    }
 40  
 41    let end = Math.min(start + 140, lineLength);
 42    if (end > lineLength - 5) {
 43      end = lineLength;
 44    }
 45    if (end === lineLength) {
 46      start = Math.max(end - 140, 0);
 47    }
 48  
 49    newLine = newLine.slice(start, end);
 50    if (start > 0) {
 51      newLine = `'{snip} ${newLine}`;
 52    }
 53    if (end < lineLength) {
 54      newLine += ' {snip}';
 55    }
 56  
 57    return newLine;
 58  }
 59  
 60  /**
 61   * Join values in array
 62   * @param input array of values to be joined together
 63   * @param delimiter string to be placed in-between values
 64   * @returns Joined values
 65   */
 66  // eslint-disable-next-line @typescript-eslint/no-explicit-any
 67  function safeJoin(input, delimiter) {
 68    if (!Array.isArray(input)) {
 69      return '';
 70    }
 71  
 72    const output = [];
 73    // eslint-disable-next-line @typescript-eslint/prefer-for-of
 74    for (let i = 0; i < input.length; i++) {
 75      const value = input[i];
 76      try {
 77        output.push(String(value));
 78      } catch (e) {
 79        output.push('[value cannot be serialized]');
 80      }
 81    }
 82  
 83    return output.join(delimiter);
 84  }
 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 (!isString(value)) {
100      return false;
101    }
102  
103    if (isRegExp(pattern)) {
104      return pattern.test(value);
105    }
106    if (isString(pattern)) {
107      return requireExactStringMatch ? value === pattern : value.includes(pattern);
108    }
109  
110    return false;
111  }
112  
113  /**
114   * Test the given string against an array of strings and regexes. By default, string matching is done on a
115   * substring-inclusion basis rather than a strict equality basis
116   *
117   * @param testString The string to test
118   * @param patterns The patterns against which to test the string
119   * @param requireExactStringMatch If true, `testString` must match one of the given string patterns exactly in order to
120   * count. If false, `testString` will match a string pattern if it contains that pattern.
121   * @returns
122   */
123  function stringMatchesSomePattern(
124    testString,
125    patterns = [],
126    requireExactStringMatch = false,
127  ) {
128    return patterns.some(pattern => isMatchingPattern(testString, pattern, requireExactStringMatch));
129  }
130  
131  export { isMatchingPattern, safeJoin, snipLine, stringMatchesSomePattern, truncate };
132  //# sourceMappingURL=string.js.map