<commit_before>/* @flow */
import arrayReduce from 'fast-loops/lib/arrayReduce'
import findIndex from './findIndex'
import insertAtIndex from './insertAtIndex'
import objectReduce from 'fast-loops/lib/objectReduce'

// TODO: we can further improve this one
export default function objectSortByScore(
  obj: Object,
  getScore: Function
): Object {
  const sortedKeys = objectReduce(
    obj,
    (resultSortedKeys, value, key) => {
      const index = findIndex(resultSortedKeys, el => {
        return getScore(obj[el], el) > getScore(value, key)
      })

      if (index !== -1) {
        return insertAtIndex(resultSortedKeys, key, index)
      }

      return [...resultSortedKeys, key]
    },
    []
  )

  return arrayReduce(
    sortedKeys,
    (sortedObj, key) => {
      sortedObj[key] = obj[key]
      return sortedObj
    },
    {}
  )
}<commit_msg>cleanup<commit_after>/* @flow */
import arrayReduce from 'fast-loops/lib/arrayReduce'
import objectReduce from 'fast-loops/lib/objectReduce'
import findIndex from './findIndex'
import insertAtIndex from './insertAtIndex'

// TODO: we can further improve this one
export default function objectSortByScore(
  obj: Object,
  getScore: Function
): Object {
  const sortedKeys = objectReduce(
    obj,
    (resultSortedKeys, value, key) => {
      const index = findIndex(resultSortedKeys, el => getScore(obj[el], el) > getScore(value, key))

      if (index !== -1) {
        return insertAtIndex(resultSortedKeys, key, index)
      }

      return [...resultSortedKeys, key]
    },
    []
  )

  return arrayReduce(
    sortedKeys,
    (sortedObj, key) => {
      sortedObj[key] = obj[key]
      return sortedObj
    },
    {}
  )
}
{'fraction_non_alphanumeric': 0.12300413956238912, 'fraction_numerical': 0.0011827321111768185, 'mean_word_length': 3.047846889952153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3944500', 'n_tokens_mistral': 542, 'n_tokens_neox': 534, 'n_words': 137}