URL query 参数处理函数

class querystrings {
  stringify(obj = {}) {
    let strArr = new Array();
    for (let i in obj) {
      strArr.push(`${i}=${encodeURIComponent(obj[i])}`);
    }
    return strArr.join("&");
  }
  parse(url) {
    url = decodeURIComponent(url);
    let UrlArr = url.split("?");
    if (UrlArr.length === 1) {
      return {};
    }
    let queryObj = new Object();
    UrlArr[1].split("&").map(value => {
      value = value.split("=");
      queryObj[value[0]] = decodeURIComponent(value[1]);
    });
    return queryObj;
  }
};

export default new querystrings();

你可能感兴趣的:(URL query 参数处理函数)