Commit 5e9d9eb9 authored by Kyle Keating's avatar Kyle Keating Committed by Kyle Keating

Fix tag service to work with ASCII chars

The regex-ex was not capturing words at the start of a line which had ASCII characters.

This fix first splits the string based on words and searches for any match and then additionally checks the entire string against the query
parent ede3280f
......@@ -41,12 +41,15 @@ export class TagsService {
// (e.g. tag "pink banana" matches query "ban"), OR
// * tag has substring query occurring after a non-word character
// (e.g. tag "pink!banana" matches query "ban")
let regex = new RegExp('(\\W|\\b)' + query, 'i');
return savedTags.filter(tag => {
if (tag.match(regex)) {
if (limit === null || resultCount < limit) {
// limit allows a subset of the results
// See https://github.com/hypothesis/client/issues/1606
const words = tag.split(/\W+/);
let regex = new RegExp(`^${query}`, 'i'); // Only match the start of the string
// limit allows a subset of the results
// See https://github.com/hypothesis/client/issues/1606
if (limit === null || resultCount < limit) {
const matches =
words.some(word => word.match(regex)) || tag.match(regex);
if (matches) {
++resultCount;
return true;
}
......
......@@ -56,6 +56,12 @@ describe('TagsService', () => {
count: 1,
updated: stamp,
},
'žŸ¡¢£¤¥¦§': {
// ASCII chars
text: 'žŸ¡¢£¤¥¦§',
count: 1,
updated: stamp,
},
};
const savedTagsList = Object.keys(savedTagsMap);
......@@ -83,6 +89,10 @@ describe('TagsService', () => {
assert.deepEqual(tags.filter('b', 2), ['bar', 'bar argon']);
assert.deepEqual(tags.filter('b', 3), ['bar', 'bar argon', 'banana']);
});
it('returns ASCII tags that start with the query string', () => {
assert.deepEqual(tags.filter('žŸ¡'), ['žŸ¡¢£¤¥¦§']);
});
});
describe('#store', () => {
......@@ -122,6 +132,7 @@ describe('TagsService', () => {
'bar argon',
'future',
'argon',
'žŸ¡¢£¤¥¦§',
]);
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment