pugjs / js-stringify

Stringify an object so it can be safely inlined in JavaScript code
MIT License
19 stars 4 forks source link

Add single quotes to the mix? #7

Closed gsteri1 closed 5 years ago

gsteri1 commented 5 years ago

Single quotes will screw up pug templates. I like your library, but it didn't escape single quotes. I modified your code and the relevant bit looks like:

module.exports = stringify;
function stringify(obj) {
  if (obj instanceof Date) {
    return 'new Date(' + stringify(obj.toISOString()) + ')';
  }
  if (obj === undefined) {
    return 'undefined';
  }
  return JSON.stringify(obj)
             .replace(/\u2028/g, '\\u2028')
             .replace(/\u2029/g, '\\u2029')
             .replace(/</g, '\\u003C')
             .replace(/>/g, '\\u003E')
             .replace(/'/g, '\\u0027')
             .replace(/\//g, '\\u002F');
}
ForbesLindesay commented 5 years ago

single quotes work fine in pug templates