quote-string.js 445 B

1234567891011121314151617
  1. 'use strict';
  2. /**
  3. Escape string and wrap the result in quotes.
  4. @param {string} string - The string to be quoted.
  5. @param {string} quote - The quote character.
  6. @returns {string} - The quoted and escaped string.
  7. */
  8. module.exports = (string, quote = '\'') => {
  9. const escaped = string
  10. .replace(/\\/g, '\\\\')
  11. .replace(/\r/g, '\\r')
  12. .replace(/\n/g, '\\n')
  13. .replace(new RegExp(quote, 'g'), `\\${quote}`);
  14. return quote + escaped + quote;
  15. };