From 1d85893eb9a5afba78589ea087f126828be60334 Mon Sep 17 00:00:00 2001 From: pearone <1181019452@qq.com> Date: Sun, 17 Dec 2023 10:03:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81url=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=8A=A0=E8=A7=A3=E5=AF=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/event/__tests__/url.test.ts | 63 +++++++++++++++++++++++++++- packages/event/src/url.ts | 39 ----------------- 2 files changed, 61 insertions(+), 41 deletions(-) delete mode 100644 packages/event/src/url.ts diff --git a/packages/event/__tests__/url.test.ts b/packages/event/__tests__/url.test.ts index 6ad2c215..3ef5d278 100644 --- a/packages/event/__tests__/url.test.ts +++ b/packages/event/__tests__/url.test.ts @@ -1,5 +1,10 @@ -import { encodeParamsURI, decodeParamsURI } from '../src/url'; - +import { + encodeParamsURI, + decodeParamsURI, + encodeHashURI, + decodeHashURI +} from '../src/url'; +/** TEST encodeParamsURI、decodeParamsURI */ test('default url', () => { const url = encodeParamsURI('https://wuba.xinghuo.58.com', { header: 'aaa', @@ -58,3 +63,57 @@ test('object params url', () => { const params = decodeParamsURI(url); console.log(params, url); }); + +test('object params anchor url', () => { + const url = + encodeParamsURI('https://wuba.xinghuo.58.com?name=xxx', { + header: '请选择用户或用户组我是一个很长的中文字符中文', + user: { + name: 'bbb', + age: 18, + sex: 'male' + }, + target: 'ccc' + }) + '#anchor=111'; + + const params = decodeParamsURI(url); + console.log(params, url); +}); + +/** TEST encodeHashURI、decodeHashURI */ +test('default hash url', () => { + const url = encodeHashURI('https://wuba.xinghuo.58.com#hash=111', { + header: 'aaa', + user: 'bbb', + target: 'ccc' + }); + + const hash = decodeHashURI(url); + console.log(hash, url); +}); + +/** TEST prototype encodeParamsURI、decodeParamsURI */ +test('default url prototype url', () => { + const url = new URL('https://wuba.xinghuo.58.com#hash=111'); + const encode_url = url.encodeParamsURI({ + header: 'aaa', + user: 'bbb', + target: 'ccc' + }); + + const params = new URL(encode_url).decodeParamsURI(); + console.log(params, url); +}); + +/** TEST prototype encodeHashURI、decodeHashURI */ +test('default url prototype url', () => { + const url = new URL('https://wuba.xinghuo.58.com#hash=111'); + const encode_url = url.encodeHashURI({ + header: 'aaa', + user: 'bbb', + target: 'ccc' + }); + + const params = new URL(encode_url).decodeHashURI(); + console.log(params, url); +}); diff --git a/packages/event/src/url.ts b/packages/event/src/url.ts deleted file mode 100644 index 82be03eb..00000000 --- a/packages/event/src/url.ts +++ /dev/null @@ -1,39 +0,0 @@ -/** 加密URL上属性 */ -const encodeParamsURI = (uri: string, params: Record) => { - Object.keys(params).forEach((p) => { - const append = `${p}=${window.btoa( - window.encodeURIComponent(JSON.stringify(params[p])) - )}`; - - if (uri.indexOf('?') > -1) { - uri += '&'; - } else { - uri += '?'; - } - uri += append; - }); - - return uri; -}; - -/** 解密URL上属性 */ -const decodeParamsURI = (uri: string) => { - const default_params = new URL(uri).search.replace('?', '').split('&'); - const target_params: Record = {}; - default_params.forEach((p) => { - const [key, value] = p.split('='); - if (value) { - try { - const decode = JSON.parse( - window.decodeURIComponent(window.atob(value)) - ); - target_params[key] = decode; - } catch (e) { - target_params[key] = value; - } - } - }); - return target_params; -}; - -export { encodeParamsURI, decodeParamsURI };