Last active
October 11, 2018 14:44
-
-
Save fionnachan/f2ca7d44034ea961b1957ed0aaefc4ea to your computer and use it in GitHub Desktop.
Check Semantic Versioning 3rd 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 foundSymbol = range.match(/\D/); | |
const rangeSymbol = foundSymbol ? foundSymbol[0] : 'default'; | |
const rangeNumArr = range.match(/[^\^|\~]+/)[0].split('.'); | |
const verNumArr = ver.split('.'); | |
const shouldMatch = { | |
'^': [['=', '>', ''], ['=', '=', '>=']], | |
'~': [['=', '=', '>=']], | |
'default': [['=', '=', '=']] | |
}; | |
const compareHelper = { | |
'=': (x, y) => parseInt(x) === parseInt(y), | |
'>': (x, y) => parseInt(x) > parseInt(y), | |
'>=': (x, y) => parseInt(x) >= parseInt(y), | |
'': (x, y) => true | |
}; | |
return shouldMatch[rangeSymbol].some((cond) => { | |
return cond.every((op, index) => { | |
return compareHelper[op](verNumArr[index], rangeNumArr[index]); | |
}); | |
}); | |
} | |
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