Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wildcard): should compare as equal to anything #49

Merged
merged 1 commit into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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