[gnome-shell/wip/hadess/welcome-tour: 1/2] util: Add a GNOME version comparison function
- From: Bastien Nocera <hadess src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-shell/wip/hadess/welcome-tour: 1/2] util: Add a GNOME version comparison function
- Date: Thu, 4 Feb 2021 11:59:36 +0000 (UTC)
commit 5cf03be7b4f8ab8d8ab329fd893184db570b3b42
Author: Bastien Nocera <hadess hadess net>
Date: Thu Feb 4 12:26:15 2021 +0100
util: Add a GNOME version comparison function
Add a function that can compare GNOME versions, including the new naming
scheme for GNOME 40 and later.
Used in https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/3632
js/misc/util.js | 59 +++++++++++++++++++++++++++++++++++++++++++-
tests/meson.build | 2 +-
tests/unit/versionCompare.js | 49 ++++++++++++++++++++++++++++++++++++
3 files changed, 108 insertions(+), 2 deletions(-)
---
diff --git a/js/misc/util.js b/js/misc/util.js
index 2da489bbe6..9bcf6fe817 100644
--- a/js/misc/util.js
+++ b/js/misc/util.js
@@ -1,7 +1,7 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported findUrls, spawn, spawnCommandLine, spawnApp, trySpawnCommandLine,
formatTime, formatTimeSpan, createTimeLabel, insertSorted,
- ensureActorVisibleInScrollView, wiggle, lerp */
+ ensureActorVisibleInScrollView, wiggle, lerp, GNOMEversionCompare */
const { Clutter, Gio, GLib, Shell, St, GnomeDesktop } = imports.gi;
const Gettext = imports.gettext;
@@ -439,3 +439,60 @@ function wiggle(actor, params) {
function lerp(start, end, progress) {
return start + progress * (end - start);
}
+
+// _zeroPadArray:
+// @array: the array to pad
+// @len: the total length to pad to
+//
+// Pad the array with '0' elements, to simplify
+// version comparison with versions with a different
+// number of elements
+function _zeroPadArray(array, len) {
+ for (let i = array.length; i < len; i++)
+ array[i] = '0';
+}
+
+// _GNOMEversionToNumber:
+// @version: a GNOME version element
+//
+// Like Number() but returns sortable values for special-cases
+// 'alpha' and 'beta'. Returns NaN for unhandled 'versions'.
+function _GNOMEversionToNumber(version) {
+ let ret = Number(version);
+ if (!isNaN(ret))
+ return ret;
+ if (version === 'alpha')
+ return -2;
+ if (version === 'beta')
+ return -1;
+ return ret;
+}
+
+// GNOMEversionCompare:
+// @version1: a string containing a GNOME version
+// @version2: a string containing another GNOME version
+//
+// Returns an integer less than, equal to, or greater than
+// zero, if version1 is older, equal or newer than version2
+function GNOMEversionCompare(version1, version2) {
+ const v1Array = version1.split('.');
+ const v2Array = version2.split('.');
+
+ if (v1Array.length !== v2Array.length) {
+ if (v1Array.length > v2Array.length)
+ _zeroPadArray(v2Array, v1Array.length);
+ else
+ _zeroPadArray(v1Array, v2Array.length);
+ }
+
+ for (let i = 0; i < v1Array.length; i++) {
+ let elemV1 = _GNOMEversionToNumber(v1Array[i]);
+ let elemV2 = _GNOMEversionToNumber(v2Array[i]);
+ if (elemV1 < elemV2)
+ return -1;
+ if (elemV1 > elemV2)
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/tests/meson.build b/tests/meson.build
index 1e84f423ca..c0431631f0 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -10,7 +10,7 @@ run_test = configure_file(
testenv = environment()
testenv.set('GSETTINGS_SCHEMA_DIR', join_paths(meson.build_root(), 'data'))
-foreach test : ['insertSorted', 'jsParse', 'markup', 'params', 'url']
+foreach test : ['insertSorted', 'jsParse', 'markup', 'params', 'url', 'versionCompare']
test(test, run_test,
args: 'unit/@0@.js'.format(test),
env: testenv,
diff --git a/tests/unit/versionCompare.js b/tests/unit/versionCompare.js
new file mode 100644
index 0000000000..e45f533e24
--- /dev/null
+++ b/tests/unit/versionCompare.js
@@ -0,0 +1,49 @@
+// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
+
+// Test cases for version comparison
+
+const JsUnit = imports.jsUnit;
+
+const Environment = imports.ui.environment;
+Environment.init();
+
+const Util = imports.misc.util;
+
+const tests = [
+ { v1: '40',
+ v2: '40',
+ res: 0 },
+ { v1: '40',
+ v2: '42',
+ res: -1 },
+ { v1: '42',
+ v2: '40',
+ res: 1 },
+ { v1: '3.38.0',
+ v2: '40',
+ res: -1 },
+ { v1: '40',
+ v2: '3.38.0',
+ res: 1 },
+ { v1: '40',
+ v2: '3.38.0',
+ res: 1 },
+ { v1: '40.alpha.1.1',
+ v2: '40',
+ res: -1 },
+ { v1: '40.beta',
+ v2: '40',
+ res: -1 },
+ { v1: '40.1',
+ v2: '40',
+ res: 1 },
+ { v1: '',
+ v2: '40.alpha',
+ res: -1 },
+];
+
+for (let i = 0; i < tests.length; i++) {
+ print ('Test #' + i + ' v1: ' + tests[i].v1 + ', v2: ' + tests[i].v2);
+ JsUnit.assertEquals('Test #' + i + ' v1: ' + tests[i].v1 + ', v2: ' + tests[i].v2,
+ Util.GNOMEversionCompare (tests[i].v1, tests[i].v2), tests[i].res);
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]