From 4c944626355c4dbc8d39fd8cdbd5fb4da5e323d8 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Mon, 23 May 2022 19:36:59 +0200 Subject: [PATCH] Allow search datasets by name tag using `#` Also allow search by `contains` instead of `equals` tag name match --- .../src/store/historyStore/model/filtering.js | 20 +++++++++++++++++-- .../historyStore/model/filtering.test.js | 9 ++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/client/src/store/historyStore/model/filtering.js b/client/src/store/historyStore/model/filtering.js index 984bd2d1f96..589dd874cc9 100644 --- a/client/src/store/historyStore/model/filtering.js +++ b/client/src/store/historyStore/model/filtering.js @@ -30,10 +30,19 @@ function toLowerNoQuotes(value) { return toLower(value).replaceAll("'", ""); } +/** Converts name tags starting with '#' to 'name:' */ +function expandNameTag(value) { + if (value && typeof value === "string" && value.startsWith("#")) { + value = value.replace("#", "name:"); + } + return toLower(value); +} + /** * Checks if a query value is equal to the item value * @param {*} attribute of the content item * @param {*} query parameter if the attribute does not match the server query key + * @param {*} converter if item attribute value has to be transformed e.g. to a date. */ function equals(attribute, query = null, converter = null) { return { @@ -49,12 +58,19 @@ function equals(attribute, query = null, converter = null) { /** * Checks if a query value is part of the item value * @param {*} attribute of the content item + * @param {*} query parameter if the attribute does not match the server query key + * @param {*} converter if item attribute value has to be transformed e.g. to a date. */ -function contains(attribute, query = null) { +function contains(attribute, query = null, converter = null) { return { attribute, + converter, query: query || `${attribute}-contains`, handler: (v, q) => { + if (converter) { + v = converter(v); + q = converter(q); + } return toLower(v).includes(toLower(q)); }, }; @@ -107,7 +123,7 @@ const validFilters = { visible: equals("visible", "visible", toBool), name: contains("name"), state: equals("state"), - tag: contains("tags", "tag"), + tag: contains("tags", "tag-contains", expandNameTag), update_time: compare("update_time", "le", toDate), update_time_ge: compare("update_time", "ge", toDate), update_time_gt: compare("update_time", "gt", toDate), diff --git a/client/src/store/historyStore/model/filtering.test.js b/client/src/store/historyStore/model/filtering.test.js index 51c23cefd3f..187368f0e0d 100644 --- a/client/src/store/historyStore/model/filtering.test.js +++ b/client/src/store/historyStore/model/filtering.test.js @@ -54,7 +54,7 @@ describe("filtering", () => { expect(queryDict["update_time-lt"]).toBe(1640995200); expect(queryDict["state-eq"]).toBe("success"); expect(queryDict["extension-eq"]).toBe("ext"); - expect(queryDict["tag"]).toBe("first"); + expect(queryDict["tag-contains"]).toBe("first"); expect(queryDict["deleted"]).toBe(false); expect(queryDict["visible"]).toBe(true); }); @@ -106,4 +106,11 @@ describe("filtering", () => { expect(toAlias(getFilters(filterTexts[index]))).toEqual(parsedFilterSettings); }); }); + test("named tag (hash) conversion", () => { + const filters = getFilters("tag=#test"); + expect(filters[0][0]).toBe("tag"); + expect(filters[0][1]).toBe("#test"); + const queryDict = getQueryDict("tag=#test"); + expect(queryDict["tag-contains"]).toBe("name:test"); + }); });