<commit_before>var Module = require('module')
  , dirname = require('path').dirname
  , join = require('path').join
  , resolve = require('path').resolve
  , callerId = require('caller-id')
  , originalLoader = Module._load
  , mockExports   = {}
  , pendingMockExports = {}
  ;

Module._load = function(request, parent) {
  var fullFilePath = getFullPath(request, parent.filename);

  if (pendingMockExports.hasOwnProperty(fullFilePath)){
    mockExports[fullFilePath] = typeof pendingMockExports[fullFilePath] === 'string' ?
        require(pendingMockExports[fullFilePath]) :
        pendingMockExports[fullFilePath];

    delete pendingMockExports[fullFilePath];
  }

  return mockExports.hasOwnProperty(fullFilePath)
    ? mockExports[fullFilePath]
    : originalLoader.apply(this, arguments);
};

function startMocking(path, mockExport) {
  var calledFrom = callerId.getData().filePath;

  if (typeof mockExport === 'string') {
    mockExport = getFullPath(mockExport, calledFrom);
  }

  pendingMockExports[getFullPath(path, calledFrom)] = mockExport;
}

function stopMocking(path) {
  var calledFrom = callerId.getData().filePath;
  var fullPath = getFullPath(path, calledFrom);
  delete pendingMockExports[fullPath];
  delete mockExports[fullPath];
}

function stopMockingAll() {
  mockExports = {};
  pendingMockExports = {};
}

function reRequire(path) {
  var module = getFullPath(path, callerId.getData().filePath);
  delete require.cache[require.resolve(module)];
  return require(module);
}

function isFileContainedIn(dir, file, path) {
    if (!file)
        return false;

    // `dir` might be relative, make it absolute
    dir = resolve(process.cwd(), dir);

    // Do not consider files in node_modules to be in NODE_PATH
    if (/[/\\]node_modules[/\\]?/.test(dir))
        return false;

    const longPath = resolve(dir, path);

    if (file.indexOf(longPath) !== 0)
        return false;

    try {
        return file == require.resolve(longPath);
    } catch (e) {
        return false;
    }
}

function isInNodePath(path, resolvedPath) {
    // Check whether resolvedPath is in any of the NODE_PATH directories.
    return Module.globalPaths.some(function(p) {
        return isFileContainedIn(p, resolvedPath, path);
    });
}

function getFullPath(path, calledFrom) {
  var resolvedPath;
  try {
    resolvedPath = require.resolve(path);
  } catch(e) { }

  var isLocalModule = /^\.{1,2}[/\\]?/.test(path);
  var isInPath = isInNodePath(path, resolvedPath);
  var isExternal = !isLocalModule && /[/\\]node_modules[/\\]/.test(resolvedPath);
  var isSystemModule = resolvedPath === path;

  if (isExternal || isSystemModule || isInPath) {
    return resolvedPath;
  }

  if (!isLocalModule) {
    return path;
  }
<commit_msg>Simplify checking if the file is in the node path<commit_after>var Module = require('module')
  , dirname = require('path').dirname
  , join = require('path').join
  , resolve = require('path').resolve
  , callerId = require('caller-id')
  , originalLoader = Module._load
  , mockExports   = {}
  , pendingMockExports = {}
  ;

Module._load = function(request, parent) {
  var fullFilePath = getFullPath(request, parent.filename);

  if (pendingMockExports.hasOwnProperty(fullFilePath)){
    mockExports[fullFilePath] = typeof pendingMockExports[fullFilePath] === 'string' ?
        require(pendingMockExports[fullFilePath]) :
        pendingMockExports[fullFilePath];

    delete pendingMockExports[fullFilePath];
  }

  return mockExports.hasOwnProperty(fullFilePath)
    ? mockExports[fullFilePath]
    : originalLoader.apply(this, arguments);
};

function startMocking(path, mockExport) {
  var calledFrom = callerId.getData().filePath;

  if (typeof mockExport === 'string') {
    mockExport = getFullPath(mockExport, calledFrom);
  }

  pendingMockExports[getFullPath(path, calledFrom)] = mockExport;
}

function stopMocking(path) {
  var calledFrom = callerId.getData().filePath;
  var fullPath = getFullPath(path, calledFrom);
  delete pendingMockExports[fullPath];
  delete mockExports[fullPath];
}

function stopMockingAll() {
  mockExports = {};
  pendingMockExports = {};
}

function reRequire(path) {
  var module = getFullPath(path, callerId.getData().filePath);
  delete require.cache[require.resolve(module)];
  return require(module);
}

function isInNodePath(path, resolvedPath) {
  if (!resolvedPath) return false;

  return Module.globalPaths
    .map(function(nodePath) {
      return resolve(process.cwd(), nodePath);
    })
    .some(function(fullNodePath) {
      return resolvedPath.indexOf(fullNodePath) === 0;
    });
}

function getFullPath(path, calledFrom) {
  var resolvedPath;
  try {
    resolvedPath = require.resolve(path);
  } catch(e) { }

  var isLocalModule = /^\.{1,2}[/\\]?/.test(path);
  var isInPath = isInNodePath(path, resolvedPath);
  var isExternal = !isLocalModule && /[/\\]node_modules[/\\]/.test(resolvedPath);
  var isSystemModule = resolvedPath === path;

  if (isExternal || isSystemModule || isInPath) {
    return resolvedPath;
  }

  if (!isLocalModule) {
    return path;
  }

{'fraction_non_alphanumeric': 0.13244252308901552, 'fraction_numerical': 0.0011790135586559245, 'mean_word_length': 4.120724346076459, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6655509', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1563, 'n_words': 322}