I need to compare two versions, the local, that is installed on the user's device, and the version, that is available in app stores. Currently, I am trying to achieve the desired result using library compareVersions. That works great, returns string 'new' if the remote version is greater. But I need a bit more other output that I am trying to get, for example, if local version is 1.0.5 and remote is 1.0.6 (changes the last number), then I need to return 'minor', and if local version is 1.0.5 and remote is 1.1.0 (changes the middle number and last), then return 'major'. The main idea for me is to get a different output in these cases, so based on version check I can update the UI accordingly. Any help highly appreciated!
I wrote once a function that compares two versions, you could adapt it to instead of returning a boolean return a string:
const DEFAULT_VERSION = '0.0.0';
export const isLatestGreaterThanCurrent = ({ latest = DEFAULT_VERSION, current = DEFAULT_VERSION } = {}) => {
const [latestMajor, latestMinor, latestPatch] = latest.split('.').map((s) => parseInt(s, 10));
const [currentMajor, currentMinor, currentPatch] = current.split('.').map((s) => parseInt(s, 10));
return (
latestMajor > currentMajor ||
(latestMajor === currentMajor && latestMinor > currentMinor) ||
(latestMajor === currentMajor && latestMinor === currentMinor && latestPatch > currentPatch)
);
};
and of course, its tests:
import { isLatestGreaterThanCurrent } from '../isLatestGreaterThanCurrent';
import each from "jest-each";
describe('isLatestGreaterThanCurrent', () => {
each([
// [latest, current]
['0.0.0', '0.0.0'],
['0.0.0', '0.0.1'],
['0.0.1', '0.1.0'],
['0.1.0', '0.1.0'],
['0.1.0', '0.1.1'],
['0.1.1', '1.0.0'],
['1.0.0', '1.0.0'],
['1.0.0', '1.0.1'],
['1.0.1', '1.1.0'],
['1.1.0', '1.1.0'],
['1.1.0', '1.1.1'],
['1.1.1', '1.1.1'],
]).test('latest %s is NOT greater than current %s', (latest, current) => {
expect(isLatestGreaterThanCurrent({latest, current})).toBeFalsy();
});
each([
// [latest, current]
['0.0.1', '0.0.0'],
['0.1.0', '0.0.1'],
['0.1.1', '0.1.0'],
['1.0.0', '0.1.1'],
['1.0.1', '1.0.0'],
['1.1.0', '1.0.0'],
['1.1.0', '1.0.1'],
['1.1.1', '1.1.0'],
]).test('latest %s is greater than current %s', (latest, current) => {
expect(isLatestGreaterThanCurrent({latest, current})).toBeTruthy();
})
});