Home > other >  React Native: Compare app versions and return major or minor strings
React Native: Compare app versions and return major or minor strings

Time:05-27

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!

CodePudding user response:

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();
  })
});

CodePudding user response:

I would split the versions into an array of numbers. Then compare each element of version A with the element of version B on the same index. Stop when you find the first number that doesn't match. Then use the index of the non-matching element to return a result

// Zip arrays together.
//
//     zip([1,2,3], [4,5,6]) //=> [[1,4], [2,5], [3,6]]
//
function zip(...arrays) {
  if (!arrays.length) return;
  const length = Math.max(...arrays.map(array => array.length));
  return Array.from({ length }, (_, i) => arrays.map(array => array[i]));
}

// Convert a semantic version into an array of sub-versions.
//
//     versionToArray("1.2.3") //=> [1, 2, 3]
//
function versionToArray(version) {
  return version.split(".").map(subVersion => parseInt(subVersion, 10));
}

function versionDiff(versionA, versionB) {
  const versions = ["superMajor", "major", "minor", "patch"];
  
  versionA = versionToArray(versionA);
  versionB = versionToArray(versionB);
  
  const index = zip(versionA, versionB).findIndex(([a, b]) => a != b);
  return versions[index] || "equal";
}

console.log(versionDiff("1.0.5", "1.0.6"));
console.log(versionDiff("1.0.5", "1.1.0"));

  • Related