url/deleteUrlParam.jsjavascript
/**
 * 从 URL 中删除指定查询参数,并返回新 URL。原始字符串不会被修改,hash 会被保留。
 * @function deleteUrlParam
 * @memberof module:url
 * @param {string} url 要处理的 URL 字符串
 * @param {string[]} paramNames 要删除的非空参数名数组
 * @returns {string} 删除参数后的 URL 字符串
 * @throws {TypeError} 参数类型不符合要求时抛出
 */

function deleteUrlParam(url, paramNames) {
  if (typeof url !== 'string' || !Array.isArray(paramNames) || paramNames.length === 0 || paramNames.some(param => typeof param !== 'string')) {
    throw new TypeError('url must be a string and paramNames must be a non-empty string array')
  }

  const hashIndex = url.indexOf('#')
  const hash = hashIndex === -1 ? '' : url.slice(hashIndex)
  const withoutHash = hashIndex === -1 ? url : url.slice(0, hashIndex)
  const queryIndex = withoutHash.indexOf('?')
  if (queryIndex === -1) return url

  const urlSearch = new URLSearchParams(withoutHash.slice(queryIndex))
  paramNames.forEach(param => urlSearch['delete'](param))
  const baseUrl = withoutHash.slice(0, queryIndex)
  const query = urlSearch.toString()
  return `${baseUrl}${query ? `?${query}` : ''}${hash}`
}

export default deleteUrlParam