Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
16 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
'use strict'; | ||
|
||
/** | ||
Escape apostrophe and wrap the result in single quotes. | ||
Escape string and wrap the result in quotes. | ||
@param {string} string - The string to be quoted. | ||
@returns {string} - The quoted string. | ||
@param {quote} string - The quote character. | ||
@returns {string} - The quoted and escaped string. | ||
*/ | ||
module.exports = string => `'${string.replace(/'/g, '\\\'')}'`; | ||
module.exports = (string, quote = '\'') => { | ||
const escaped = string | ||
.replace(new RegExp(quote, 'g'), `\\${quote}`) | ||
.replace(/\r/g, '\\r') | ||
.replace(/\n/g, '\\n'); | ||
return quote + escaped + quote; | ||
}; |