Skip to content

Commit

Permalink
added a compare version number helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Apr 8, 2013
1 parent c4a6c58 commit 3d37290
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
37 changes: 37 additions & 0 deletions hawtio-web/src/main/webapp/app/core/js/helpers.ts
Expand Up @@ -579,4 +579,41 @@ module Core {
}
return null;
}

/**
* Compares the 2 version arrays and returns -1 if v1 is less than v2 or 0 if they are equal or 1 if v1 is greater than v2
*
* @param v1 an array of version numbers with the most significant version first (major, minor, patch).
* @param v2
*/
export function compareVersionNumberArrays(v1:number[], v2:number[]) {
if (v1 && !v2) {
return 1;
}
if (!v1 && v2) {
return -1;
}
if (v1 === v2) {
return 0;
}
for (var i = 0; i < v1.length; i++) {
var n1 = v1[i];
if (i >= v2.length) {
return 1;
}
var n2 = v2[i];
if (!angular.isDefined(n1)) {
return -1;
}
if (!angular.isDefined(n2)) {
return 1;
}
if (n1 > n2) {
return 1;
} else if (n1 < n2) {
return -1;
}
}
return 0;
}
}
14 changes: 14 additions & 0 deletions hawtio-web/src/test/specs/spec/CoreSpec.js
Expand Up @@ -34,4 +34,18 @@ describe("Core", function() {
expect(Core.parseVersionNumbers("camel-2.3.45.jar")).toEqual([2, 3, 45]);
expect(Core.parseVersionNumbers("camel-55.3.45.jar")).toEqual([55, 3, 45]);
});

it("compare version numbers", function() {
function compareVersion(text, version, expectedValue) {
var actualVersion = Core.parseVersionNumbers(text);
var compared = Core.compareVersionNumberArrays(actualVersion, version);
expect(compared).toEqual(expectedValue);
}
expect(compareVersion("camel-2.1.jar", [2, 1], 0));
expect(compareVersion("camel-2.11.jar", [2, 10], 1));
expect(compareVersion("camel-2.11.0.jar", [2, 10], 1));
expect(compareVersion("camel-2.11.jar", [2, 12], -1));
expect(compareVersion("camel-2.11.2.jar", [2, 12], -1));
expect(compareVersion("camel-2.11.2.jar", [2, 11, 3], -1));
});
});

0 comments on commit 3d37290

Please sign in to comment.