Skip to content

Commit 908fd5c

Browse files
psaizzzacharo
authored andcommitted
selector: check the corner case when one value starts with the same string as another value
1 parent 9a3ce63 commit 908fd5c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/lib/state/selectors/query.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ import _pick from "lodash/pick";
1818
* @param {string} second a string
1919
*/
2020
function startsWith(first, second) {
21-
return first.indexOf(second) === 0;
21+
/* This is a bit tricky, since the verification should distinguish between the label and the children
22+
In other words:
23+
* 'q=type:hello&subtype:there' (or 'q=type:hello') starts with 'q=type:hello'
24+
* 'q=type:hello there' should not start with 'q=type:hello'
25+
So the check verifies that both terms are the same length, or that there is an '&' as soon as the second finishes
26+
*/
27+
return (
28+
first.indexOf(second) === 0 &&
29+
(first.length === second.length || first.indexOf(second + "&") === 0)
30+
);
2231
}
2332

2433
function toString(array) {

src/lib/state/selectors/query.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ describe("queries with first level filters.", () => {
7272
["type", "Publication"],
7373
]);
7474
});
75+
76+
test("query with terms that start with the same text", () => {
77+
const state = [
78+
["file_type", "pdf"],
79+
["file_type", "pdf2"],
80+
];
81+
const query = ["file_type", "pdf"];
82+
83+
const newState = updateQueryFilters(query, state);
84+
85+
expect(newState).toEqual([["file_type", "pdf2"]]);
86+
});
7587
});
7688

7789
describe("queries with second level filters.", () => {
@@ -142,6 +154,17 @@ describe("queries with second level filters.", () => {
142154
["type", "Publication"],
143155
]);
144156
});
157+
test("query with terms that start with the same text and with children", () => {
158+
const state = [
159+
["file_type", "pdf", ["sub_type", "image"]],
160+
["file_type", "pdf2"],
161+
];
162+
const query = ["file_type", "pdf"];
163+
164+
const newState = updateQueryFilters(query, state);
165+
166+
expect(newState).toEqual([["file_type", "pdf2"]]);
167+
});
145168
});
146169

147170
describe("queries with third level filters.", () => {

0 commit comments

Comments
 (0)