Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda