From c257717ea00ffc70fd0cd43daf364728792ed804 Mon Sep 17 00:00:00 2001
From: Hutson Betts <hutson@hyper-expanse.net>
Date: Sat, 25 Mar 2017 23:08:01 -0500
Subject: [PATCH] refactor(versions): refactor versions helper (#143)

Refactor versions helper to use lodash filters, instead of
nested if statements.
---
 lib/helpers/versions.js | 42 ++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/lib/helpers/versions.js b/lib/helpers/versions.js
index fddef22e58..dcc52616ba 100644
--- a/lib/helpers/versions.js
+++ b/lib/helpers/versions.js
@@ -1,6 +1,7 @@
 const logger = require('winston');
 const semver = require('semver');
 const stable = require('semver-stable');
+const _ = require('lodash');
 
 module.exports = {
   determineUpgrades,
@@ -11,11 +12,11 @@ module.exports = {
 };
 
 function determineUpgrades(dep, currentVersion, config) {
-  const versions = dep.versions;
   if (!isValidVersion(currentVersion)) {
     logger.verbose(`${dep.name} currentVersion is invalid`);
     return [];
   }
+  const versions = dep.versions;
   if (!versions || Object.keys(versions).length === 0) {
     logger.verbose(`${dep.name} - no versions`);
     return [];
@@ -34,24 +35,24 @@ function determineUpgrades(dep, currentVersion, config) {
     };
     workingVersion = maxSatisfying;
   }
-  // Loop through all possible versions
-  versionList.forEach((newVersion) => {
-    if (semver.gt(newVersion, workingVersion)) {
-      if (config.ignoreUnstable && stable.is(workingVersion) && !stable.is(newVersion)) {
-        // Ignore unstable versions, unless the current version is unstable
-        logger.debug(`Ignoring version ${newVersion} because it's unstable`);
-        return;
-      }
-      if (config.ignoreFuture &&
-          !isFuture(versions[workingVersion]) && isFuture(versions[newVersion])) {
-        logger.debug(`Ignoring version ${newVersion} because it's marked as "future"`);
-        return;
-      }
-      if (config.respectLatest
-          && isPastLatest(dep, newVersion) && !isPastLatest(dep, workingVersion)) {
-        logger.debug(`Ignoring version ${newVersion} because it's newer than the repo's "latest" tag`);
-        return;
-      }
+  _(versionList)
+    // Filter out older versions as we can't upgrade to those
+    .filter(version => semver.gt(version, workingVersion))
+
+    // Ignore unstable versions, unless the current version is unstable
+    .reject(version => config.ignoreUnstable &&
+            stable.is(workingVersion) && !stable.is(version))
+
+    // Ignore future versions, unless the current version is marked as future
+    .reject(version => config.ignoreFuture &&
+            !isFuture(versions[workingVersion]) && isFuture(versions[version]))
+
+    // Ignore versions newer than "latest", unless current version is newer than the "latest"
+    .reject(version => config.respectLatest &&
+            isPastLatest(dep, version) && !isPastLatest(dep, workingVersion))
+
+    // Loop through all possible versions
+    .forEach((newVersion) => {
       // Group by major versions
       const newVersionMajor = semver.major(newVersion);
       // Save this, if it's a new major version or greater than the previous greatest
@@ -65,8 +66,7 @@ function determineUpgrades(dep, currentVersion, config) {
           workingVersion,
         };
       }
-    }
-  });
+    });
   if (allUpgrades.pin && Object.keys(allUpgrades).length > 1) {
     // Remove the pin if we found upgrades
     delete allUpgrades.pin;
-- 
GitLab