'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var isStr = _interopDefault(require('./isStr'));

/**
 * 获取url中的查询参数
 * @param {*} url
 * @returns
 */
function parseQueryString(url) {
  if (!isStr(url)) url = '';
  var params = {};

  if (url.indexOf('=') > -1) {
    var queryStr = url.indexOf('?') > -1 ? url.substring(url.lastIndexOf('?') + 1) : url;

    if (queryStr) {
      var querys = queryStr.split('&');

      for (var i = 0; i < querys.length; i++) {
        var keyVal = querys[i].split('=');
        var key = keyVal[0];
        var val = keyVal[1];
        var decode = decodeURIComponent;
        if (key) params[decode(key)] = decode(val || '');
      }
    }
  }

  return params;
}

module.exports = parseQueryString;
