Skip to content

Commit

Permalink
add a version comparator function
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Aug 25, 2020
1 parent 8f1f011 commit 839a7cd
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export * from './getVersionUpgrade';
export * from './diffTokenLists';
export * from './minVersionBump';
export * from './nextVersion';
export * from './versionComparator';

export { schema };
4 changes: 2 additions & 2 deletions src/isVersionUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getVersionUpgrade, VersionUpgrade } from './getVersionUpgrade';
import { versionComparator } from './versionComparator';
import { Version } from './types';

/**
* Returns true if versionB is an update over versionA
*/
export function isVersionUpdate(base: Version, update: Version): boolean {
return getVersionUpgrade(base, update) !== VersionUpgrade.NONE;
return versionComparator(base, update) < 0;
}
28 changes: 28 additions & 0 deletions src/versionComparator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Version } from 'types';

/**
* Comparator function that allows sorting version from lowest to highest
* @param versionA version A to compare
* @param versionB version B to compare
* @returns -1 if versionA comes before versionB, 0 if versionA is equal to version B, and 1 if version A comes after version B
*/
export function versionComparator(
versionA: Version,
versionB: Version
): -1 | 0 | 1 {
if (versionA.major < versionB.major) {
return -1;
} else if (versionA.major > versionB.major) {
return 1;
} else if (versionA.minor < versionB.minor) {
return -1;
} else if (versionA.minor > versionB.minor) {
return 1;
} else if (versionA.patch < versionB.patch) {
return -1;
} else if (versionA.patch > versionB.patch) {
return 1;
} else {
return 0;
}
}
108 changes: 108 additions & 0 deletions test/versionComparator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { versionComparator } from '../src';

describe('#versionComparator', () => {
it('major version', () => {
expect(
versionComparator(
{ major: 1, minor: 0, patch: 0 },
{ major: 2, minor: 0, patch: 0 }
)
).toEqual(-1);
expect(
versionComparator(
{ major: 1, minor: 0, patch: 0 },
{ major: 1, minor: 0, patch: 0 }
)
).toEqual(0);
expect(
versionComparator(
{ major: 1, minor: 0, patch: 0 },
{ major: 0, minor: 0, patch: 0 }
)
).toEqual(1);
expect(
versionComparator(
{ major: 1, minor: 1, patch: 0 },
{ major: 2, minor: 0, patch: 0 }
)
).toEqual(-1);
expect(
versionComparator(
{ major: 1, minor: 0, patch: 2 },
{ major: 1, minor: 0, patch: 1 }
)
).toEqual(1);
expect(
versionComparator(
{ major: 1, minor: 0, patch: 2 },
{ major: 1, minor: 0, patch: 2 }
)
).toEqual(0);
});

it('minor version', () => {
expect(
versionComparator(
{ major: 1, minor: 0, patch: 0 },
{ major: 1, minor: 1, patch: 0 }
)
).toEqual(-1);
expect(
versionComparator(
{ major: 1, minor: 0, patch: 0 },
{ major: 1, minor: 0, patch: 0 }
)
).toEqual(0);
expect(
versionComparator(
{ major: 1, minor: 1, patch: 0 },
{ major: 1, minor: 0, patch: 0 }
)
).toEqual(1);
expect(
versionComparator(
{ major: 1, minor: 1, patch: 1 },
{ major: 1, minor: 2, patch: 0 }
)
).toEqual(-1);
});

it('patch version', () => {
expect(
versionComparator(
{ major: 1, minor: 0, patch: 0 },
{ major: 1, minor: 1, patch: 0 }
)
).toEqual(-1);
expect(
versionComparator(
{ major: 1, minor: 0, patch: 0 },
{ major: 1, minor: 0, patch: 0 }
)
).toEqual(0);
expect(
versionComparator(
{ major: 1, minor: 1, patch: 0 },
{ major: 1, minor: 0, patch: 0 }
)
).toEqual(1);
expect(
versionComparator(
{ major: 1, minor: 1, patch: 1 },
{ major: 1, minor: 2, patch: 0 }
)
).toEqual(-1);
expect(
versionComparator(
{ major: 1, minor: 1, patch: 1 },
{ major: 2, minor: 1, patch: 1 }
)
).toEqual(-1);
expect(
versionComparator(
{ major: 2, minor: 1, patch: 1 },
{ major: 2, minor: 1, patch: 2 }
)
).toEqual(-1);
});
});

0 comments on commit 839a7cd

Please sign in to comment.