
/**
 * 将url和对象拼接成完整url
 * @param {*} url 
 * @param {*} options 
 */
function convertToQuery(url = '', options = {}) {
    if (!url.includes('?')) {
        url += '?'
    }
    let query = ''
    for (let key in options) {
		if(options[key]){
			query += `&${encodeURIComponent(key)}=${encodeURIComponent(options[key])}`
		}
    }
	query = query.slice(1);
    return url + query
}

/**
 * 将完整url解析成url + options
 * @param {*} url 
 * @param {*} options 
 */
function convertToParam(longUrl = '') {
    if (!longUrl) {
        return {}
    }
    let url = ''
    let param = {}
    longUrl = longUrl.split('?')
    url = longUrl[0]
    if (longUrl[1]) {
        let query = longUrl[1].replace(/^&+|&+$/g, '')
        query.split('&').forEach((val, inx) => {
            val = val.split('=')
            if (val[0]) {
                param[val[0]] = val.length > 1 ? (val[1] ? val[1] : ''): ''
            }
        })
    }
    return { url, param }
}

export {
    convertToQuery,
    convertToParam
}