๐Ÿ“ฆ Kong / httpsnippet

๐Ÿ“„ escape.ts ยท 89 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89export interface EscapeOptions {
  /**
   * The delimiter that will be used to wrap the string (and so must be escaped
   * when used within the string).
   * Defaults to "
   */
  delimiter?: string;

  /**
   * The char to use to escape the delimiter and other special characters.
   * Defaults to \
   */
  escapeChar?: string;

  /**
   * Whether newlines (\n and \r) should be escaped within the string.
   * Defaults to true.
   */
  escapeNewlines?: boolean;
}

/**
 * Escape characters within a value to make it safe to insert directly into a
 * snippet. Takes options which define the escape requirements.
 *
 * This is closely based on the JSON-stringify string serialization algorithm,
 * but generalized for other string delimiters (e.g. " or ') and different escape
 * characters (e.g. Powershell uses `)
 *
 * See https://tc39.es/ecma262/multipage/structured-data.html#sec-quotejsonstring
 * for the complete original algorithm.
 */
export function escapeString(rawValue: any, options: EscapeOptions = {}) {
  const { delimiter = '"', escapeChar = '\\', escapeNewlines = true } = options;

  const stringValue = rawValue.toString();

  return [...stringValue]
    .map(c => {
      if (c === '\b') {
        return `${escapeChar}b`;
      } else if (c === '\t') {
        return `${escapeChar}t`;
      } else if (c === '\n') {
        if (escapeNewlines) {
          return `${escapeChar}n`;
        }
        return c; // Don't just continue, or this is caught by < \u0020
      } else if (c === '\f') {
        return `${escapeChar}f`;
      } else if (c === '\r') {
        if (escapeNewlines) {
          return `${escapeChar}r`;
        }
        return c; // Don't just continue, or this is caught by < \u0020
      } else if (c === escapeChar) {
        return escapeChar + escapeChar;
      } else if (c === delimiter) {
        return escapeChar + delimiter;
      } else if (c < '\u0020' || c > '\u007E') {
        // Delegate the trickier non-ASCII cases to the normal algorithm. Some of these
        // are escaped as \uXXXX, whilst others are represented literally. Since we're
        // using this primarily for header values that are generally (though not 100%
        // strictly?) ASCII-only, this should almost never happen.
        return JSON.stringify(c).slice(1, -1);
      }
      return c;
    })
    .join('');
}

/**
 * Make a string value safe to insert literally into a snippet within single quotes,
 * by escaping problematic characters, including single quotes inside the string,
 * backslashes, newlines, and other special characters.
 *
 * If value is not a string, it will be stringified with .toString() first.
 */
export const escapeForSingleQuotes = (value: any) => escapeString(value, { delimiter: "'" });

/**
 * Make a string value safe to insert literally into a snippet within double quotes,
 * by escaping problematic characters, including double quotes inside the string,
 * backslashes, newlines, and other special characters.
 *
 * If value is not a string, it will be stringified with .toString() first.
 */
export const escapeForDoubleQuotes = (value: any) => escapeString(value, { delimiter: '"' });