Skip to content

Commit

Permalink
fix(wildcard): should compare as equal to anything
Browse files Browse the repository at this point in the history
Based on testing done with semver.satisfies() where 3.3.3==3.x.x (not gt).
  • Loading branch information
omichelsen committed Nov 12, 2021
1 parent 2b5c00a commit d8ecb89
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
3 changes: 3 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const validateAndParseVersion = (v) => {
return match;
};

const isWildcard = (s) => s === '*' || s === 'x' || s === 'X';

const tryParse = (v) => {
const n = parseInt(v, 10);
return isNaN(n) ? v : n;
Expand All @@ -59,6 +61,7 @@ const forceType = (a, b) =>
typeof a !== typeof b ? [String(a), String(b)] : [a, b];

const compareStrings = (a, b) => {
if (isWildcard(a) || isWildcard(b)) return 0;
const [ap, bp] = forceType(tryParse(a), tryParse(b));
if (ap > bp) return 1;
if (ap < bp) return -1;
Expand Down
54 changes: 39 additions & 15 deletions test/compare.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ describe('compare versions', () => {
const runTests = (dataSet) => {
dataSet.forEach(([v1, v2, expected]) => {
it(`${v1} ${cmp[expected]} ${v2}`, () =>
assert.strictEqual(compareVersions(v1, v2), expected));
assert.strictEqual(
compareVersions(v1, v2),
expected,
`${v1} ${cmp[expected]} ${v2} !== ${expected}`
));
});
};

Expand Down Expand Up @@ -120,6 +124,33 @@ describe('compare versions', () => {
]);
});

describe('wildcards', () => {
runTests([
['3', '3.x.x', 0],
['3.3', '3.x.x', 0],
['3.3.3', '3.x.x', 0],
['3.x.x', '3.3.3', 0],
['3.3.3', '3.X.X', 0],
['3.3.3', '3.3.x', 0],
['3.3.3', '3.*.*', 0],
['3.3.3', '3.3.*', 0],
['3.0.3', '3.0.*', 0],
['0.7.x', '0.6.0', 1],
['0.7.x', '0.6.0-asdf', 1],
['0.7.x', '0.6.2', 1],
['0.7.x', '0.7.0-asdf', 1],
['1.2.*', '1.1.3', 1],
['1.2.*', '1.1.9999', 1],
['1.2.x', '1.0.0', 1],
['1.2.x', '1.1.0', 1],
['1.2.x', '1.1.3', 1],
['2.*.*', '1.0.1', 1],
['2.*.*', '1.1.3', 1],
['2.x.x', '1.0.0', 1],
['2.x.x', '1.1.3', 1],
]);
});

describe('invalid input', () => {
[
[42, /Invalid argument expected string/],
Expand All @@ -141,10 +172,6 @@ describe('compare versions', () => {
runTests([
['0.1.20', '0.1.5', 1],
['0.6.1-1', '0.6.1-0', 1],
['0.7.x', '0.6.0', 1],
['0.7.x', '0.6.0-asdf', 1],
['0.7.x', '0.6.2', 1],
['0.7.x', '0.7.0-asdf', 1],
['1', '0.0.0-beta', 1],
['1', '0.2.3', 1],
['1', '0.2.4', 1],
Expand All @@ -157,17 +184,10 @@ describe('compare versions', () => {
['1.0.0', '0.0.1', 1],
['1.0.0', '0.2.3', 1],
['1.0.0-beta.2', '1.0.0-beta.1', 1],
['1.2.*', '1.1.3', 1],
['1.2.*', '1.1.9999', 1],
['1.2.2', '1.2.1', 1],
['1.2.x', '1.0.0', 1],
['1.2.x', '1.1.0', 1],
['1.2.x', '1.1.3', 1],
['2', '1.0.0', 1],
['2', '1.0.0-beta', 1],
['2', '1.9999.9999', 1],
['2.*.*', '1.0.1', 1],
['2.*.*', '1.1.3', 1],
['2.0.0', '1.0.0', 1],
['2.0.0', '1.1.1', 1],
['2.0.0', '1.2.9', 1],
Expand All @@ -176,8 +196,6 @@ describe('compare versions', () => {
['2.3', '2.2.2', 1],
['2.4', '2.3.0', 1],
['2.4', '2.3.5', 1],
['2.x.x', '1.0.0', 1],
['2.x.x', '1.1.3', 1],
['3.2.1', '2.3.2', 1],
['3.2.1', '3.2.0', 1],
['v0.5.4-pre', '0.5.4-alpha', 1],
Expand All @@ -189,7 +207,11 @@ describe('human readable compare versions', () => {
const runTests = (dataSet) => {
dataSet.forEach(([v1, v2, operator, expected]) => {
it(`${v1} ${operator} ${v2}`, () =>
assert.strictEqual(compare(v1, v2, operator), expected));
assert.strictEqual(
compare(v1, v2, operator),
expected,
`${v1} ${operator} ${v2} !== ${expected}`
));
});
};

Expand Down Expand Up @@ -261,6 +283,8 @@ describe('human readable compare versions', () => {
['10.1.8', '10.0.4', '>=', true],
['10.0.1', '10.0.1', '=', true],
['10.0.1', '10.1.*', '=', false],
['3.3.3', '3.x.x', '<', false],
['3.3.3', '3.x.x', '>=', true],
['10.1.1', '10.2.2', '<', true],
['10.1.1', '10.0.2', '<', false],
['10.1.1', '10.2.2', '<=', true],
Expand Down

0 comments on commit d8ecb89

Please sign in to comment.