Skip to content

Instantly share code, notes, and snippets.

@privman
Last active August 29, 2015 14:07
Show Gist options
  • Save privman/bf5bf6ae0cd0caed4cc1 to your computer and use it in GitHub Desktop.
Save privman/bf5bf6ae0cd0caed4cc1 to your computer and use it in GitHub Desktop.
var Util = {
versionString: '0.12.0.1', // Change this manually each version, or (better) assign Util.versionString from config or build script
isOlderThan: function (thresholdVersionString) {
return (Util.versionStringToFloat(Util.versionString) < Util.versionStringToFloat(thresholdVersionString));
},
// Converts a string of form '1.4.2' to an easily comparable decimal number
// Assumes sub-enumeration cannot reach 1000
versionStringToFloat: function (versionString) {
var splitVersion = versionString.split('.'),
maxEnumeration = 1000;
return _.reduceRight( // I used underscore.js, but it's a standard implementation
versionString.split('.'),
function (total, subEnumeration) {
subEnumeration = +subEnumeration; // Convert from string to number
total /= maxEnumeration;
total += subEnumeration;
return total;
},
0 // initial value for total
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment