Escaper.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Generated by CoffeeScript 1.12.4
  2. var Escaper, Pattern;
  3. Pattern = require('./Pattern');
  4. Escaper = (function() {
  5. var ch;
  6. function Escaper() {}
  7. Escaper.LIST_ESCAPEES = ['\\', '\\\\', '\\"', '"', "\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f", "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", "\x18", "\x19", "\x1a", "\x1b", "\x1c", "\x1d", "\x1e", "\x1f", (ch = String.fromCharCode)(0x0085), ch(0x00A0), ch(0x2028), ch(0x2029)];
  8. Escaper.LIST_ESCAPED = ['\\\\', '\\"', '\\"', '\\"', "\\0", "\\x01", "\\x02", "\\x03", "\\x04", "\\x05", "\\x06", "\\a", "\\b", "\\t", "\\n", "\\v", "\\f", "\\r", "\\x0e", "\\x0f", "\\x10", "\\x11", "\\x12", "\\x13", "\\x14", "\\x15", "\\x16", "\\x17", "\\x18", "\\x19", "\\x1a", "\\e", "\\x1c", "\\x1d", "\\x1e", "\\x1f", "\\N", "\\_", "\\L", "\\P"];
  9. Escaper.MAPPING_ESCAPEES_TO_ESCAPED = (function() {
  10. var i, j, mapping, ref;
  11. mapping = {};
  12. for (i = j = 0, ref = Escaper.LIST_ESCAPEES.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
  13. mapping[Escaper.LIST_ESCAPEES[i]] = Escaper.LIST_ESCAPED[i];
  14. }
  15. return mapping;
  16. })();
  17. Escaper.PATTERN_CHARACTERS_TO_ESCAPE = new Pattern('[\\x00-\\x1f]|\xc2\x85|\xc2\xa0|\xe2\x80\xa8|\xe2\x80\xa9');
  18. Escaper.PATTERN_MAPPING_ESCAPEES = new Pattern(Escaper.LIST_ESCAPEES.join('|').split('\\').join('\\\\'));
  19. Escaper.PATTERN_SINGLE_QUOTING = new Pattern('[\\s\'":{}[\\],&*#?]|^[-?|<>=!%@`]');
  20. Escaper.requiresDoubleQuoting = function(value) {
  21. return this.PATTERN_CHARACTERS_TO_ESCAPE.test(value);
  22. };
  23. Escaper.escapeWithDoubleQuotes = function(value) {
  24. var result;
  25. result = this.PATTERN_MAPPING_ESCAPEES.replace(value, (function(_this) {
  26. return function(str) {
  27. return _this.MAPPING_ESCAPEES_TO_ESCAPED[str];
  28. };
  29. })(this));
  30. return '"' + result + '"';
  31. };
  32. Escaper.requiresSingleQuoting = function(value) {
  33. return this.PATTERN_SINGLE_QUOTING.test(value);
  34. };
  35. Escaper.escapeWithSingleQuotes = function(value) {
  36. return "'" + value.replace(/'/g, "''") + "'";
  37. };
  38. return Escaper;
  39. })();
  40. module.exports = Escaper;