export function toRelativePath(url, host) {
  if (host) {
    const reg = new RegExp(host.replace(/^https?:\/\//, ''));

    if (reg.test(url)) {
      const path = url.replace(/^https?:\/\//, '').replace(reg, '');
      if (path === '') return '/';
      return path;
    }
  }

  return url;
}
/**
 * 检查路由的包含关系， 不会检查查询参数
 *
 * @export
 * @param {*} href
 * @param {*} url
 * @param {boolean} [fuzzy=false] 模糊查找
 * @returns
 */

export function includeUrl(href, url, fuzzy = false) {
  if (url === href) return true;

  if (fuzzy) {
    if (href.split('?')[0].replace(/(\/$|#.*)/, '').indexOf(url.split('?')[0].replace(/(\/$|#.*)/, '')) > -1) {
      return true;
    }
  } else {
    if (url.split('?')[0].replace(/(\/$|#.*)/, '') === href.split('?')[0].replace(/(\/$|#.*)/, '')) {
      return true;
    }
  }

  return false;
}
export function getQueryParams(url) {
  const result = {};
  const param = /([^?=&]+)=([^&]+)/ig;
  let match;

  while ((match = param.exec(url)) != null) {
    result[match[1]] = match[2];
  }

  return result;
}
export function parse(url) {
  const [hostname, ...pathname] = url.toLowerCase().replace(/^:?\/\/|https?:\/\/?(.+?)/, '$1').split('/');
  const params = getQueryParams(url);
  return {
    hostname,
    pathname: '/' + pathname.join('/').replace(/(\?.*?)$/, ''),
    params
  };
}