From 37e247e114d6086b2a8fc0e5e3ad8b9d2ca853ab Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 14 Jul 2018 09:49:53 +0200
Subject: [PATCH] refactor(npm): major minor handling

---
 lib/versioning/semver/range.js | 45 +++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/lib/versioning/semver/range.js b/lib/versioning/semver/range.js
index ce3e62b2ee..018615f077 100644
--- a/lib/versioning/semver/range.js
+++ b/lib/versioning/semver/range.js
@@ -38,7 +38,8 @@ function getNewValue(currentValue, rangeStrategy, fromVersion, toVersion) {
     }
     return `${currentValue} || ${newValue}`;
   }
-  // console.log(range);
+  const toVersionMajor = major(toVersion);
+  const toVersionMinor = minor(toVersion);
   // Simple range
   if (rangeStrategy === 'bump') {
     if (parsedRange.length === 1) {
@@ -46,11 +47,11 @@ function getNewValue(currentValue, rangeStrategy, fromVersion, toVersion) {
         const split = currentValue.split('.');
         if (split.length === 1) {
           // ^4
-          return '^' + major(toVersion);
+          return '^' + toVersionMajor;
         }
         if (split.length === 2) {
           // ^4.1
-          return '^' + major(toVersion) + '.' + minor(toVersion);
+          return '^' + toVersionMajor + '.' + toVersionMinor;
         }
         return `^${toVersion}`;
       }
@@ -58,11 +59,11 @@ function getNewValue(currentValue, rangeStrategy, fromVersion, toVersion) {
         const split = currentValue.split('.');
         if (split.length === 1) {
           // ~4
-          return '~' + major(toVersion);
+          return '~' + toVersionMajor;
         }
         if (split.length === 2) {
           // ~4.1
-          return '~' + major(toVersion) + '.' + minor(toVersion);
+          return '~' + toVersionMajor + '.' + toVersionMinor;
         }
         return `~${toVersion}`;
       }
@@ -84,31 +85,31 @@ function getNewValue(currentValue, rangeStrategy, fromVersion, toVersion) {
     if (!fromVersion) {
       return `^${toVersion}`;
     }
-    if (major(toVersion) === major(fromVersion)) {
-      if (major(toVersion) === 0) {
-        if (minor(toVersion) === 0) {
+    if (toVersionMajor === major(fromVersion)) {
+      if (toVersionMajor === 0) {
+        if (toVersionMinor === 0) {
           return `^${toVersion}`;
         }
-        return `^${major(toVersion)}.${minor(toVersion)}.0`;
+        return `^${toVersionMajor}.${toVersionMinor}.0`;
       }
       return `^${toVersion}`;
     }
-    return `^${major(toVersion)}.0.0`;
+    return `^${toVersionMajor}.0.0`;
   }
   if (element.operator === '=') {
     return `=${toVersion}`;
   }
   if (element.operator === '~') {
-    return `~${major(toVersion)}.${minor(toVersion)}.0`;
+    return `~${toVersionMajor}.${toVersionMinor}.0`;
   }
   if (element.operator === '<=') {
     let res;
     if (element.patch) {
       res = `<=${toVersion}`;
     } else if (element.minor) {
-      res = `<=${major(toVersion)}.${minor(toVersion)}`;
+      res = `<=${toVersionMajor}.${toVersionMinor}`;
     } else {
-      res = `<=${major(toVersion)}`;
+      res = `<=${toVersionMajor}`;
     }
     if (currentValue.includes('<= ')) {
       res = res.replace('<=', '<= ');
@@ -118,14 +119,14 @@ function getNewValue(currentValue, rangeStrategy, fromVersion, toVersion) {
   if (element.operator === '<') {
     let res;
     if (currentValue.endsWith('.0.0')) {
-      const newMajor = major(toVersion) + 1;
+      const newMajor = toVersionMajor + 1;
       res = `<${newMajor}.0.0`;
     } else if (element.patch) {
       res = `<${increment(toVersion, 'patch')}`;
     } else if (element.minor) {
-      res = `<${major(toVersion)}.${minor(toVersion) + 1}`;
+      res = `<${toVersionMajor}.${toVersionMinor + 1}`;
     } else {
-      res = `<${major(toVersion) + 1}`;
+      res = `<${toVersionMajor + 1}`;
     }
     if (currentValue.includes('< ')) {
       res = res.replace('<', '< ');
@@ -135,20 +136,20 @@ function getNewValue(currentValue, rangeStrategy, fromVersion, toVersion) {
   if (!element.operator) {
     if (element.minor) {
       if (element.minor === 'x') {
-        return `${major(toVersion)}.x`;
+        return `${toVersionMajor}.x`;
       }
       if (element.minor === '*') {
-        return `${major(toVersion)}.*`;
+        return `${toVersionMajor}.*`;
       }
       if (element.patch === 'x') {
-        return `${major(toVersion)}.${minor(toVersion)}.x`;
+        return `${toVersionMajor}.${toVersionMinor}.x`;
       }
       if (element.patch === '*') {
-        return `${major(toVersion)}.${minor(toVersion)}.*`;
+        return `${toVersionMajor}.${toVersionMinor}.*`;
       }
-      return `${major(toVersion)}.${minor(toVersion)}`;
+      return `${toVersionMajor}.${toVersionMinor}`;
     }
-    return `${major(toVersion)}`;
+    return `${toVersionMajor}`;
   }
   return toVersion;
 }
-- 
GitLab