// query 制作小工具,不需要手动拼接,直接传入 json格式数据即可
url format.ts
// FIXME: 当参数为数组或二级对象时,请按自己的需求进行修改
type Val = any;
interface Query {
[t:string]: Val
}
function urlFormat(url:string, query:Query, fun:(string, Val)=>boolean):string {
let res:Val[] = [];
if(fun) {
res = Object.keys(query).filter(key => fun(key, query[key]))
} else {
// 过滤空值,不加入拼接
res = Object.keys(query).filter(key => fun(key, query[key]))
}
const format = res.map(key => `${key}=${query[key]}`).join('&');
if(/\?/g.test(url)) url += `&${format}`;
else url += `?${format}`;
return url
}
标签:Val,format,url,res,key,query,路由
From: https://www.cnblogs.com/qoon-f/p/16975167.html