_createEscaper.js 625 B

12345678910111213141516171819
  1. var keys = require('./keys.js');
  2. // Internal helper to generate functions for escaping and unescaping strings
  3. // to/from HTML interpolation.
  4. function createEscaper(map) {
  5. var escaper = function(match) {
  6. return map[match];
  7. };
  8. // Regexes for identifying a key that needs to be escaped.
  9. var source = '(?:' + keys(map).join('|') + ')';
  10. var testRegexp = RegExp(source);
  11. var replaceRegexp = RegExp(source, 'g');
  12. return function(string) {
  13. string = string == null ? '' : '' + string;
  14. return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
  15. };
  16. }
  17. module.exports = createEscaper;