<commit_before>const _memoize = require("lodash/memoize");

function bareFindModuleByName(pGraph, pSource) {
  return pGraph.find((pNode) => pNode.source === pSource);
}

const findModuleByName = _memoize(
  bareFindModuleByName,
  (_pGraph, pSource) => pSource
);

function isDependent(pResolvedName) {
  return (pModule) =>
    pModule.dependencies.some(
      (pDependency) => pDependency.resolved === pResolvedName
    );
}

function clearCache() {
  findModuleByName.cache.clear();
}

module.exports = { findModuleByName, clearCache, isDependent };<commit_msg>refactor(enrich): use different caching strategy for finding modules by name (#531)<commit_after>let gIndexedGraph = null;

/**
 * Returns the module with attribute pSource, when it exists in the pModuleGraph.
 * Returns undefined when it doesn't.
 *
 * This function uses an indexed cache for efficiency reasons. If you need to
 * call this function consecutively for different module graphs, you can clear
 * this cache with the clearCache function from this module.
 *
 * @param {import('../../../types/cruise-result').IModule[]} pModuleGraph
 * @param {string} pSource
 * @returns {import('../../../types/cruise-result').IModule | undefined}
 */
function findModuleByName(pModuleGraph, pSource) {
  if (!gIndexedGraph) {
    gIndexedGraph = new Map(
      pModuleGraph.map((pModule) => [pModule.source, pModule])
    );
  }
  return gIndexedGraph.get(pSource);
}

function isDependent(pResolvedName) {
  return (pModule) =>
    pModule.dependencies.some(
      (pDependency) => pDependency.resolved === pResolvedName
    );
}

function clearCache() {
  gIndexedGraph = null;
}

module.exports = {
  findModuleByName,
  clearCache,
  isDependent,
};
{'fraction_non_alphanumeric': 0.1252184041933605, 'fraction_numerical': 0.0017472335468841002, 'mean_word_length': 4.40251572327044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '4299481', 'n_tokens_mistral': 531, 'n_tokens_neox': 528, 'n_words': 133}