React Native:比较应用程序版本和 return 主要或次要字符串
React Native: Compare app versions and return major or minor strings
我需要比较两个版本,一个是安装在用户设备上的本地版本,另一个是应用商店中提供的版本。目前,我正在尝试使用库 compareVersions 实现预期的结果。效果很好,如果远程版本更大,returns string 'new'。但是我需要更多我试图获得的其他输出,例如,如果本地版本是 1.0.5
而远程是 1.0.6
(更改最后一个数字),那么我需要 return 'minor',如果本地版本是 1.0.5
,远程版本是 1.1.0
(更改中间的数字和最后一个),则 return 'major'。我的主要想法是在这些情况下获得不同的输出,因此基于版本检查我可以相应地更新 UI。非常感谢任何帮助!
我曾经写过一个比较两个版本的函数,你可以调整它而不是 returning 一个布尔值 return 一个字符串:
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)
);
};
当然还有它的测试:
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();
})
});
我需要比较两个版本,一个是安装在用户设备上的本地版本,另一个是应用商店中提供的版本。目前,我正在尝试使用库 compareVersions 实现预期的结果。效果很好,如果远程版本更大,returns string 'new'。但是我需要更多我试图获得的其他输出,例如,如果本地版本是 1.0.5
而远程是 1.0.6
(更改最后一个数字),那么我需要 return 'minor',如果本地版本是 1.0.5
,远程版本是 1.1.0
(更改中间的数字和最后一个),则 return 'major'。我的主要想法是在这些情况下获得不同的输出,因此基于版本检查我可以相应地更新 UI。非常感谢任何帮助!
我曾经写过一个比较两个版本的函数,你可以调整它而不是 returning 一个布尔值 return 一个字符串:
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)
);
};
当然还有它的测试:
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();
})
});