Unverified Commit b9f2f756 by Sam Padgett Committed by GitHub

Merge pull request #301 from spadgett/compare-versions

Add utilities for comparing versions
parents 6131019b 5c40516a
...@@ -37,7 +37,8 @@ ...@@ -37,7 +37,8 @@
"kubernetes-label-selector": "~2.1.1", "kubernetes-label-selector": "~2.1.1",
"lodash": "~4.17.4", "lodash": "~4.17.4",
"patternfly": ">=3.27.2 <4.0", "patternfly": ">=3.27.2 <4.0",
"uri.js": "~1.18.12" "uri.js": "~1.18.12",
"compare-versions": "^3.1.0"
}, },
"devDependencies": { "devDependencies": {
"angular-mocks": "~1.5.11", "angular-mocks": "~1.5.11",
......
...@@ -3721,6 +3721,36 @@ angular.module('openshiftCommonServices') ...@@ -3721,6 +3721,36 @@ angular.module('openshiftCommonServices')
}); });
;'use strict'; ;'use strict';
angular.module("openshiftCommonServices")
.service("VersionsService", function(){
var compareInteral = function(v1, v2, newestFirst) {
v1 = v1 || '';
v2 = v2 || '';
try {
// compareVersions will sort via semver and throw an error if one of
// the values is not a version string.
var result = window.compareVersions(v1, v2);
return newestFirst ? result * -1 : result;
} catch (e) {
// One of the values is not a version string. Fall back to string comparison.
// Numbers will be sorted higher by localeCompare.
return v1.localeCompare(v2);
}
};
return {
// Order oldest version first.
compare: function(v1, v2) {
return compareInteral(v1, v2, false);
},
// Order newest version first.
rcompare: function(v1, v2) {
return compareInteral(v1, v2, true);
},
};
});
;'use strict';
// Provide a websocket implementation that behaves like $http // Provide a websocket implementation that behaves like $http
// Methods: // Methods:
// $ws({ // $ws({
......
...@@ -5787,6 +5787,36 @@ angular.module('openshiftCommonServices') ...@@ -5787,6 +5787,36 @@ angular.module('openshiftCommonServices')
}); });
;'use strict'; ;'use strict';
angular.module("openshiftCommonServices")
.service("VersionsService", function(){
var compareInteral = function(v1, v2, newestFirst) {
v1 = v1 || '';
v2 = v2 || '';
try {
// compareVersions will sort via semver and throw an error if one of
// the values is not a version string.
var result = window.compareVersions(v1, v2);
return newestFirst ? result * -1 : result;
} catch (e) {
// One of the values is not a version string. Fall back to string comparison.
// Numbers will be sorted higher by localeCompare.
return v1.localeCompare(v2);
}
};
return {
// Order oldest version first.
compare: function(v1, v2) {
return compareInteral(v1, v2, false);
},
// Order newest version first.
rcompare: function(v1, v2) {
return compareInteral(v1, v2, true);
},
};
});
;'use strict';
// Provide a websocket implementation that behaves like $http // Provide a websocket implementation that behaves like $http
// Methods: // Methods:
// $ws({ // $ws({
......
...@@ -2610,6 +2610,24 @@ error_description:"No API token returned" ...@@ -2610,6 +2610,24 @@ error_description:"No API token returned"
} }
}; };
} ]; } ];
}), angular.module("openshiftCommonServices").service("VersionsService", function() {
var compareInteral = function(v1, v2, newestFirst) {
v1 = v1 || "", v2 = v2 || "";
try {
var result = window.compareVersions(v1, v2);
return newestFirst ? -1 * result :result;
} catch (e) {
return v1.localeCompare(v2);
}
};
return {
compare:function(v1, v2) {
return compareInteral(v1, v2, !1);
},
rcompare:function(v1, v2) {
return compareInteral(v1, v2, !0);
}
};
}), angular.module("openshiftCommonServices").provider("$ws", [ "$httpProvider", function($httpProvider) { }), angular.module("openshiftCommonServices").provider("$ws", [ "$httpProvider", function($httpProvider) {
this.$get = [ "$q", "$injector", "Logger", function($q, $injector, Logger) { this.$get = [ "$q", "$injector", "Logger", function($q, $injector, Logger) {
var authLogger = Logger.get("auth"); var authLogger = Logger.get("auth");
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
"description": "Library of common services and components for OpenShift Web Console.", "description": "Library of common services and components for OpenShift Web Console.",
"homepage": "https://github.com/openshift/origin-web-common.git", "homepage": "https://github.com/openshift/origin-web-common.git",
"dependencies": { "dependencies": {
"angular": "^1.5.11" "angular": "^1.5.11",
"compare-versions": "^3.1.0"
}, },
"devDependencies": { "devDependencies": {
"bower": "^1.8.2", "bower": "^1.8.2",
......
'use strict';
angular.module("openshiftCommonServices")
.service("VersionsService", function(){
var compareInteral = function(v1, v2, newestFirst) {
v1 = v1 || '';
v2 = v2 || '';
try {
// compareVersions will sort via semver and throw an error if one of
// the values is not a version string.
var result = window.compareVersions(v1, v2);
return newestFirst ? result * -1 : result;
} catch (e) {
// One of the values is not a version string. Fall back to string comparison.
// Numbers will be sorted higher by localeCompare.
return v1.localeCompare(v2);
}
};
return {
// Order oldest version first.
compare: function(v1, v2) {
return compareInteral(v1, v2, false);
},
// Order newest version first.
rcompare: function(v1, v2) {
return compareInteral(v1, v2, true);
},
};
});
...@@ -18,6 +18,7 @@ module.exports = function(config) { ...@@ -18,6 +18,7 @@ module.exports = function(config) {
files: [ files: [
"bower_components/jquery/dist/jquery.js", "bower_components/jquery/dist/jquery.js",
"bower_components/lodash/lodash.js", "bower_components/lodash/lodash.js",
"bower_components/compare-versions/index.js",
"bower_components/angular/angular.js", "bower_components/angular/angular.js",
'bower_components/angular-mocks/angular-mocks.js', 'bower_components/angular-mocks/angular-mocks.js',
"bower_components/angular-sanitize/angular-sanitize.js", "bower_components/angular-sanitize/angular-sanitize.js",
......
describe('VersionsService', function() {
'use strict';
var VersionsService;
beforeEach(function() {
inject(function(_VersionsService_) {
VersionsService = _VersionsService_;
});
});
var versions = ["10.1", "9.1", "10.0", "9.0", "8.0", "10.1.1", "0.10", "latest", "old"];
describe('#compare', function() {
it('sort versions with lowest version first', function() {
var result = angular.copy(versions).sort(VersionsService.compare);
return expect(result).toEqual(["0.10", "8.0", "9.0", "9.1", "10.0", "10.1", "10.1.1", "latest", "old"]);
});
});
describe('#rcompare', function() {
it('sort versions with highest version first', function() {
var result = angular.copy(versions).sort(VersionsService.rcompare);
return expect(result).toEqual(["10.1.1", "10.1", "10.0", "9.1", "9.0", "8.0", "0.10", "latest", "old"]);
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment