eslint-plugin-unicorn
Version:
Various awesome ESLint rules
18 lines (15 loc) • 445 B
JavaScript
/**
Escape string and wrap the result in quotes.
@param {string} string - The string to be quoted.
@param {string} quote - The quote character.
@returns {string} - The quoted and escaped string.
*/
module.exports = (string, quote = '\'') => {
const escaped = string
.replace(/\\/g, '\\\\')
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(new RegExp(quote, 'g'), `\\${quote}`);
return quote + escaped + quote;
};
;