Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
React Native: Compare app versions and return major or minor strings

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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();
  })
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!