unescape() 已弃用方法的替代方法?

问题描述 投票:0回答:1

unesacpe()
方法有哪些替代方法?我知道
decodeURI
decodeURIComponent
是网上列出的一些。但是如果我有一个像这样的字符串怎么办
test%20test%
?调用
decodeURIComponent
只会导致 uri 错误,而不是“test test%”。我想使用
unescape
但它已被弃用。我还能做什么?

javascript escaping uri decode deprecated
1个回答
0
投票

这是我对 escape 和 unescape 函数的替换实现。

/**
 * The [escape()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape) function ~ _**deprecation-alt**_
 * - Replaces all characters with escape sequences, with the exception of ASCII word characters `(A–Z, a–z, 0–9, _)` and `@\*_+-./`.
 * - [EcmaScript spec](https://262.ecma-international.org/5.1/#sec-B.2.1)
 * 
 * @param {any} value - parse text
 * @returns {string} escaped text
 */
function _escape(value) {
  const text = String(value);
  if (!text.length)
    return text;
  const skip = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./,';
  let buffer = '';
  for (let i = 0; i < text.length; i++) {
    let char = text[i];
    if (skip.indexOf(char) < 0) {
      const ord = text.charCodeAt(i);
      char = ord < 256 ? '%' + ('00' + ord.toString(16)).toUpperCase().slice(-2) : '%u' + ('0000' + ord.toString(16)).toUpperCase().slice(-4);
    }
    buffer += char;
  }
  return buffer;
}

/**
 * The [unescape()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape) function ~ _**deprecation-alt**_
 * - Computes a new string in which hexadecimal escape sequences are replaced with the characters that they represent ~ _see `_escape()`_
 * - [EcmaScript spec](https://262.ecma-international.org/5.1/#sec-B.2.2)
 * 
 * @param {any} value - parse text
 * @returns {string} unescaped text
 */
function _unescape(value) {
  const text = String(value),
    len = text.length;
  if (!len)
    return text;
  let buffer = '',
    k = 0;
  while (k < len) {
    let char = text[k];
    if (char === '%') {
      let chars = k <= (len - 6) && text[k + 1] === 'u' ? text.substring(k + 2, k + 6) : k <= (len - 3) ? text.substring(k + 1, k + 3) : '';
      if (!/^[0-9A-F]+$/i.test(chars)) chars = '';
      if (chars.length === 4) {
        char = String.fromCharCode(parseInt(chars, 16));
        k += 5;
      } else if (chars.length === 2) {
        char = String.fromCharCode(parseInt('00' + chars, 16));
        k += 2;
      }
    }
    buffer += char;
    k += 1;
  }
  return buffer;
}


//-- test values
const tests = [
  ['abc123', 'abc123'],
  ['äöü', '%E4%F6%FC'],
  ['ć', '%u0107'],
];

//-- test `_escape`
tests.forEach(test => {
  const value = test[0];
  const result = test[1];
  console.log(`_escape('${value}') === '${result}'`, _escape(value) === result);
});

//-- test `_unescape`
tests.forEach(test => {
  const value = test[1];
  const result = test[0];
  console.log(`_unescape('${value}') === '${result}'`, _unescape(value) === result);
});

// _escape('abc123') === 'abc123' true
// _escape('äöü') === '%E4%F6%FC' true
// _escape('ć') === '%u0107' true
// _unescape('abc123') === 'abc123' true
// _unescape('%E4%F6%FC') === 'äöü' true
// _unescape('%u0107') === 'ć' true

© www.soinside.com 2019 - 2024. All rights reserved.