Skip to content

Commit

Permalink
feat(url): encodeUrlParam
Browse files Browse the repository at this point in the history
  • Loading branch information
novlan1 committed May 31, 2022
1 parent 9286f87 commit c9b5f58
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/url/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* url参数变数组
* @param {*} url
* @returns {Object} search对象
*
*
* @example
```ts
const res = getQueryObj('https://igame.qq.com?name=mike&age=18&feel=cold&from=china');
Expand Down Expand Up @@ -39,7 +39,7 @@ export function getQueryObj(url) {
* @param {string} url
* @param {Object} queryObj
* @returns {string} 组装后的url
*
*
* @example
```ts
const res = composeUrlQuery('https://baidu.com', {
Expand Down Expand Up @@ -76,3 +76,20 @@ export function composeUrlQuery(url, queryObj) {
})
return pathname.substr(0, pathname.length - 1)
}

export function encodeUrlParam(obj) {
if (typeof obj === 'object') {
return encodeURIComponent(JSON.stringify(obj))
}
return encodeURIComponent(obj)
}

export function decodeUrlParam(str) {
let res = ''
try {
res = JSON.parse(decodeURIComponent(str))
} catch (err) {
console.log('decodeUrlParam.error', err)
}
return res
}

0 comments on commit c9b5f58

Please sign in to comment.