stringify-object icon indicating copy to clipboard operation
stringify-object copied to clipboard

Special characters are not escaped correctly

Open DanielSWolf opened this issue 3 years ago • 1 comments

I believe that stringify-object doesn't correctly escape special characters in strings. Consider the following program:

const stringifyObject = require('stringify-object');

const s = 'tab: \t newline: \n backslash: \\';
console.log(JSON.stringify(s));     // Output: "tab: \t newline: \n backslash: \\"
console.log(stringifyObject(s));    // Output: 'tab: 	 newline: \n backslash: \'

I'd expect stringifyObject() to give results similar to JSON.stringify(), in that both should return a valid JavaScript expression. However, both the tab character and the backslash are not escaped, but are copied to the output verbatim.

In the case of the tab character, the resulting code is unexpected but still technically correct.

In the case of the backslash, the resulting code is no valid JavaScript any more, but leaves an unterminated string.

Only the newline character is escaped as expected.

DanielSWolf avatar Dec 16 '20 11:12 DanielSWolf