diff --git a/lib/utils/common.js b/lib/utils/common.js index 408f154..59dd7f7 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -26,7 +26,24 @@ utils.common = { return list }, quote: function (obj) { - return JSON.stringify(obj) + function escapeString (s) { + let escaped = s.replace(/\\/g, '\\\\') + .replace(/\"/g, '\\\"') // eslint-disable-line no-useless-escape + .replace(/\0/g, '\\0') + .replace(/\n/g, '\\n') + .replace(/[^\0-~]/g, function (ch) { + return '\\u' + ('000' + ch.charCodeAt(0).toString(16)).slice(-4) + }) + + return '\"' + escaped + '\"' // eslint-disable-line no-useless-escape + } + + if (typeof s === 'string') { + return escapeString(obj) + } else { + // For other things (such as numbers, |null|, and |undefined|), stringify + return JSON.stringify(obj) + } }, b64encode: function (str) { // Unicode safe b64 encoding