Skip to content

Instantly share code, notes, and snippets.

@dislick
Last active March 6, 2023 00:42
Show Gist options
  • Save dislick/914e67444f8f71df3900bd77ccec6091 to your computer and use it in GitHub Desktop.
Save dislick/914e67444f8f71df3900bd77ccec6091 to your computer and use it in GitHub Desktop.
Convert Semantic Versioning String to Integer in ES6
/**
* Convert a sematic versioning string into an 32-bit integer.
*
* Make sure the input string is compatible with the standard found
* at semver.org. Since this only uses 10-bit per major/minor/patch version,
* the highest possible SemVer string would be 1023.1023.1023.
* @param {string} version SemVer string
* @return {number} Numeric version
*/
const convertVersionToInt32 = function(version) {
// Split a given version string into three parts.
let parts = version.split('.');
// Check if we got exactly three parts, otherwise throw an error.
if (parts.length !== 3) {
throw new Error('Received invalid version string');
}
// Make sure that no part is larger than 1023 or else it
// won't fit into a 32-bit integer.
parts.forEach((part) => {
if (part >= 1024) {
throw new Error(`Version string invalid, ${part} is too large`);
}
});
// Let's create a new number which we will return later on
let numericVersion = 0;
// Shift all parts either 0, 10 or 20 bits to the left.
for (let i = 0; i < 3; i++) {
numericVersion |= parts[i] << i * 10;
}
return numericVersion;
};
/**
* Converts a 32-bit integer into a semantic versioning (SemVer) compatible string.
* @param {number} v Numeric version
* @return {string} SemVer string
*/
const convertInt32VersionToString = function(v) {
// Works by shifting the numeric version to the right and then masking it
// with 0b1111111111 (or 1023 in decimal).
return `${v & 1023}.${v >> 10 & 1023}.${v >> 20 & 1023}`
};
@thomijasir
Copy link

Hemm I think there is some bug in your code..

convertVersionToInt32('1.1.1') 
// 1049601

convertVersionToInt32('1.0.1')
// 1048577

so far so good right, but when I input

convertVersionToInt32('2.1.0')
// 1026
convertVersionToInt32('2.1.0') > convertVersionToInt32('1.1.1')
// it return false, should true 2.1.0 is grather than 1.1.1

@dislick
Copy link
Author

dislick commented Jan 24, 2023

Hemm I think there is some bug in your code..

The integers produced by this function were never meant to be sortable. The goal was to encode a semver string in a 32-bit integer.

@yeganemehr
Copy link

Actually it can convert to a sortable int easily.
Checkout my fork: https://gist.github.com/yeganemehr/2468c2b28acde8f34dd170db65f05f62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment