unescape.js 820 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. module.exports = factory;
  3. /* Factory to de-escape a value, based on a list at `key`
  4. * in `ctx`. */
  5. function factory(ctx, key) {
  6. return unescape;
  7. /* De-escape a string using the expression at `key`
  8. * in `ctx`. */
  9. function unescape(value) {
  10. var prev = 0;
  11. var index = value.indexOf('\\');
  12. var escape = ctx[key];
  13. var queue = [];
  14. var character;
  15. while (index !== -1) {
  16. queue.push(value.slice(prev, index));
  17. prev = index + 1;
  18. character = value.charAt(prev);
  19. /* If the following character is not a valid escape,
  20. * add the slash. */
  21. if (!character || escape.indexOf(character) === -1) {
  22. queue.push('\\');
  23. }
  24. index = value.indexOf('\\', prev);
  25. }
  26. queue.push(value.slice(prev));
  27. return queue.join('');
  28. }
  29. }