es.unescape.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var toString = require('../internals/to-string');
  5. var fromCharCode = String.fromCharCode;
  6. var charAt = uncurryThis(''.charAt);
  7. var exec = uncurryThis(/./.exec);
  8. var stringSlice = uncurryThis(''.slice);
  9. var hex2 = /^[\da-f]{2}$/i;
  10. var hex4 = /^[\da-f]{4}$/i;
  11. // `unescape` method
  12. // https://tc39.es/ecma262/#sec-unescape-string
  13. $({ global: true }, {
  14. unescape: function unescape(string) {
  15. var str = toString(string);
  16. var result = '';
  17. var length = str.length;
  18. var index = 0;
  19. var chr, part;
  20. while (index < length) {
  21. chr = charAt(str, index++);
  22. if (chr === '%') {
  23. if (charAt(str, index) === 'u') {
  24. part = stringSlice(str, index + 1, index + 5);
  25. if (exec(hex4, part)) {
  26. result += fromCharCode(parseInt(part, 16));
  27. index += 5;
  28. continue;
  29. }
  30. } else {
  31. part = stringSlice(str, index, index + 2);
  32. if (exec(hex2, part)) {
  33. result += fromCharCode(parseInt(part, 16));
  34. index += 2;
  35. continue;
  36. }
  37. }
  38. }
  39. result += chr;
  40. } return result;
  41. }
  42. });