<commit_before>// @flow
import { memoize } from 'lodash'
import coerceSelection from './coerceSelection'

const resolver = (select?: KeysT, defaultStyle?: Object) =>
  [coerceSelection(select), JSON.stringify(defaultStyle)].join()

export default substyle => memoize(substyle, resolver)<commit_msg>better and simpler memoize<commit_after>// @flow
import coerceSelection from './coerceSelection'
import { SubstyleT } from './types'

const calculateHash = (select?: KeysT, defaultStyle?: Object) =>
  [coerceSelection(select), JSON.stringify(defaultStyle)].join()

// based on code by @philogb and @addyosmani (released under an MIT license)
// https://addyosmani.com/blog/faster-javascript-memoization/
const memoize = (substyle: SubstyleT) => (...args) => {
  substyle.memoize = substyle.memoize || {}
  const hash = calculateHash(...args)
  return hash in substyle.memoize
    ? substyle.memoize[hash]
    : (substyle.memoize[hash] = substyle.apply(this, args))
}

export default memoize
{'fraction_non_alphanumeric': 0.1396761133603239, 'fraction_numerical': 0.0, 'mean_word_length': 5.142857142857143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 1, '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': '5264602', 'n_tokens_mistral': 321, 'n_tokens_neox': 305, 'n_words': 77}