Skip to content

Commit

Permalink
refactor: add actual semVerRegex and rename old one
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Nov 18, 2023
1 parent b6a5e4d commit 09fe555
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
13 changes: 5 additions & 8 deletions lib/helpers/regexs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// source: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
export const semVerRegex = new RegExp(
export const semVerRegexLine = new RegExp(
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/,
);
export const semVerRegex = new RegExp(
semVerRegexLine.source.replace(/[\^$]/g, ""),
);

export const regularVersionRegex = composeSemVerRegex(/\bversion:\s*"/, /"/);
export const attributeVersionRegex = composeSemVerRegex(/@version\s+"/, /"/);
Expand All @@ -17,11 +20,5 @@ export const allVersionRegexs = new RegExp(
* @returns {RegExp}
*/
function composeSemVerRegex(head, tail) {
return new RegExp(
head.source +
"(" +
semVerRegex.source.replace(/[\^$]/g, "") +
")" +
tail.source,
);
return new RegExp(head.source + "(" + semVerRegex.source + ")" + tail.source);
}
8 changes: 4 additions & 4 deletions lib/helpers/regexs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import {
allVersionRegexs,
attributeVersionRegex,
regularVersionRegex,
semVerRegex,
semVerRegexLine,
} from "./regexs";

describe("semVerRegex", () => {
describe("semVerRegexLine", () => {
it("should match valid values", () => {
// eslint-disable-next-line jest/prefer-expect-assertions
expect.assertions(validSemVers.length);

for (let semVer of validSemVers) {
expect(semVer).toMatch(new RegExp("^" + semVerRegex.source + "$"));
expect(semVer).toMatch(semVerRegexLine);
}
});

Expand All @@ -24,7 +24,7 @@ describe("semVerRegex", () => {
expect.assertions(invalidSemVers.length);

for (let semVer of invalidSemVers) {
expect(semVer).not.toMatch(new RegExp("^" + semVerRegex.source + "$"));
expect(semVer).not.toMatch(semVerRegexLine);
}
});
});
Expand Down

0 comments on commit 09fe555

Please sign in to comment.