2019-10-17 refactor cht shared u

2019-10-17  本文已影响0人  五大RobertWu伍洋

get raw function by cht search

var x = "http\\u00253A\\u00252F\\u00252Fexample.com";
var r = /\\u([\d\w]{4})/gi;
x = x.replace(r, function (match, grp) {
    return String.fromCharCode(parseInt(grp, 16)); } );
console.log(x);  // http%3A%2F%2Fexample.com
x = unescape(x);
console.log(x);  // http://example.com

refactor it to a function in tool js

const unicodedecode = (
  unicodeStr = "http\\u00253A\\u00252F\\u00252Fexample.com"
) => {
  const r = /\\u([\d\w]{4})/gi;
  const encodeStr = unicodeStr.replace(r, (match, grp) =>
    String.fromCharCode(parseInt(grp, 16))
  );
  // console.log(encodeStr); // http%3A%2F%2Fexample.com
  const decodeStr = unescape(encodeStr);
  // console.log(decodeStr); // http://example.com
  return { decodeStr, encodeStr };
};

add one option to cli tool

  .option("--ud, --unicodedecode [word]", "unicodedecode the string")

now I could quick test in vscode using bash to call my cli tool

msgs=(
"\u521b\u5efa\u8ba2\u5355\u884c\u6587\u672c\u5931\u8d25"
"\u6210\u529f"
"\u8bb0\u5f55\u5df2\u7ecf\u5b58\u5728"
"\u8c03\u7528\u5931\u8d25"
"\u90e8\u5206\u6210\u529f"
)
# msgs=$(grep message ../abtest_290-ip201_Full-errorInfo-try.txt)
for msg in "${msgs[@]}"
do
echo "$msg"
cli --ud "$msg"
done
上一篇下一篇

猜你喜欢

热点阅读