Name
sn_agent.AgentVersionComparator
Description
No description available
Script
var AgentVersionComparator = Class.create();
AgentVersionComparator.prototype = {
initialize: function() {},
/**
* Compare two versions.
*
* The value returned is for the first parameter compared to the second. Return values are -1, 0, or 1.
* A value of 0 indicates the versions are equal. A value of -1 indicates that the first value is an
* older version, whereas a value of 1 indicates it is a newer version when compared to the second value.
*
* When comparing versions that contain strings, the values will be compared alphabetically, with
* numeric values having a greater precedence than character values if the component is not implied from
* a shortened version string (e.g., 2.a compared to 2.a.a vs 2.a.0 compared to 2.a.a).
*
* For example, here are the expected return values:
* compare('2.00.0', '2.0') -> 0 (Evaluate to the same version)
* compare('2.a0.3', '2.a1.0') -> -1 (Alphanumeric comparison of a0 vs a1 makes versionB higher)
* compare('2.10.1', '2.3.0') -> 1 (2.10.1 is a more recent version in numeric comparisons)
* compare('2.a.4', '2.10') -> -1 (2.10 is considered more recent as numeric version has priority)
* compare('2.z.0', '2.a.4') -> 1 (2.z.0 is considered more recent to 2.a.4 due to alphabetical comparison)
* compare('2.b', '2.b.a') -> -1 (2.b.a is considered more recent to 2.b since 2.b has an implied version component)
* compare('2.b.0', '2.b.a') -> 1 (2.b.0 is considered more recent to 2.b.a as it is numeric and not implied)
*/
compare: function(versionA, versionB) {
if (versionA != versionB) {
// Split the version components
var componentsA = versionA.split('.');
var componentsB = versionB.split('.');
var iterations = Math.max(componentsA.length, componentsB.length);
for (var i = 0; i < iterations; i++) {
// Check the component exists
var existsA = componentsA.length > i;
var existsB = componentsB.length > i;
// Get the numeric values, default to zero if component not there
var valA = existsA ? componentsA[i] : 0;
var valB = existsB ? componentsB[i] : 0;
// Convert to numbers (or NaN if alphanumeric)
var numA = Number(valA);
var numB = Number(valB);
// Compare
if (isNaN(numA) && isNaN(numB)) {
// Alphabetical comparison due to strings in both versions
if (valA > valB) {
// versionA is higher than versionB
return 1;
} else if (valA < valB) {
// versionA is lower than versionB
return -1;
}
} else if (isNaN(numA) && !isNaN(numB)) {
// Mixed Comparison - versionA is numeric and versionB is alphanumeric
// If the versionA component is implied (not present) versionB is higher (e.g., 2.a vs 2.a.b),
// otherwise versionA takes priority as it is a number (2.a.5 vs 2.a.b),
if (existsA) {
return -1;
}
return 1;
} else if (!isNaN(numA) && isNaN(numB)) {
// Mixed Comparison - versionA is alphanumeric and versionB is numeric
// If the versionB component is implied (not present) versionA is higher (e.g., 2.a.b vs 2.a),
// otherwise versionB takes priority as it is a number (2.a.b vs 2.a.1),
if (existsB) {
return -1;
}
return 1;
} else {
// Numeric comparison
if (numA > numB) {
// versionA is higher than versionB
return 1;
} else if (numA < numB) {
// versionA is lower than versionB
return -1;
}
}
}
}
// Versions are equal
return 0;
},
type: 'AgentVersionComparator'
};
Sys ID
0cf702c1532021100b12ddeeff7b1274