fix: standardize version parsing and simplify string escaping
This commit is contained in:
1 parent
4e2b6b1200
commit
38a7c3da63
23 files changed
+245
-137
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import parse from 'semver/functions/parse.js';
|
||||
|
||||
/** Parse a canonical SemVer string; tag prefixes belong at the GitHub boundary. */
|
||||
export function parseVersion(value: unknown) {
|
||||
if (typeof value !== 'string') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed = parse(value);
|
||||
|
||||
if (!parsed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const canonical = parsed.version + (parsed.build.length ? `+${parsed.build.join('.')}` : '');
|
||||
return value === canonical ? parsed : null;
|
||||
}
|
||||
|
||||
export function compareVersions(a: string, b: string): number {
|
||||
const left = parseVersion(a);
|
||||
const right = parseVersion(b);
|
||||
|
||||
if (!left || !right) {
|
||||
throw new TypeError('Invalid semantic version');
|
||||
}
|
||||
|
||||
return left.compare(right);
|
||||
}
|
||||
Reference in new issue
Block a user