mathiasbynens / jsesc

Given some data, jsesc returns the shortest possible stringified & ASCII-safe representation of that data.
https://mths.be/jsesc
MIT License
716 stars 53 forks source link

'Allow newlines' option #20

Closed cohix closed 9 years ago

cohix commented 9 years ago

jsesc should have an option to allow newline characters:

var escaped = jsesc(string, {newlinesAllowed: true} );

So if the string contains a newline character (\n), it would not be replaced by \\n

mathiasbynens commented 9 years ago

What’s your use case exactly?

cohix commented 9 years ago

I have a field in my database that I want to allow real newline characters.

mathiasbynens commented 9 years ago

For now this can be solved with:

var escaped = string.split('\n').map(function(line) {
  return jsesc(line);
}).join('\n');

I’m worried that this is not common enough to extend the API for. Also, allowNewlines seems like too specific an option. If we decide to add something like this, maybe add a whitelist property instead?

cohix commented 9 years ago

Yeah I like the whitelist idea. Good call.