export function joinTimestamp<T extends boolean>(
|
join: boolean,
|
restful: T,
|
): T extends true ? string : object;
|
|
export function joinTimestamp(join: boolean, restful = false): object | string {
|
if (!join) {
|
return restful ? '' : {};
|
}
|
const now = Date.now();
|
if (restful) {
|
return `?_t=${now}`;
|
}
|
return { _t: now };
|
}
|
/**
|
* Add the object as a parameter to the URL
|
* @param baseUrl url
|
* @param obj
|
* @returns {string}
|
* eg:
|
* let obj = {a: '3', b: '4'}
|
* setObjToUrlParams('www.baidu.com', obj)
|
* ==>www.baidu.com?a=3&b=4
|
*/
|
export function setObjToUrlParams(baseUrl: string, obj: any): string {
|
let parameters = '';
|
for (const key in obj) {
|
parameters += `${key}=${encodeURIComponent(obj[key])}&`;
|
}
|
parameters = parameters.replace(/&$/, '');
|
return /\?$/.test(baseUrl)
|
? baseUrl + parameters
|
: baseUrl.replace(/\/?$/, '?') + parameters;
|
}
|