<commit_before>export default function({
  userModel,
  map: []} = {}) {

  return function(req, res) {
  }

}<commit_msg>feat: add `state.res` implementation<commit_after>import _ from 'lodash'
import assert from 'assert'
import Promise from 'bluebird'
import util from 'util'

function models(map) {
  const base = {}
  const state = {}
  map.forEach(mapping => {
    base[_.kebabCase(mapping.base.name)] = {
      model: mapping.state,
      baseField: mapping.baseField,
      userField: mapping.userField
    }
    state[_.kebabCase(mapping.base.name)] = mapping.base
  })

  return Object.freeze({
    base,
    state
  })
}

function typeMap(user, documents, table) {
  const types = {}
  if (!_.isArray(documents) && _.isObject(documents)) {
    documents = [documents]
  }

  if (!_.isArray(documents)) {
    throw new Error('Wrong type "documents".')
  }

  _.forEach(documents, document => {
    const type = _.get(table.base, document.type, false)
    if (!type) {
      return
    }

    if (!_.isObject(types[document.type])) {
      types[document.type] = {}
    }

    if (!_.has(types[document.type], document.id)) {
      if (!_.has(document, 'attributes')) {
        document.attributes = {}
      }

      types[document.type][document.id] = {
        model: table.base[document.type].model,
        original: document.attributes,
        query: {
          [table.base[document.type].baseField]: document.id,
          [table.base[document.type].userField]: user.id
        }
      }
    }
  })

  return types
}

function req({
  userParam = 'user',
  map = []} = {}) {

  assert.ok(userParam)

  const table = models(map)

  return function(req, res) {
  }

}

function res({
  userParam = 'user',
  map = []} = {}) {

  assert.ok(userParam)

  const table = models(map)

  return function(req, res) {
    const map = typeMap(req.user, res.body.data, table)
    const list = []
    _.forEach(map, type => _.forEach(type, id => {
      const p = id.model
        .findOne({where: id.query})
        .then(document => {
          if (!document) {
            return
          }
          const state = _.omit(document.toJSON(), _.keys(id.query))
          _.merge(id.original, state)
        })
      list.push(p)
    }))
    return Promise.all(list)
  }

}

export default Object.freeze({
  req,
  res
})
{'fraction_non_alphanumeric': 0.15563198624247634, 'fraction_numerical': 0.0, 'mean_word_length': 2.4835329341317367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '1591215', 'n_tokens_mistral': 775, 'n_tokens_neox': 754, 'n_words': 154}