Skip to content

Instantly share code, notes, and snippets.

@fionnachan
Last active October 11, 2018 14:44
Show Gist options
  • Save fionnachan/f2ca7d44034ea961b1957ed0aaefc4ea to your computer and use it in GitHub Desktop.
Save fionnachan/f2ca7d44034ea961b1957ed0aaefc4ea to your computer and use it in GitHub Desktop.
Check Semantic Versioning 3rd attempt
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