helpers.js 620 B

12345678910111213141516171819202122
  1. /*
  2. this seems to be not only shorter, but faster than
  3. string.replace(/\\/g, '\\\\').
  4. replace(/\u0008/g, '\\b').
  5. replace(/\t/g, '\\t').
  6. replace(/\n/g, '\\n').
  7. replace(/\f/g, '\\f').
  8. replace(/\r/g, '\\r').
  9. replace(/'/g, '\\\'').
  10. replace(/"/g, '\\"');
  11. or string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
  12. see http://jsperf.com/string-escape-regexp-vs-json-stringify
  13. */
  14. function srcEscape(str) {
  15. var a = {};
  16. a[str] = 1;
  17. return JSON.stringify(a).slice(1,-3);
  18. }
  19. module.exports.srcEscape = srcEscape;