Skip to content

Instantly share code, notes, and snippets.

@gryzzly
Created April 14, 2023 09:20
Show Gist options
  • Save gryzzly/a677df537178773414ce261d70738f4a to your computer and use it in GitHub Desktop.
Save gryzzly/a677df537178773414ce261d70738f4a to your computer and use it in GitHub Desktop.
Semver generated by chatGPT
// semver.js
function isValidSemVer(version) {
const semverRegex = /^(\d+)\.(\d+)\.(\d+)(?:-([\w\d]+(?:\.[\w\d]+)*))?(?:\+([\w\d]+(?:\.[\w\d]+)*))?$/;
if (!semverRegex.test(version)) {
return false;
}
return true;
}
function isSemVerInRange(version, range) {
const components = range.split(' ');
if (components.length > 1) {
const operator = components[0];
const targetVersion = components[1];
switch (operator) {
case '>':
return isValidSemVer(targetVersion) && version > targetVersion;
case '>=':
return isValidSemVer(targetVersion) && version >= targetVersion;
case '<':
return isValidSemVer(targetVersion) && version < targetVersion;
case '<=':
return isValidSemVer(targetVersion) && version <= targetVersion;
default:
return false;
}
} else {
return isValidSemVer(components[0]) && version === components[0];
}
}
export { isValidSemVer, isSemVerInRange };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment