Created
May 13, 2021 07:01
-
-
Save muzi131313/bc8680ac177058727438f94393c3c033 to your computer and use it in GitHub Desktop.
remove duplicate query param in link address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function handleDuplicateLink(link) { | |
if (!link) { | |
console.warn('long link was empty'); | |
return link; | |
} | |
if (!link.includes('?')) { | |
console.warn(`link(${link}) no need handle`); | |
return link; | |
} | |
const links = link.split('?') | |
if (links.length !== 2) { | |
console.warn(`link(${link}) contains too many ?`); | |
return link; | |
} | |
const paramsString = links[1] | |
if (!paramsString.includes('&')) { | |
return link; | |
} | |
const paramsArray = paramsString.split('&'); | |
const params = {}; | |
paramsArray.forEach(param => { | |
if (!param) { | |
return | |
} | |
const paramArray = param.split('='); | |
const paramKey = paramArray[0]; | |
const paramValue = paramArray[1]; | |
params[paramKey] = paramValue; | |
}); | |
let noDuplicateParamString = ''; | |
Object.keys(params).forEach(key => { | |
const value = params[key]; | |
noDuplicateParamString += `${key}=${value}&` | |
}); | |
if (noDuplicateParamString) { | |
noDuplicateParamString = noDuplicateParamString.slice(0, -1); | |
} | |
const noDuplicateLink = [ links[0], noDuplicateParamString ].join('?'); | |
return noDuplicateLink; | |
} | |
console.dir(handleDuplicateLink('https://lanhuapp.com/web/#/item/project/stage?&pid=ff75ee49-cb93-4e4a-b412-481996a284c3&activeSectionId=&teamId=7e39cc8f-2420-473a-a873-75f863af19f0&userId=88b42a0b-4995-4851-9ae4-c225de4640b3¶m=ee82d272-0baf-4acc-8fd2-d5f64fe58a18&type=share_board&userId=27486379-db49-4c6e-bdc8-8dc44cf95e80&activeSectionId=')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment