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.
add a method for getting the version upgrade
- Loading branch information
1 parent
0700be6
commit 286c26c
Showing
5 changed files
with
149 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Enum describing types of version differences | ||
*/ | ||
import { Version } from './types'; | ||
|
||
export enum VersionUpgrade { | ||
NONE, | ||
PATCH, | ||
MINOR, | ||
MAJOR, | ||
} | ||
|
||
/** | ||
* Return the upgrade type from the base version to the update version. | ||
* Note that downgrades and equivalent versions are both treated as `NONE`. | ||
* @param base base list | ||
* @param update update to the list | ||
*/ | ||
export function getVersionUpgrade( | ||
base: Version, | ||
update: Version | ||
): VersionUpgrade { | ||
if (update.major > base.major) { | ||
return VersionUpgrade.MAJOR; | ||
} | ||
if (update.major < base.major) { | ||
return VersionUpgrade.NONE; | ||
} | ||
if (update.minor > base.minor) { | ||
return VersionUpgrade.MINOR; | ||
} | ||
if (update.minor < base.minor) { | ||
return VersionUpgrade.NONE; | ||
} | ||
return update.patch > base.patch ? VersionUpgrade.PATCH : VersionUpgrade.NONE; | ||
} |
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,20 +1,9 @@ | ||
import { getVersionUpgrade, VersionUpgrade } from './getVersionUpgrade'; | ||
import { Version } from './types'; | ||
|
||
/** | ||
* Returns true if versionB is an update over versionA | ||
*/ | ||
export function isVersionUpdate(base: Version, update: Version): boolean { | ||
if (update.major > base.major) { | ||
return true; | ||
} | ||
if (update.major < base.major) { | ||
return false; | ||
} | ||
if (update.minor > base.minor) { | ||
return true; | ||
} | ||
if (update.minor < base.minor) { | ||
return false; | ||
} | ||
return update.patch > base.patch; | ||
return getVersionUpgrade(base, update) !== VersionUpgrade.NONE; | ||
} |
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 { getVersionUpgrade, VersionUpgrade } from '../src'; | ||
|
||
describe('#getVersionUpgrade', () => { | ||
it('major version', () => { | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 0 }, | ||
{ major: 2, minor: 0, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.MAJOR); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 0 }, | ||
{ major: 1, minor: 0, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.NONE); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 0 }, | ||
{ major: 0, minor: 0, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.NONE); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 1, patch: 0 }, | ||
{ major: 2, minor: 0, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.MAJOR); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 2 }, | ||
{ major: 1, minor: 0, patch: 1 } | ||
) | ||
).toEqual(VersionUpgrade.NONE); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 2 }, | ||
{ major: 1, minor: 0, patch: 2 } | ||
) | ||
).toEqual(VersionUpgrade.NONE); | ||
}); | ||
|
||
it('minor version', () => { | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 0 }, | ||
{ major: 1, minor: 1, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.MINOR); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 0 }, | ||
{ major: 1, minor: 0, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.NONE); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 1, patch: 0 }, | ||
{ major: 1, minor: 0, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.NONE); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 1, patch: 1 }, | ||
{ major: 1, minor: 2, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.MINOR); | ||
}); | ||
|
||
it('patch version', () => { | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 0 }, | ||
{ major: 1, minor: 1, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.MINOR); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 0, patch: 0 }, | ||
{ major: 1, minor: 0, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.NONE); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 1, patch: 0 }, | ||
{ major: 1, minor: 0, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.NONE); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 1, patch: 1 }, | ||
{ major: 1, minor: 2, patch: 0 } | ||
) | ||
).toEqual(VersionUpgrade.MINOR); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 1, minor: 1, patch: 1 }, | ||
{ major: 2, minor: 1, patch: 1 } | ||
) | ||
).toEqual(VersionUpgrade.MAJOR); | ||
expect( | ||
getVersionUpgrade( | ||
{ major: 2, minor: 1, patch: 1 }, | ||
{ major: 2, minor: 1, patch: 2 } | ||
) | ||
).toEqual(VersionUpgrade.PATCH); | ||
}); | ||
}); |
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