forked from 1Hive/token-lists
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f1f011
commit 839a7cd
Showing
4 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |