"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var url = {
  scope: 'search',
  paramReg: /^(?:([A-Za-z]+):(\/{0,3}))?([0-9.\-A-Za-z]+\.[0-9A-Za-z]+)?(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/,
  names: ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query', 'hash'],
  query: function query(name, scope) {
    if (scope === 'hash') {
      return this.queryHash(name, scope);
    }

    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var scope = scope || this.scope;
    var r = location[scope].substr(1).match(reg);

    if (r != null) {
      return decodeURIComponent(r[2]);
    }

    return null;
  },
  queryHash: function queryHash(name, scope) {
    var url = location[scope] + '';
    var regstr = '/(\\?|\\&)' + name + '=([^\\&]+)/';
    var reg = eval(regstr);
    var result = url.match(reg);

    if (result && result[2]) {
      return result[2];
    }
  },
  addParam: function addParam(url, data) {
    if (typeof data === 'undefined') {
      return;
    }

    var hashReg = /#.*$/gi;
    var hashMatch = url.match(hashReg);
    var hash = '';

    if (hashMatch) {
      hash = hashMatch[0];
    } // 去除hash值的url


    var preUrl = url.replace(hash, '');
    var searchReg = /\?(.*?)$/gi;
    var result = searchReg.exec(preUrl);
    var query, newUrl;

    if (result) {
      query = result[1].split('#')[0];
    }

    var id = !query ? '' : '&';
    return preUrl + (query && query.length > 0 ? '' : '?') + (id + $.param(data)) + hash;
  },
  delQuery: function delQuery(url, key) {
    var path = '';
    var param = '';
    var hash = '';

    if (url.indexOf('?') == -1) {
      path = url;
    } else {
      path = url.substr(0, url.indexOf('?'));
      param = url.substr(url.indexOf('?') + 1);

      if (param.indexOf('#') != -1) {
        hash = param.substr(param.indexOf('#') + 1);
        param = param.substr(0, param.indexOf('#'));
      }
    }

    var params = param.split('&');

    for (var i = params.length - 1; i >= 0; i--) {
      if (params[i].indexOf(key + '=') > -1) {
        params.splice(i, 1);
        break;
      }
    }

    return path + (params.join('&') ? '?' + params.join('&') : '') + (hash ? '#' + hash : '');
  },

  /**
     * 将URL query的字符串解析成对象
     */
  parseQuery: function parseQuery(query) {
    var ret = {};
    var qs;
    var q;

    var _qs;

    if (_typeof(query) === 'object') {
      for (var p in query) {
        if (query.hasOwnProperty(p) && query[p] !== null) {
          ret[p] = query[p];
        }
      }
    } else {
      qs = query.split('&');

      for (var i = 0; i < qs.length; i++) {
        if ((q = qs[i]) && (_qs = q.split('=')) && _qs[0]) {
          ret[_qs[0]] = _qs[1] ? decodeURIComponent(_qs[1]) : '';
        }
      }
    }

    return ret;
  },

  /**
     * 将URL解析为对象
     * @name parse
     * @return {
     *      url: '',
     *      hash: '',
     *      query: '',
     *      host: ''
     *      ...
     * }
     */
  parse: function parse(url) {
    var results = this.paramReg.exec(url);
    var names = this.names;
    var ret = {};
    var key;

    for (var i = 0, len = names.length; i < len; i += 1) {
      key = names[i];
      ret[key] = (key == 'path' ? '/' : '') + (results[i] || '');
    }

    return ret;
  },

  /**
      * 将对象解析为URL字符串
      */
  stringify: function stringify(data) {
    var ret = '';
    var names = this.names;

    for (var i = 1; i < names.length; i++) {
      var key = names[i];
      var value = data[key];

      if (typeof value !== 'undefined') {
        ret += function (key, value) {
          if (key == 'scheme') {
            return value + ':';
          }

          if (key == 'port') {
            return value ? ':' + value : '';
          }

          if (key == 'host') {
            return value;
          }

          if (key == 'query') {
            return value ? '?' + value : '';
          }

          if (key == 'hash') {
            return value ? '#' + value : '';
          }

          return value;
        }(key, value);
      }
    }

    return ret;
  }
};
module.exports = url;
//# sourceMappingURL=index.js.map
