Skip to content

Commit

Permalink
Fix isVersionGreater and add tests (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
dahliasalem authored Apr 29, 2020
1 parent 4a334e0 commit 222f280
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
6 changes: 2 additions & 4 deletions app/Util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dayjs from 'dayjs';
import { Platform } from 'react-native';
import semver from 'semver';

export function isPlatformiOS() {
return Platform.OS === 'ios';
Expand All @@ -13,10 +14,7 @@ export function nowStr() {
return dayjs().format('H:mm');
}

export const isVersionGreater = (source, target) =>
source
.split('.')
.some((val, index) => parseInt(val) > parseInt(target.split('.')[index]));
export const isVersionGreater = (source, target) => semver.gt(source, target);

export default {
isPlatformiOS,
Expand Down
27 changes: 27 additions & 0 deletions app/__tests__/Util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { isVersionGreater } from '../Util';

describe('Util class', () => {
let versions;

beforeEach(() => {
versions = [
['0.0.1', '0.0.0', true],
['1.0.1', '0.9.9', true],
['3.0.0', '2.7.2+asdf', true],
['1.2.3-a.10', '1.2.3-a.5', true],
['1.2.3-a.b', '1.2.3-a', true],
['0.9.5', '1.0.1', false],
['0.9.9', '1.0.0', false],
['1.3.4', '1.3.5', false],
['2.9.1', '3.0.0', false],
['1.2.3', '1.2.3', false],
];
});

it('version greater than works', () => {
versions.forEach(([v0, v1, expectedValue]) => {
const result = isVersionGreater(v0, v1);
expect(result).toBe(expectedValue);
});
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"react-native-zip-archive": "^5.0.1",
"reanimated-bottom-sheet": "^1.0.0-alpha.19",
"rn-fetch-blob": "^0.12.0",
"semver": "^7.3.2",
"victory-native": "^34.1.0"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8536,6 +8536,11 @@ semver@^7.0.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-7.1.3.tgz#e4345ce73071c53f336445cfc19efb1c311df2a6"
integrity sha512-ekM0zfiA9SCBlsKa2X1hxyxiI4L3B6EbVJkkdgQXnSEEaHlGdvyodMruTiulSRWMMB4NeIuYNMC9rTKTz97GxA==

semver@^7.3.2:
version "7.3.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.2.tgz#604962b052b81ed0786aae84389ffba70ffd3938"
integrity sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==

[email protected]:
version "0.17.1"
resolved "https://registry.yarnpkg.com/send/-/send-0.17.1.tgz#c1d8b059f7900f7466dd4938bdc44e11ddb376c8"
Expand Down

0 comments on commit 222f280

Please sign in to comment.