From a0d2fda5dd9ef2511add185fd83685a9c41e95d1 Mon Sep 17 00:00:00 2001 From: rdalal Date: Wed, 10 Apr 2019 21:08:19 +0000 Subject: [PATCH] Bug 1543484 - Add version comparison to JEXL filter r=mythmon Differential Revision: https://phabricator.services.mozilla.com/D26975 --HG-- extra : moz-landing-system : lando --- toolkit/components/utils/FilterExpressions.jsm | 14 ++++++++++++++ .../utils/test/unit/test_FilterExpressions.js | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/toolkit/components/utils/FilterExpressions.jsm b/toolkit/components/utils/FilterExpressions.jsm index 773bd3bfe9db..c8375c53bc3c 100644 --- a/toolkit/components/utils/FilterExpressions.jsm +++ b/toolkit/components/utils/FilterExpressions.jsm @@ -4,6 +4,7 @@ "use strict"; +const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm"); ChromeUtils.defineModuleGetter(this, "PreferenceFilters", "resource://gre/modules/components-utils/PreferenceFilters.jsm"); @@ -25,6 +26,7 @@ XPCOMUtils.defineLazyGetter(this, "jexl", () => { length, mapToProperty, regExpMatch, + versionCompare, }); jexl.addBinaryOp("intersect", 40, operatorIntersect); return jexl; @@ -98,3 +100,15 @@ function regExpMatch(str, pattern, flags) { const re = new RegExp(pattern, flags); return str.match(re); } + + +/** + * Compares v1 to v2 and returns 0 if they are equal, a negative number if + * v1 < v2 or a positive number if v1 > v2. + * @param {string} v1 + * @param {string} v2 + * @return {number} + */ +function versionCompare(v1, v2) { + return Services.vc.compare(v1, v2); +} diff --git a/toolkit/components/utils/test/unit/test_FilterExpressions.js b/toolkit/components/utils/test/unit/test_FilterExpressions.js index 3bf262311444..dd20ce24fe88 100644 --- a/toolkit/components/utils/test/unit/test_FilterExpressions.js +++ b/toolkit/components/utils/test/unit/test_FilterExpressions.js @@ -305,3 +305,16 @@ add_task(async function test_regExpMatch() { "regExpMatch returns null if there are no matches", ); }); + +add_task(async function test_versionCompare() { + let val; + + val = await FilterExpressions.eval('"1.0.0"|versionCompare("1")'); + ok(val === 0); + + val = await FilterExpressions.eval('"1.0.0"|versionCompare("1.1")'); + ok(val < 0); + + val = await FilterExpressions.eval('"1.0.0"|versionCompare("0.1")'); + ok(val > 0); +});