Skip to content

Commit

Permalink
feat(semver): recognise ^6, >=5 etc as valid
Browse files Browse the repository at this point in the history
Closes #122
  • Loading branch information
JamieMason committed Feb 18, 2023
1 parent aa31244 commit be637f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/lib/is-semver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe('isSemver', () => {
expect(isSemver('^1.2.3')).toBeTrue();
expect(isSemver('>=1.2.3')).toBeTrue();
expect(isSemver('>1.2.3')).toBeTrue();
expect(isSemver('>1')).toBeTrue();
expect(isSemver('>=1')).toBeTrue();
expect(isSemver('^1')).toBeTrue();
expect(isSemver('*')).toBeFalse();
expect(isSemver('>=16.8.0 <17.0.0')).toBeFalse();
expect(isSemver('https://github.com/npm/npm.git')).toBeFalse();
Expand Down
18 changes: 12 additions & 6 deletions src/lib/is-semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ export function isValidSemverRange(
);
}

export function isSemver(version: unknown): boolean {
export function isSemver(version: unknown): version is string {
const range = '(~|\\^|>=|>|<=|<)?';
const ints = '[0-9]+';
const intsOrX = '([0-9]+|x)';
const dot = '\\.';
const major = new RegExp(`^${range}${ints}$`);
const minor = new RegExp(`^${range}${ints}${dot}${intsOrX}$`);
const patch = new RegExp(`^${range}${ints}${dot}${intsOrX}${dot}${intsOrX}$`);
return (
isString(version) &&
version.search(/^(~|\^|>=|>|<=|<|)?[0-9]+\.[0-9x]+\.[0-9x]+/) !== -1 &&
version.indexOf(' ') === -1
(version.search(major) !== -1 ||
version.search(minor) !== -1 ||
version.search(patch) !== -1)
);
}

export function isLooseSemver(version: unknown): boolean {
return (
isString(version) && isSemver(version) && version.search(/\.x(\.|$)/) !== -1
);
return isSemver(version) && version.search(/\.x(\.|$)/) !== -1;
}

0 comments on commit be637f0

Please sign in to comment.