<commit_before>import { each, has, pick, defaults, mapKeys, mapValues, pickBy } from 'lodash'
import { cookie } from 'cookie_js'
import decorator from './_decorator'

const numberOrNull = numeric => {
  const num = parseInt(numeric, 10)
  return isNaN(num) ? null : num
}

const dropInvalid = value => !!value

const normalizer = {
  id: numberOrNull,
  weight: numberOrNull,
  content: numberOrNull
}

const filter = {
  id: dropInvalid,
  content: dropInvalid
}

const imageDefaults = data => {
  return pickBy(
      mapValues(
        mapKeys(
          defaults(data, {
            weight: 0,
            csrfmiddlewaretoken: cookie.get('csrftoken')
          }),
          (value, key) => ({ contentId: 'content' }[key] || key)
        ),
        (value, key) => has(normalizer, key) ? normalizer[key](value) : value
      ),
      (value, key) => has(filter, key) ? filter[key](value) : true
    )
}

const transform = file => ({
  id: file.id,
  label: file.title,
  content: file.path
})

export default decorator((req, opts) => {
  const endpoint = opts.url('images/')

  const get = (params = {}) => {
    return req.get(endpoint, {params})
      .then(res => Promise.resolve(res.data.map(transform)))
  }

  const create = (data = {}) => {
    const formData = new window.FormData()
    each(imageDefaults(data), (value, key) => formData.append(key, value))

    return req.post(endpoint, formData)
      .then(res => Promise.resolve(transform(res.data)))
  }

  const update = (data = {}) => {
    const putData = imageDefaults(pick(data, ['id', 'weight']))
    return req.put(endpoint, putData)
      .then(res => Promise.resolve(res.data))
  }

  return {get, create, update}
})
<commit_msg>add progress callback to image api adapter<commit_after>import { each, has, pick, defaults, mapKeys, mapValues, pickBy } from 'lodash'
import { cookie } from 'cookie_js'
import decorator from './_decorator'

const numberOrNull = numeric => {
  const num = parseInt(numeric, 10)
  return isNaN(num) ? null : num
}

const dropInvalid = value => !!value

const normalizer = {
  id: numberOrNull,
  weight: numberOrNull,
  content: numberOrNull
}

const filter = {
  id: dropInvalid,
  content: dropInvalid
}

const imageDefaults = data => {
  return pickBy(
      mapValues(
        mapKeys(
          defaults(data, {
            weight: 0,
            csrfmiddlewaretoken: cookie.get('csrftoken')
          }),
          (value, key) => ({ contentId: 'content' }[key] || key)
        ),
        (value, key) => has(normalizer, key) ? normalizer[key](value) : value
      ),
      (value, key) => has(filter, key) ? filter[key](value) : true
    )
}

const transform = file => ({
  id: file.id,
  label: file.title,
  content: file.path
})

export default decorator((req, opts) => {
  const endpoint = opts.url('images/')

  const get = (params = {}) => {
    return req.get(endpoint, {params})
      .then(res => Promise.resolve(res.data.map(transform)))
  }

  const create = (data = {}, conf = {}) => {
    const formData = new window.FormData()
    const requestConf = {}
    each(imageDefaults(data), (value, key) => formData.append(key, value))

    if (conf.onProgress) {
      requestConf.onUploadProgress = conf.onProgress
    }

    return req.post(endpoint, formData, requestConf)
      .then(res => Promise.resolve(transform(res.data)))
  }

  const update = (data = {}) => {
    const putData = imageDefaults(pick(data, ['id', 'weight']))
    return req.put(endpoint, putData)
      .then(res => Promise.resolve(res.data))
  }

  return {get, create, update}
})

{'fraction_non_alphanumeric': 0.1531153953618329, 'fraction_numerical': 0.0016764459346186086, 'mean_word_length': 3.058956916099773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6247236', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1143, 'n_words': 284}