<commit_before>const {
  getMajor,
  isGreaterThan,
  isStable,
} = require('../../../versioning/semver');

module.exports = {
  filterVersions,
  filterUnstable,
  filterLatest,
};

function filterVersions(config, fromVersion, latestVersion, versions) {
  const { ignoreUnstable, respectLatest } = config;
  if (!fromVersion) {
    return [];
  }
  // Leave only versions greater than current
  let filteredVersions = versions.filter(version =>
    isGreaterThan(version, fromVersion)
  );
  filteredVersions = filterUnstable(
    ignoreUnstable,
    fromVersion,
    filteredVersions
  );
  filteredVersions = filterLatest(
    respectLatest,
    fromVersion,
    latestVersion,
    filteredVersions
  );
  return filteredVersions;
}

function filterUnstable(ignoreUnstable, fromVersion, versions) {
  // Filter nothing out if we are not ignoring unstable
  if (ignoreUnstable === false) {
    return versions;
  }
  // Filter out all unstable if fromVersion is stable
  if (isStable(fromVersion)) {
    // Remove all unstable
    return versions.filter(isStable);
  }
  // Allow unstable only in current major
  return versions.filter(
    version => isStable(version) || getMajor(version) === getMajor(fromVersion)
  );
}

function filterLatest(respectLatest, fromVersion, latestVersion, versions) {
  // No filtering if no latest
  if (!latestVersion) {
    return versions;
  }
  // No filtering if not respecting latest
  if (respectLatest === false) {
    return versions;
  }
  // No filtering if fromVersion is already past latest
  if (isGreaterThan(fromVersion, latestVersion)) {
    return versions;
  }
  return versions.filter(version => !isGreaterThan(version, latestVersion));
}<commit_msg>fix: respect latest only if ignoring unstable<commit_after>const {
  getMajor,
  isGreaterThan,
  isStable,
} = require('../../../versioning/semver');

module.exports = {
  filterVersions,
  filterLatest,
};

function filterVersions(config, fromVersion, latestVersion, versions) {
  const { ignoreUnstable, respectLatest } = config;
  if (!fromVersion) {
    return [];
  }

  // Leave only versions greater than current
  let filteredVersions = versions.filter(version =>
    isGreaterThan(version, fromVersion)
  );

  // Return all versions if we aren't ignore unstable. Also ignore latest
  if (ignoreUnstable === false) {
    return filteredVersions;
  }

  // if current is unstable then allow unstable in the current major only
  if (!isStable(fromVersion)) {
    // Allow unstable only in current major
    return filteredVersions.filter(
      version =>
        isStable(version) || getMajor(version) === getMajor(fromVersion)
    );
  }

  // Normal case: remove all unstable
  filteredVersions = filteredVersions.filter(isStable);

  return filterLatest(
    respectLatest,
    fromVersion,
    latestVersion,
    filteredVersions
  );
}

function filterLatest(respectLatest, fromVersion, latestVersion, versions) {
  // No filtering if no latest
  // istanbul ignore if
  if (!latestVersion) {
    return versions;
  }
  // No filtering if not respecting latest
  if (respectLatest === false) {
    return versions;
  }
  // No filtering if fromVersion is already past latest
  if (isGreaterThan(fromVersion, latestVersion)) {
    return versions;
  }
  return versions.filter(version => !isGreaterThan(version, latestVersion));
}
{'fraction_non_alphanumeric': 0.10059701492537314, 'fraction_numerical': 0.0, 'mean_word_length': 3.733050847457627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1057393', 'n_tokens_mistral': 985, 'n_tokens_neox': 953, 'n_words': 269}