Last active
October 10, 2018 13:59
-
-
Save fionnachan/e93943a612718788dfb4f447099f6485 to your computer and use it in GitHub Desktop.
Check Semantic Versioning 1st attempt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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