Created
April 14, 2023 09:20
-
-
Save gryzzly/a677df537178773414ce261d70738f4a to your computer and use it in GitHub Desktop.
Semver generated by chatGPT
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
// 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