<commit_before>import forEach from './forEach'

export default function sendRequest(params, cb) {
  return new Promise(function(resolve, reject) {
    var method = (params.method || 'GET').toUpperCase()
    var url = params.url
    if (['GET', 'POST', 'PUT', 'DELETE'].indexOf(method) < 0) {
      throw new Error("Parameter 'method' must be 'GET', 'POST', 'PUT', or 'DELETE'.")
    }
    if (!url) {
      throw new Error("Parameter 'url' is required.")
    }
    var xmlhttp = new XMLHttpRequest()
    xmlhttp.onreadystatechange = function() {
      // TODO: we could support more states here to give feedback
      // e.g. about progress of an upload
      if (xmlhttp.readyState === 4) return _done()
    }
    xmlhttp.open(method, url, true)
    if (params.header) {
      forEach(params.header, function(val, key) {
        xmlhttp.setRequestHeader(key, val)
      })
    }
    if (params.data) {
      xmlhttp.send(JSON.stringify(params.data))
    } else {
      xmlhttp.send()
    }

    function _done() {
      if (xmlhttp.status === 200) {
        var response = xmlhttp.responseText
        if (cb) cb(null, response)
        resolve(response)
      } else {
        console.error(xmlhttp.statusText)
        if (cb) cb(xmlhttp.status)
        reject(xmlhttp.statusText, xmlhttp.status)
      }
    }
  })
}

<commit_msg>Add option for sendRequest helper to send form data.<commit_after>import forEach from './forEach'

export default function sendRequest(params, cb) {
  return new Promise(function(resolve, reject) {
    var method = (params.method || 'GET').toUpperCase()
    var url = params.url
    if (['GET', 'POST', 'PUT', 'DELETE'].indexOf(method) < 0) {
      throw new Error("Parameter 'method' must be 'GET', 'POST', 'PUT', or 'DELETE'.")
    }
    if (!url) {
      throw new Error("Parameter 'url' is required.")
    }
    var xmlhttp = new XMLHttpRequest()
    xmlhttp.onreadystatechange = function() {
      // TODO: we could support more states here to give feedback
      // e.g. about progress of an upload
      if (xmlhttp.readyState === 4) return _done()
    }
    xmlhttp.open(method, url, true)
    if (params.header) {
      forEach(params.header, function(val, key) {
        xmlhttp.setRequestHeader(key, val)
      })
    }
    if (params.data instanceof FormData) {
      xmlhttp.send(params.data)
    } else if (params.data) {
      xmlhttp.send(JSON.stringify(params.data))
    } else {
      xmlhttp.send()
    }

    function _done() {
      if (xmlhttp.status === 200) {
        var response = xmlhttp.responseText
        if (cb) cb(null, response)
        resolve(response)
      } else {
        console.error(xmlhttp.statusText)
        if (cb) cb(xmlhttp.status)
        reject(xmlhttp.statusText, xmlhttp.status)
      }
    }
  })
}


{'fraction_non_alphanumeric': 0.1356297093649085, 'fraction_numerical': 0.003588087549336204, 'mean_word_length': 2.551592356687898, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 27}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7445108', 'n_tokens_mistral': 873, 'n_tokens_neox': 855, 'n_words': 228}