_createEscaper.js 656 B

123456789101112131415161718192021
  1. define(['./keys'], function (keys) {
  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. return createEscaper;
  18. });