endecode-lib.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /**
  2. * 本库提供几个常用方法:
  3. * 1、enDecodeTools.uniEncode(text); 将中文进行Unicode编码并输出
  4. * 2、enDecodeTools.base64Encode(text); 将文字进行base64编码并输出
  5. * 3、enDecodeTools.base64Decode(text); 将经过base64编码的文字进行base64解码并输出
  6. * 4、enDecodeTools.utf8Encode(text); 将文字进行utf-8编码并输出
  7. * 5、enDecodeTools.utf8Decode(text); 将经过utf-8编码的文字进行utf-8解码并输出
  8. */
  9. import Md5Utils from './md5.js';
  10. let EncodeUtils = (() => {
  11. //base64编码字符集
  12. let _base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  13. //base64解码字符集
  14. let _base64DecodeChars = [
  15. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  16. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  17. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  18. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  19. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  20. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  21. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  22. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];
  23. /**
  24. * 此方法实现中文向Unicode的转码,与Jre中的"native2ascii"命令一样
  25. * @param {String} text 需要进行转码的字符串
  26. * @return {String} Unicode码
  27. */
  28. let _uniEncode = function (text) {
  29. let res = [];
  30. for (let i = 0; i < text.length; i++) {
  31. res[i] = ("00" + text.charCodeAt(i).toString(16)).slice(-4);
  32. }
  33. return "\\u" + res.join("\\u");
  34. };
  35. /**
  36. * 此方法用于将Unicode码解码为正常字符串
  37. * @param {Object} text
  38. */
  39. let _uniDecode = function (text) {
  40. text = text = text.replace(/(\\)?\\u/gi, "%u").replace('%u0025', '%25');
  41. text = unescape(text.toString().replace(/%2B/g, "+"));
  42. let matches = text.match(/(%u00([0-9A-F]{2}))/gi);
  43. if (matches) {
  44. for (let matchid = 0; matchid < matches.length; matchid++) {
  45. let code = matches[matchid].substring(1, 3);
  46. let x = Number("0x" + code);
  47. if (x >= 128) {
  48. text = text.replace(matches[matchid], code);
  49. }
  50. }
  51. }
  52. text = unescape(text.toString().replace(/%2B/g, "+"));
  53. return text;
  54. };
  55. /**
  56. * 此方法用于将文字进行UTF-8编码
  57. * @param {Object} str 源码
  58. * @return {String} UTF-8码
  59. */
  60. let _utf8Encode = function (str) {
  61. let out, i, len, c;
  62. out = "";
  63. len = str.length;
  64. for (i = 0; i < len; i++) {
  65. c = str.charCodeAt(i);
  66. if ((c >= 0x0001) && (c <= 0x007F)) {
  67. out += str.charAt(i);
  68. } else if (c > 0x07FF) {
  69. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  70. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  71. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  72. } else {
  73. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  74. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  75. }
  76. }
  77. return out;
  78. };
  79. /**
  80. * 此方法用于将文字进行UTF-8解码
  81. * @param {Object} str
  82. * @return {String} 原文字
  83. */
  84. let _utf8Decode = function (str) {
  85. let out, i, len, c;
  86. let char2, char3;
  87. out = "";
  88. len = str.length;
  89. i = 0;
  90. while (i < len) {
  91. c = str.charCodeAt(i++);
  92. switch (c >> 4) {
  93. case 0:
  94. case 1:
  95. case 2:
  96. case 3:
  97. case 4:
  98. case 5:
  99. case 6:
  100. case 7:
  101. // 0xxxxxxx
  102. out += str.charAt(i - 1);
  103. break;
  104. case 12:
  105. case 13:
  106. // 110x xxxx  10xx xxxx
  107. char2 = str.charCodeAt(i++);
  108. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  109. break;
  110. case 14:
  111. // 1110 xxxx 10xx xxxx 10xx xxxx
  112. char2 = str.charCodeAt(i++);
  113. char3 = str.charCodeAt(i++);
  114. out += String.fromCharCode(((c & 0x0F) << 12) |
  115. ((char2 & 0x3F) << 6) |
  116. ((char3 & 0x3F) << 0));
  117. break;
  118. }
  119. }
  120. return out;
  121. };
  122. /**
  123. * 此方法用于将文字进行base64编码
  124. * @param {Object} str 源码
  125. * @return {String} base64码
  126. */
  127. let _base64Encode = function (str) {
  128. let out, i, len;
  129. let c1, c2, c3;
  130. len = str.length;
  131. i = 0;
  132. out = "";
  133. while (i < len) {
  134. c1 = str.charCodeAt(i++) & 0xff;
  135. if (i == len) {
  136. out += _base64EncodeChars.charAt(c1 >> 2);
  137. out += _base64EncodeChars.charAt((c1 & 0x3) << 4);
  138. out += "==";
  139. break;
  140. }
  141. c2 = str.charCodeAt(i++);
  142. if (i == len) {
  143. out += _base64EncodeChars.charAt(c1 >> 2);
  144. out += _base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  145. out += _base64EncodeChars.charAt((c2 & 0xF) << 2);
  146. out += "=";
  147. break;
  148. }
  149. c3 = str.charCodeAt(i++);
  150. out += _base64EncodeChars.charAt(c1 >> 2);
  151. out += _base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  152. out += _base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  153. out += _base64EncodeChars.charAt(c3 & 0x3F);
  154. }
  155. return out;
  156. };
  157. /**
  158. * 此方法用于将文字进行base64解码
  159. * @param {Object} str 源码
  160. * @return {String} 源码
  161. */
  162. let _base64Decode = function (str) {
  163. // 首先进行URL解码处理,将%XX格式的字符转换回原始字符
  164. try {
  165. // 使用decodeURIComponent进行URL解码
  166. str = decodeURIComponent(str);
  167. } catch (e) {
  168. // 如果decodeURIComponent失败,尝试手动替换常见的URL编码字符
  169. str = str.replace(/%2B/g, '+')
  170. .replace(/%2F/g, '/')
  171. .replace(/%3D/g, '=')
  172. .replace(/%20/g, ' ');
  173. }
  174. let c1, c2, c3, c4;
  175. let i, len, out;
  176. len = str.length;
  177. i = 0;
  178. out = "";
  179. while (i < len) {
  180. /* c1 */
  181. do {
  182. c1 = _base64DecodeChars[str.charCodeAt(i++) & 0xff];
  183. }
  184. while (i < len && c1 == -1);
  185. if (c1 == -1)
  186. break;
  187. /* c2 */
  188. do {
  189. c2 = _base64DecodeChars[str.charCodeAt(i++) & 0xff];
  190. }
  191. while (i < len && c2 == -1);
  192. if (c2 == -1)
  193. break;
  194. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  195. /* c3 */
  196. do {
  197. c3 = str.charCodeAt(i++) & 0xff;
  198. if (c3 == 61)
  199. return out;
  200. c3 = _base64DecodeChars[c3];
  201. }
  202. while (i < len && c3 == -1);
  203. if (c3 == -1)
  204. break;
  205. out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  206. /* c4 */
  207. do {
  208. c4 = str.charCodeAt(i++) & 0xff;
  209. if (c4 == 61)
  210. return out;
  211. c4 = _base64DecodeChars[c4];
  212. }
  213. while (i < len && c4 == -1);
  214. if (c4 == -1)
  215. break;
  216. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  217. }
  218. return out;
  219. };
  220. /**
  221. * 中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位
  222. * @param str
  223. * @return {String}
  224. */
  225. let _utf16to8 = function (str) {
  226. return str.replace(/\\x/g, '%');
  227. };
  228. let _utf8to16 = function (str) {
  229. return str.replace(/%/g, '\\x');
  230. };
  231. /**
  232. * md5加密
  233. * @param str
  234. */
  235. let md5 = (str) => {
  236. return Md5Utils.md5(str);
  237. };
  238. /**
  239. * 字符串与Hex编码互转
  240. * @param input
  241. * @returns {string}
  242. */
  243. let hexTools = (function (input) {
  244. let utf8encode = function (str, isGetBytes) {
  245. let back = [];
  246. let byteSize = 0;
  247. for (let i = 0; i < str.length; i++) {
  248. let code = str.charCodeAt(i);
  249. if (0x00 <= code && code <= 0x7f) {
  250. byteSize += 1;
  251. back.push(code);
  252. } else if (0x80 <= code && code <= 0x7ff) {
  253. byteSize += 2;
  254. back.push((192 | (31 & (code >> 6))));
  255. back.push((128 | (63 & code)))
  256. } else if ((0x800 <= code && code <= 0xd7ff)
  257. || (0xe000 <= code && code <= 0xffff)) {
  258. byteSize += 3;
  259. back.push((224 | (15 & (code >> 12))));
  260. back.push((128 | (63 & (code >> 6))));
  261. back.push((128 | (63 & code)))
  262. }
  263. }
  264. for (let i = 0; i < back.length; i++) {
  265. back[i] &= 0xff;
  266. }
  267. if (isGetBytes) {
  268. return back
  269. }
  270. if (byteSize <= 0xff) {
  271. return [0, byteSize].concat(back);
  272. } else {
  273. return [byteSize >> 8, byteSize & 0xff].concat(back);
  274. }
  275. };
  276. let utf8decode = function (arr) {
  277. if (typeof arr === 'string') {
  278. return arr;
  279. }
  280. let UTF = '', _arr = arr;
  281. for (let i = 0; i < _arr.length; i++) {
  282. let one = _arr[i].toString(2),
  283. v = one.match(/^1+?(?=0)/);
  284. if (v && one.length === 8) {
  285. let bytesLength = v[0].length;
  286. let store = _arr[i].toString(2).slice(7 - bytesLength);
  287. for (let st = 1; st < bytesLength; st++) {
  288. store += _arr[st + i].toString(2).slice(2)
  289. }
  290. UTF += String.fromCharCode(parseInt(store, 2));
  291. i += bytesLength - 1
  292. } else {
  293. UTF += String.fromCharCode(_arr[i])
  294. }
  295. }
  296. return UTF
  297. };
  298. let hexEncode = function (str) {
  299. let charBuf = utf8encode(str, true);
  300. let re = '';
  301. for (let i = 0; i < charBuf.length; i++) {
  302. let x = (charBuf[i] & 0xFF).toString(16);
  303. if (x.length === 1) {
  304. x = '0' + x;
  305. }
  306. re += x;
  307. }
  308. return re;
  309. };
  310. let hexDecode = function (str) {
  311. let buf = [];
  312. for (let i = 0; i < str.length; i += 2) {
  313. buf.push(parseInt(str.substring(i, i + 2), 16));
  314. }
  315. return utf8decode(buf);
  316. };
  317. return {hexEncode, hexDecode};
  318. })();
  319. /**
  320. * html代码转换成js
  321. * @param txt
  322. * @returns {string}
  323. */
  324. let _html2js = function (txt) {
  325. let htmlArr = txt.replace(/\\/g, "\\\\").replace(/\\/g, "\\/").replace(/\'/g, "\\\'").split('\n');
  326. let len = htmlArr.length;
  327. let outArr = [];
  328. outArr.push("let htmlCodes = [\n");
  329. htmlArr.forEach((value, index) => {
  330. if (value !== "") {
  331. if (index === len - 1) {
  332. outArr.push("\'" + value + "\'");
  333. } else {
  334. outArr.push("\'" + value + "\',\n");
  335. }
  336. }
  337. });
  338. outArr.push("\n].join(\"\");");
  339. return outArr.join("");
  340. };
  341. /**
  342. * URL 参数解析
  343. * @param url
  344. * @returns {{url: *, params: Array}}
  345. * @private
  346. */
  347. let _urlParamsDecode = function (url) {
  348. let res = {};
  349. try {
  350. let params = [];
  351. let urlObj = new URL(url);
  352. for (let item of urlObj.searchParams) {
  353. params.push(item);
  354. }
  355. res = {
  356. url: urlObj.href,
  357. params: params,
  358. protocol: urlObj.protocol,
  359. pathname: urlObj.pathname,
  360. hostname: urlObj.hostname
  361. }
  362. } catch (e) {
  363. res.error = '这不是一个合法的URL!无法完成解析!'
  364. }
  365. return res;
  366. };
  367. // sha1加密
  368. let _sha1Encode = function (str) {
  369. function encodeUTF8(s) {
  370. let i, r = [], c, x;
  371. for (i = 0; i < s.length; i++)
  372. if ((c = s.charCodeAt(i)) < 0x80) r.push(c);
  373. else if (c < 0x800) r.push(0xC0 + (c >> 6 & 0x1F), 0x80 + (c & 0x3F));
  374. else {
  375. if ((x = c ^ 0xD800) >> 10 == 0)
  376. c = (x << 10) + (s.charCodeAt(++i) ^ 0xDC00) + 0x10000,
  377. r.push(0xF0 + (c >> 18 & 0x7), 0x80 + (c >> 12 & 0x3F));
  378. else r.push(0xE0 + (c >> 12 & 0xF));
  379. r.push(0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
  380. }
  381. return r;
  382. }
  383. var data = new Uint8Array(encodeUTF8(str))
  384. var i, j, t;
  385. var l = ((data.length + 8) >>> 6 << 4) + 16, s = new Uint8Array(l << 2);
  386. s.set(new Uint8Array(data.buffer)), s = new Uint32Array(s.buffer);
  387. for (t = new DataView(s.buffer), i = 0; i < l; i++)s[i] = t.getUint32(i << 2);
  388. s[data.length >> 2] |= 0x80 << (24 - (data.length & 3) * 8);
  389. s[l - 1] = data.length << 3;
  390. var w = [], f = [
  391. function () { return m[1] & m[2] | ~m[1] & m[3]; },
  392. function () { return m[1] ^ m[2] ^ m[3]; },
  393. function () { return m[1] & m[2] | m[1] & m[3] | m[2] & m[3]; },
  394. function () { return m[1] ^ m[2] ^ m[3]; }
  395. ], rol = function (n, c) { return n << c | n >>> (32 - c); },
  396. k = [1518500249, 1859775393, -1894007588, -899497514],
  397. m = [1732584193, -271733879, null, null, -1009589776];
  398. m[2] = ~m[0], m[3] = ~m[1];
  399. for (i = 0; i < s.length; i += 16) {
  400. var o = m.slice(0);
  401. for (j = 0; j < 80; j++)
  402. w[j] = j < 16 ? s[i + j] : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1),
  403. t = rol(m[0], 5) + f[j / 20 | 0]() + m[4] + w[j] + k[j / 20 | 0] | 0,
  404. m[1] = rol(m[1], 30), m.pop(), m.unshift(t);
  405. for (j = 0; j < 5; j++)m[j] = m[j] + o[j] | 0;
  406. };
  407. t = new DataView(new Uint32Array(m).buffer);
  408. for (var i = 0; i < 5; i++)m[i] = t.getUint32(i << 2);
  409. var hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer), function (e) {
  410. return (e < 16 ? "0" : "") + e.toString(16);
  411. }).join("");
  412. return hex;
  413. };
  414. // 来自网友的贡献,做jwt解码
  415. let jwtDecode = (() => {
  416. class InvalidTokenError extends Error {
  417. }
  418. InvalidTokenError.prototype.name = "InvalidTokenError";
  419. function b64DecodeUnicode(str) {
  420. return decodeURIComponent(atob(str).replace(/(.)/g, (m, p) => {
  421. let code = p.charCodeAt(0).toString(16).toUpperCase();
  422. if (code.length < 2) {
  423. code = "0" + code;
  424. }
  425. return "%" + code;
  426. }));
  427. }
  428. function base64UrlDecode(str) {
  429. let output = str.replace(/-/g, "+").replace(/_/g, "/");
  430. switch (output.length % 4) {
  431. case 0:
  432. break;
  433. case 2:
  434. output += "==";
  435. break;
  436. case 3:
  437. output += "=";
  438. break;
  439. default:
  440. throw new Error("base64 string is not of the correct length");
  441. }
  442. try {
  443. return b64DecodeUnicode(output);
  444. }
  445. catch (err) {
  446. return atob(output);
  447. }
  448. }
  449. return function(token) {
  450. if (typeof token !== "string") {
  451. throw new InvalidTokenError("Invalid token specified: must be a string");
  452. }
  453. const parts = token.split(".");
  454. if (parts.length !== 3) {
  455. throw new InvalidTokenError("Invalid token specified: must be three parts");
  456. }
  457. for(let i = 0; i < parts.length; i++){
  458. if (typeof parts[i] !== "string" || parts[i].length === 0) {
  459. throw new InvalidTokenError(`Invalid token specified: missing part #${i + 1}`);
  460. }
  461. }
  462. return {
  463. header: base64UrlDecode(parts[0]),
  464. payload: base64UrlDecode(parts[1]),
  465. sign: parts[2]
  466. }
  467. }
  468. })();
  469. /**
  470. * 将cookie字符串格式化为JSON对象
  471. * @param {string} cookieString - 原始的cookie字符串
  472. * @returns {string} 格式化后的JSON字符串
  473. */
  474. let formatCookieStringToJson = (cookieString) => {
  475. // 将原始cookie字符串分割成各个键值对,并解析为JSON对象
  476. const cookiesArray = cookieString.split(';').map(pair => {
  477. const [key, value] = pair.trim().split('=');
  478. let obj = {},dk , vk ;
  479. try {
  480. dk = decodeURIComponent(key);
  481. vk = decodeURIComponent(value);
  482. } catch (error) {
  483. dk = key;
  484. vk = value;
  485. }
  486. obj[dk] = vk;
  487. return obj;
  488. }).reduce((accumulator, current) => {
  489. // 合并所有键值对到一个对象中
  490. const key = Object.keys(current)[0];
  491. accumulator[key] = current[key];
  492. return accumulator;
  493. }, {});
  494. // 返回格式化的JSON
  495. return cookiesArray;
  496. }
  497. /**
  498. * 使用gzip压缩文本(返回Base64编码的结果)
  499. * @param {string} text - 需要压缩的文本
  500. * @returns {Promise<string>} 压缩后的Base64字符串
  501. */
  502. let _gzipEncode = async function(text) {
  503. try {
  504. // 检查浏览器是否支持CompressionStream API
  505. if (typeof CompressionStream === 'undefined') {
  506. throw new Error('当前浏览器不支持CompressionStream API,请使用Chrome 80+、Firefox 113+或Safari 16.4+');
  507. }
  508. // 将文本转换为Uint8Array
  509. const encoder = new TextEncoder();
  510. const data = encoder.encode(text);
  511. // 创建压缩流
  512. const compressionStream = new CompressionStream('gzip');
  513. const compressedStream = new Response(data).body.pipeThrough(compressionStream);
  514. // 读取压缩后的数据
  515. const compressedArrayBuffer = await new Response(compressedStream).arrayBuffer();
  516. // 将ArrayBuffer转换为Base64字符串
  517. const compressedArray = new Uint8Array(compressedArrayBuffer);
  518. let binaryString = '';
  519. for (let i = 0; i < compressedArray.length; i++) {
  520. binaryString += String.fromCharCode(compressedArray[i]);
  521. }
  522. return btoa(binaryString);
  523. } catch (error) {
  524. throw new Error('Gzip压缩失败: ' + error.message);
  525. }
  526. };
  527. /**
  528. * 使用gzip解压缩Base64编码的数据
  529. * @param {string} compressedBase64 - Base64编码的压缩数据
  530. * @returns {Promise<string>} 解压缩后的文本
  531. */
  532. let _gzipDecode = async function(compressedBase64) {
  533. try {
  534. // 检查浏览器是否支持DecompressionStream API
  535. if (typeof DecompressionStream === 'undefined') {
  536. throw new Error('当前浏览器不支持DecompressionStream API,请使用Chrome 80+、Firefox 113+或Safari 16.4+');
  537. }
  538. // 将Base64字符串转换为Uint8Array
  539. const binaryString = atob(compressedBase64);
  540. const compressedArray = new Uint8Array(binaryString.length);
  541. for (let i = 0; i < binaryString.length; i++) {
  542. compressedArray[i] = binaryString.charCodeAt(i);
  543. }
  544. // 创建解压缩流
  545. const decompressionStream = new DecompressionStream('gzip');
  546. const decompressedStream = new Response(compressedArray).body.pipeThrough(decompressionStream);
  547. // 读取解压缩后的数据
  548. const decompressedArrayBuffer = await new Response(decompressedStream).arrayBuffer();
  549. // 将ArrayBuffer转换为文本
  550. const decoder = new TextDecoder();
  551. return decoder.decode(decompressedArrayBuffer);
  552. } catch (error) {
  553. throw new Error('Gzip解压缩失败: ' + error.message);
  554. }
  555. };
  556. let _stringEscape = function(text) {
  557. return text
  558. .replace(/\\/g, '\\\\')
  559. .replace(/\n/g, '\\n')
  560. .replace(/\r/g, '\\r')
  561. .replace(/\t/g, '\\t')
  562. .replace(/\f/g, '\\f')
  563. .replace(/"/g, '\\"')
  564. .replace(/'/g, "\\'");
  565. };
  566. let _stringUnescape = function(text) {
  567. return text.replace(/\\(n|r|t|f|\\|"|'|u[0-9a-fA-F]{4})/g, function(match, p1) {
  568. switch(p1) {
  569. case 'n': return '\n';
  570. case 'r': return '\r';
  571. case 't': return '\t';
  572. case 'f': return '\f';
  573. case '\\': return '\\';
  574. case '"': return '"';
  575. case "'": return "'";
  576. default:
  577. if (p1.startsWith('u')) {
  578. return String.fromCharCode(parseInt(p1.substring(1), 16));
  579. }
  580. return match;
  581. }
  582. });
  583. };
  584. return {
  585. uniEncode: _uniEncode,
  586. uniDecode: _uniDecode,
  587. base64Encode: _base64Encode,
  588. base64Decode: _base64Decode,
  589. utf8Encode: _utf8Encode,
  590. utf8Decode: _utf8Decode,
  591. utf16to8: _utf16to8,
  592. utf8to16: _utf8to16,
  593. md5: md5,
  594. hexEncode: hexTools.hexEncode,
  595. hexDecode: hexTools.hexDecode,
  596. html2js: _html2js,
  597. urlParamsDecode: _urlParamsDecode,
  598. sha1Encode: _sha1Encode,
  599. jwtDecode,
  600. formatCookieStringToJson,
  601. gzipEncode: _gzipEncode,
  602. gzipDecode: _gzipDecode,
  603. stringEscape: _stringEscape,
  604. stringUnescape: _stringUnescape
  605. };
  606. })();
  607. export default EncodeUtils;