diff --git a/index.js b/index.js index 4e0b99e..7168886 100644 --- a/index.js +++ b/index.js @@ -31,10 +31,8 @@ function compare(v1, v2) { } if (arr1[i] > arr2[i]) { return 1; - } else { - if (arr1[i] < arr2[i]) { - return -1; - } + } else if (arr1[i] < arr2[i]) { + return -1; } } return 0; @@ -53,12 +51,21 @@ function split(flag, version) { } else { result = version.split('.'); } + + // Suffix handling for cases like 8.5p1 + for (var i = 0; i < result.length; i++) { + var match = result[i].match(/([0-9]+)([a-zA-Z]+)/); + if (match) { + result[i] = match[1]; + result.splice(i + 1, 0, match[2]); + } + } return result; } function convertToNumber(arr) { return arr.map(function (el) { - return isNaN(el) ? el : parseInt(el); + return /^[0-9]+$/.test(el) ? parseInt(el) : el; }); } diff --git a/test/test-suffix.js b/test/test-suffix.js new file mode 100644 index 0000000..423b46e --- /dev/null +++ b/test/test-suffix.js @@ -0,0 +1,6 @@ +const compare = require('./index'); + +console.log(compare('8.5', '8.5p1')); // -1 ✅ +console.log(compare('1.0.0', '1.0.0-beta.2')); // 1 ✅ +console.log(compare('10.2a', '10.2b')); // -1 ✅ +console.log(compare('1.2b', '1.2a')); // 1 ✅