Support array parameter parsing (#4300)

This commit is contained in:
alucardxh 2024-09-02 10:55:11 +09:00 committed by GitHub
parent 617e594d8d
commit 6d2de002ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,14 +20,19 @@ export function getPopupContainer(node?: HTMLElement): HTMLElement {
* @param obj
* @returns {string}
* eg:
* let obj = {a: '3', b: '4'}
* let obj = {a: '3', b: '4', c: ['1','2']}
* setObjToUrlParams('www.baidu.com', obj)
* ==>www.baidu.com?a=3&b=4
* ==>www.baidu.com?a=3&b=4&c=1,2
*/
export function setObjToUrlParams(baseUrl: string, obj: any): string {
let parameters = '';
for (const key in obj) {
parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
const value = obj[key];
if (Array.isArray(value)) {
parameters += `${key}=${encodeURIComponent(value.join(','))}&`;
} else {
parameters += `${key}=${encodeURIComponent(value)}&`;
}
}
parameters = parameters.replace(/&$/, '');
return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;