<commit_before>var readyStates = {
  UNSENT           : 0, // open()has not been called yet.
  OPENED           : 1, // send()has not been called yet.
  HEADERS_RECEIVED : 2, // send() has been called, and headers and status are available.
  LOADING          : 3, // Downloading; responseText holds partial data.
  DONE             : 4  // The operation is complete.
};

function Ajax(options) {
  if (typeof(options) === "string") {
    options = {url: options};
  }

  var deferred = Promise.defer();
  var request  = new XMLHttpRequest();
  var url      = options.url;
  var method   = options.method  || "GET";
  var data     = options.data    || null;
  var headers  = options.headers || {};
  var async    = Ajax.async;

  if (!url) {
    throw new TypeError("Must provide a URL");
  }

  if (options.hasOwnProperty("withCredentials")) {
    request.withCredentials = options.withCredentials;
  }

  if (options.hasOwnProperty("timeout")) {
    request.timeout = options.timeout;
  }

  request.onreadystatechange = StateChanged(request, options, deferred);
  request.open(method, url, async, options.user, options.password);

  // Make sure to add all the headers.
  addHeader(request, Ajax.headers);
  addHeader(request, headers);

  request.send(data);
  return deferred.promise;
}

function StateChanged(request, options, deferred) {
  return function StateChangedDelegate() {
    var state = request.readyState;

    // If there is a state change handler, call it with the request object and options.
    if (options.stateChange) {
      options.stateChange(request, options);
    }

    if (state === readyStates.DONE) {
      if (request.status >= 100 && request.status < 300) {
        // Handle response transformation.
        var result = (options.transform || transform)(request.responseText, request.getResponseHeader("Content-Type"));

        // Call global success handler
        Ajax.success(result, request);

        if (options.success) {
          options.success(result, request);
        }

        // Resolve deferred Promise
        deferred.resolve(result);
      }
      else {
        // Call global error handler
        Ajax.error(request);

        if (options.reject) {
          options.reject(request);
        }

        // Reject deferred Promise
        deferred.reject(request);
      }
    }
  };
}

function transform(text, type) {
  var transformHandler = Ajax.transforms[type];
  return transformHandler ? transformHandler(text) : text;
}

function addHeader(request, headers) {
  for (var header in headers) {
    if (headers.hasOwnProperty(header)) {
      request.setRequestHeader(header, headers[header]);
    }
  }
}

// Setup global async to true. ONLY for debugging.<commit_msg>changes so that `error` is now called. refactored to use Promise constructor<commit_after>var readyStates = {
  UNSENT           : 0, // open()has not been called yet.
  OPENED           : 1, // send()has not been called yet.
  HEADERS_RECEIVED : 2, // send() has been called, and headers and status are available.
  LOADING          : 3, // Downloading; responseText holds partial data.
  DONE             : 4  // The operation is complete.
};

function Ajax(options) {
  if (typeof(options) === "string") {
    options = {url: options};
  }

  var request  = new XMLHttpRequest();
  var url      = options.url;
  var method   = options.method  || "GET";
  var data     = options.data    || null;
  var headers  = options.headers || {};
  var async    = Ajax.async;

  if (!url) {
    throw new TypeError("Must provide a URL");
  }

  if (options.hasOwnProperty("withCredentials")) {
    request.withCredentials = options.withCredentials;
  }

  if (options.hasOwnProperty("timeout")) {
    request.timeout = options.timeout;
  }

  return new Promise(function(resolve, reject) {
    function StateChangedDelegate() {
      var state = request.readyState;

      // If there is a state change handler, call it with the request object and options.
      if (options.stateChange) {
        options.stateChange(request, options);
      }

      if (state === readyStates.DONE) {
        if (request.status >= 100 && request.status < 300) {
          // Handle response transformation.
          var result = (options.transform || transform)(request.responseText, request.getResponseHeader("Content-Type"));

          // Call global success handler
          Ajax.success(result, request);

          if (options.success) {
            options.success(result, request);
          }

          // Resolve deferred Promise
          resolve(result);
        }
        else {
          // Call global error handler
          Ajax.error(request);

          if (options.error) {
            options.error(request);
          }

          // Reject deferred Promise
          reject(request);
        }
      }
    }

    request.onreadystatechange = StateChangedDelegate;
    request.open(method, url, async, options.user, options.password);

    // Make sure to add all the headers.
    addHeader(request, Ajax.headers);
    addHeader(request, headers);

    request.send(data);
  });
}

function transform(text, type) {
  var transformHandler = Ajax.transforms[type];
  return transformHandler ? transformHandler(text) : text;
}

function addHeader(request, headers) {
  for (var header in headers) {
    if (headers.hasOwnProperty(header)) {
      request.setRequestHeader(header, headers[header]);
    }
  }
}

// Setup global async to true. ONLY for debugging.
{'fraction_non_alphanumeric': 0.11450937556890588, 'fraction_numerical': 0.004005097396686692, 'mean_word_length': 2.5768229166666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3541714', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1507, 'n_words': 430}