<commit_before>import assign from 'object-assign'
import Promise from 'bluebird'

const debug = require('debug')('wololobot:florins')

export default function (opts) {
  opts = assign({
    delay: 4000
  }, opts)

  const { db } = opts
  function florinsOf(user) {
    return db('transactions')
      .sum('amount as florins')
      .where('lower(username)', '=', user.toLowerCase())
  }
  function florinsOfMany(users) {
    return db('transactions')
      .select(db.raw('lower(username) as lname')).sum('amount as wallet')
      .whereIn('lname', users.map(u => u.toLowerCase()))
      .groupBy('lname')
      .reduce((o, t) => assign(o, { [t.lname]: t.wallet }), {})
  }
  function transaction(username, amount, description = '') {
    return db('transactions').insert({ username: username.toLowerCase()
                                     , amount: amount
                                     , description: description })
  }
  function transactions(list) {
    return Promise.all(list.map(t => transaction(t.username, t.amount, t.description)))
  }

  return function (bot) {
    let florinsTimeout = null
    let florinsChecks = []
    function respondFlorins() {
      florinsOfMany(florinsChecks)
        .then(o => Object.keys(o)
                         .map(name => `${name} - ${o[name]}`)
                         .join(', '))
        .tap(bot.send.bind(bot))
      florinsChecks = []
      florinsTimeout = null
    }

    assign(bot, { florinsOf, florinsOfMany, transaction, transactions })
<commit_msg>keep username capitalisation in !florins response<commit_after>import assign from 'object-assign'
import Promise from 'bluebird'

const debug = require('debug')('wololobot:florins')

export default function (opts) {
  opts = assign({
    delay: 4000
  }, opts)

  const { db } = opts
  function florinsOf(user) {
    return db('transactions')
      .sum('amount as florins')
      .where('lower(username)', '=', user.toLowerCase())
  }
  function florinsOfMany(users) {
    let lusers = users.map(u => u.toLowerCase())
    let nameMap = users.reduce((map, user, i) => assign(map, { [lusers[i]]: user }), {})
    return db('transactions')
      .select(db.raw('lower(username) as lname')).sum('amount as wallet')
      .whereIn('lname', lusers)
      .groupBy('lname')
      .reduce((o, t) => assign(o, { [nameMap[t.lname] || t.lname]: t.wallet }), {})
  }
  function transaction(username, amount, description = '') {
    return db('transactions').insert({ username: username.toLowerCase()
                                     , amount: amount
                                     , description: description })
  }
  function transactions(list) {
    return Promise.all(list.map(t => transaction(t.username, t.amount, t.description)))
  }

  return function (bot) {
    let florinsTimeout = null
    let florinsChecks = []
    function respondFlorins() {
      florinsOfMany(florinsChecks)
        .then(o => Object.keys(o)
                         .map(name => `${name} - ${o[name]}`)
                         .join(', '))
        .tap(bot.send.bind(bot))
      florinsChecks = []
      florinsTimeout = null
    }

    assign(bot, { florinsOf, florinsOfMany, transaction, transactions })

{'fraction_non_alphanumeric': 0.1520722966656279, 'fraction_numerical': 0.0024929884699283265, 'mean_word_length': 2.5946248600223965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4974496', 'n_tokens_mistral': 994, 'n_tokens_neox': 977, 'n_words': 207}