_createEscaper.js 604 B

1234567891011121314151617
  1. import keys from './keys.js';
  2. // Internal helper to generate functions for escaping and unescaping strings
  3. // to/from HTML interpolation.
  4. export default 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. }