Skip to content

Instantly share code, notes, and snippets.

@fionnachan
Last active October 10, 2018 13:59
Show Gist options
  • Save fionnachan/e93943a612718788dfb4f447099f6485 to your computer and use it in GitHub Desktop.
Save fionnachan/e93943a612718788dfb4f447099f6485 to your computer and use it in GitHub Desktop.
Check Semantic Versioning 1st attempt
const correctSemverRange = (ver, range) => {
const shouldMatch = {
'^': [['=', '>', ''], ['=', '=', '>=']],
'~': [['=', '=', '>=']],
'default': [['=', '=', '=']]
};
const rangeSymbol = range.substring(0, 1);
const verNumArr = ver.split('.');
let rangeNumArr = range.split('.');
let result = false;
switch (rangeSymbol) {
case '^':
case '~':
rangeNumArr[0] = rangeNumArr[0].substring(1);
break;
default:
rangeSymbol = 'default';
}
for (let j = 0, caseNums = shouldMatch[rangeSymbol].length; j < caseNums; j++) {
let cond = shouldMatch[rangeSymbol][j];
for (let i = 0, arrLength = verNumArr.length; i < arrLength; i++) {
if (cond[i] === '=') {
result = (parseInt(verNumArr[i]) === parseInt(rangeNumArr[i]));
} else if (cond[i] === '>') {
result = (parseInt(verNumArr[i]) > parseInt(rangeNumArr[i]));
} else if (cond[i] === '>=') {
result = (parseInt(verNumArr[i]) >= parseInt(rangeNumArr[i]));
} else if (cond[i] === '') {
result = true;
}
if (!result) {
break;
}
}
if (result) {
break;
}
}
return result;
}
correctSemverRange("1.12.4", "~1.12.0");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment