-
Notifications
You must be signed in to change notification settings - Fork 2
/
is_version_greater_than.dart
55 lines (51 loc) · 1.39 KB
/
is_version_greater_than.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// Parse and compare [newVersion] with [currentVersion].
///
/// If [newVersion] is greater than [currentVersion], return `true`, otherwise
/// return `false`.
///
/// The expected format is `x.y.z` where `x`, `y` and `z` are integers.
///
/// Example:
///
/// ```dart
/// String a = "1.39.2";
/// String b = "1.38.14";
///
/// isVersionGreaterThan(newVersion: a, currentVersion: b); // true
/// ```
bool isVersionGreaterThan({
required String newVersion,
required String currentVersion,
}) {
final _currentVersion = currentVersion.split('.');
final _newVersion = newVersion.split('.');
bool isGreater = false;
for (int i = 0; i < 3; i++) {
final newVersionI = int.parse(_newVersion[i]);
final currentVersionI = int.parse(_currentVersion[i]);
isGreater = newVersionI > currentVersionI;
if (newVersionI != currentVersionI) break;
}
return isGreater;
}
// Test it
void main() {
test("should return false if version is inferior", () {
expect(
isVersionGreaterThan(newVersion: "1.0.0", currentVersion: "1.0.1"),
false,
);
});
test("should return false if version is equal", () {
expect(
isVersionGreaterThan(newVersion: "1.0.0", currentVersion: "1.0.0"),
false,
);
});
test("should return true if version is greater", () {
expect(
isVersionGreaterThan(newVersion: "1.0.1", currentVersion: "1.0.0"),
true,
);
});
}