Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
});
}

Expand Down
6 changes: 6 additions & 0 deletions test/test-suffix.js
Original file line number Diff line number Diff line change
@@ -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 ✅