escape-string.js 568 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. const jsesc = require('jsesc');
  3. /**
  4. Escape string and wrap the result in quotes.
  5. @param {string} string - The string to be quoted.
  6. @param {string} [quote] - The quote character.
  7. @returns {string} - The quoted and escaped string.
  8. */
  9. module.exports = (string, quote = '\'') => {
  10. /* c8 ignore start */
  11. if (typeof string !== 'string') {
  12. throw new TypeError('Unexpected string.');
  13. }
  14. /* c8 ignore end */
  15. return jsesc(string, {
  16. quotes: quote === '"' ? 'double' : 'single',
  17. wrap: true,
  18. es6: true,
  19. minimal: true,
  20. lowercaseHex: false,
  21. });
  22. };