Skip to content

Commit

Permalink
add a method for getting the version upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Jul 21, 2020
1 parent 0700be6 commit 286c26c
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 15 deletions.
36 changes: 36 additions & 0 deletions src/getVersionUpgrade.ts
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;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import schema from './tokenlist.schema.json';

export * from './types';
export * from './isVersionUpdate';
export * from './getVersionUpgrade';

export { schema };
15 changes: 2 additions & 13 deletions src/isVersionUpdate.ts
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;
}
108 changes: 108 additions & 0 deletions test/getVersionUpgrade.test.ts
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);
});
});
4 changes: 2 additions & 2 deletions test/isVersionUpdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ describe('#isVersionUpdate', () => {
expect(
isVersionUpdate(
{ major: 2, minor: 1, patch: 1 },
{ major: 1, minor: 1, patch: 2 }
{ major: 2, minor: 1, patch: 2 }
)
).toEqual(false);
).toEqual(true);
});
});

0 comments on commit 286c26c

Please sign in to comment.