# all files / src/ utils.js

100% Statements 6/6
62.5% Branches 5/8
100% Functions 2/2
100% Lines 4/4

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

    27×

    27×
    8×

    /**
     * Escape quotes in the given value.
     *
     * @param {string} value The value to escape.
     * @param {string} [defaultValue=''] The default value to return if value is falsy.
     * @returns {string}
     */
    export function escapeQuotes(value, defaultValue = '') {
      // There's no lookback in JS, so /(^|[^\\])"/ only matches the first of two `"`s.
      // Instead, just match anything before a double-quote and escape if it's not already escaped.
      return !value ? defaultValue : value.replace(/([^"]*)"/g, (_, prefix) => {
        return (/\\/).test(prefix) ? `${prefix}"` : `${prefix}\\\"`;
      });
    }