const url = {}

url.join = (baseURL, path) => {
  baseURL = baseURL.match(/https?:\/\//) ? baseURL : window.location.protocol + '//' + baseURL
  baseURL = baseURL.endsWith('/')
    ? baseURL.substring(0, baseURL.length - 1)
    : baseURL
  path = path.startsWith('/') ? path : `/${path}`

  return `${baseURL}${path}`
}

// url追加参数
url.addParams = (url, obj, clearOldParams = true) => {
  let params = ''
  if (typeof obj === 'object') {
    for (let i in obj) {
      if (obj.hasOwnProperty(i) === true) {
        params = `${params}&${i}=${obj[i]}`
      }
    }

    url = ((clearOldParams === 'true' || clearOldParams === true) && url.includes('?')) ? url.split('?')[0] : url
    url = url.endsWith('/') || url.endsWith('?') || url.endsWith('&') ? url.substring(0, url.length - 1) : url

    if (!!url && (url.includes('&') || url.includes('?'))) {
      params = params.startsWith('&') ? params : `&${params}`
    } else {
      params = params.startsWith('&') ? `?${params.substring(1)}` : `?${params}`
    }
  }
  return `${url}${params}`
}
url.getQueryValue = function (url, key) {
  if (!key) {
    key = url
    url = window.location.href
  }
  key = key.replace(new RegExp('([.*+?^=!:\x24{}()|[\\]/\\\\])', 'g'), '\\$1')
  var reg = new RegExp('(^|&|\\?|#)' + key + '=([^&#]*)(&|\x24|#)', '')
  var match = ('' + url).match(reg)
  if (match) {
    return match[2]
  }
  return null
}

export default url
