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
@@ -8,6 +8,10 @@
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts",
|
||||
"exports": {
|
||||
"./semver": {
|
||||
"types": "./src/semver.ts",
|
||||
"default": "./src/semver.ts"
|
||||
},
|
||||
"./version": {
|
||||
"types": "./src/version.ts",
|
||||
"default": "./src/version.ts"
|
||||
@@ -95,13 +99,15 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "catalog:",
|
||||
"@types/semver": "7.7.1",
|
||||
"typescript": "catalog:",
|
||||
"vitest": "^4.1.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@zxcvbn-ts/core": "^4.1.2",
|
||||
"@zxcvbn-ts/language-common": "^4.1.3",
|
||||
"@zxcvbn-ts/language-en": "^4.1.1"
|
||||
"@zxcvbn-ts/language-en": "^4.1.1",
|
||||
"semver": "7.7.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=24"
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { compareVersions, parseVersion } from '../src/semver';
|
||||
|
||||
describe('parseVersion', () => {
|
||||
it.each([
|
||||
'0.0.0',
|
||||
'1.2.3',
|
||||
'1.2.3-rc.1',
|
||||
'1.2.3-alpha-beta',
|
||||
'1.2.3+001.build',
|
||||
'1.2.3-rc.1+build.2',
|
||||
])('accepts canonical SemVer and preserves its full value: %s', (version) => {
|
||||
expect(parseVersion(version)?.raw).toBe(version);
|
||||
});
|
||||
|
||||
it.each([
|
||||
undefined,
|
||||
null,
|
||||
123,
|
||||
{},
|
||||
'',
|
||||
'1.2',
|
||||
'1.2.3.4',
|
||||
'1.2.x',
|
||||
'01.2.3',
|
||||
'1.02.3',
|
||||
'1.2.03',
|
||||
'1.2.3-01',
|
||||
'1.2.3-rc.01',
|
||||
'1.2.3-rc..1',
|
||||
'1.2.3-rc_1',
|
||||
'1.2.3+build..1',
|
||||
'1.2.3+build_1',
|
||||
'v1.2.3',
|
||||
'V1.2.3',
|
||||
'=1.2.3',
|
||||
' 1.2.3',
|
||||
'1.2.3\n',
|
||||
])('rejects noncanonical input without coercion: %j', (version) => {
|
||||
expect(parseVersion(version)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('compareVersions', () => {
|
||||
it.each([
|
||||
['1.2.3', '1.2.3', 0],
|
||||
['1.2.4', '1.2.3', 1],
|
||||
['1.2.3', '1.2.4', -1],
|
||||
['1.10.0', '1.9.0', 1],
|
||||
['2.0.0', '1.9.9', 1],
|
||||
['3.0.0', '1.0.0', 1],
|
||||
['1.0.0', '3.0.0', -1],
|
||||
['1.2.3', '1.2.3-rc.1', 1],
|
||||
['1.2.3-rc.1', '1.2.3', -1],
|
||||
['1.2.3-rc.1', '1.2.3-rc.1', 0],
|
||||
['1.2.3-rc.1', '1.2.3-rc.2', -1],
|
||||
['1.2.3-rc.2', '1.2.3-rc.1', 1],
|
||||
['1.2.3-rc.10', '1.2.3-rc.2', 1],
|
||||
['1.2.3-rc.1+build.2', '1.2.3-rc.1+build.1', 0],
|
||||
['1.2.3-alpha-beta', '1.2.3-alpha', 1],
|
||||
['2.0.0-rc.1', '1.9.9', 1],
|
||||
] as const)('compareVersions(%j, %j) === %s', (a, b, expected) => {
|
||||
expect(compareVersions(a, b)).toBe(expected);
|
||||
});
|
||||
|
||||
it.each(['1.2', '1.2.3.9', '1.2.x', '', 'v1.2.3', 'V1.2.3', '01.2.3', '1.2.3-rc.01'])(
|
||||
'rejects invalid versions instead of guessing: %j',
|
||||
(version) => {
|
||||
expect(() => compareVersions(version, '1.2.3')).toThrow();
|
||||
expect(() => compareVersions('1.2.3', version)).toThrow();
|
||||
},
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user