/*jshint -W032 */
;
/*jshint +W032 */
(function(root, factory) {
  if (typeof define === 'function') {
    define(factory);
  } else if (typeof exports === 'object') {
    module.exports = factory();
  } else {
    root.ajaxer = factory();
  }
}(this, function() {
  var
    prepareData = function(data) {
      if (!data || typeof data !== "object") {
        return;
      }
      return Object.keys(data).map(function(k) {
        return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
      }).join("&");
    },
    setCallback = function(fn, request) {
      if (typeof fn !== "function") {
        return;
      }
      request.onreadystatechange = (function() {
        if (request.readyState === 4 && request.status === 200) {
          fn(request.responseText);
        }
      }).bind(request);
    };

  return {
    post: function(url, data, callback) {
      this.connect("POST", url, data, callback);
    },

    get: function(url, data, callback) {
      this.connect("GET", url, data, callback);
    },

    connect: function(style, url, data, callback) {
      if (style !== 'POST' && style !== 'GET') {
        return;
      }
      if (typeof data === "function" && typeof callback === "undefined") {
        callback = data;
        data = null;
      }

      var request = new XMLHttpRequest(),
        sendData = prepareData(data);

      if (style === "GET") {
        var urlParams = "?" + sendData;
        url += urlParams;
        sendData = "";
      }

      setCallback(callback, request);

      request.open(style, url, true);

      if (style === "POST") {
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      }
      request.send(sendData);
    }
  };
}));

/*jshint -W032 */
;
/*jshint +W032 */
/* istanbul ignore next */
(function(root, factory) {
  if (typeof define === 'function') {
    define(factory);
  } else if (typeof exports === 'object') {
    module.exports = factory();
  } else {
    root.ajaxer = factory();
  }
}(this, function() {
  var
    prepareData = function(data) {
      if (!data || typeof data !== "object") {
        return;
      }
      return Object.keys(data).map(function(k) {
        return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
      }).join("&");
    },
    setCallback = function(fn, request) {
      if (typeof fn !== "function") {
        return;
      }
      request.onreadystatechange = (function() {
        if (request.readyState === 4 && request.status === 200) {
          fn(request.responseText);
        }
      }).bind(request);
    };

  return {
    post: function(url, data, callback) {
      this.connect("POST", url, data, callback);
    },

    get: function(url, data, callback) {
      this.connect("GET", url, data, callback);
    },

    connect: function(style, url, data, callback) {
      if (style !== 'POST' && style !== 'GET') {
        return;
      }
      if (typeof data === "function" && typeof callback === "undefined") {
        callback = data;
        data = null;
      }

      var request = new XMLHttpRequest(),
        sendData = prepareData(data);

      if (style === "GET") {
        var urlParams = "?" + sendData;
        url += urlParams;
        sendData = "";
      }

      setCallback(callback, request);

      request.open(style, url, true);

      if (style === "POST") {
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      }
      request.send(sendData);
    }
  };
}));

add istanbul ignore