From 5f5a7880a661df9accb5a4483c18724b38535f0b Mon Sep 17 00:00:00 2001 From: bimlas Date: Mon, 19 Mar 2018 20:37:58 +0100 Subject: [PATCH] Fix tag filtering in search entry Some issues introduced in #954 (https://github.com/BoostIO/Boostnote/pull/954#issuecomment-336344915): - search by `#tag1 #tag2` returns the results of only `#tag2` - search by `#tag1 content` is as same as AND search by `#tag1 content` (and `#tag1` is regarded a word, not a tag) - search by `content #tag1` returns the results of only `#tag1` This commit fixing these: - search by `#tag1 #tag2` returns the results of only `#tag2` ![screencast](https://i.imgur.com/SjhQIhl.gif) - search by `#tag1 content` is as same as AND search by `#tag1 content` (and `#tag1` is regarded a word, not a tag) ![screencast](https://i.imgur.com/G0Tmd8c.gif) - search by `content #tag1` returns the results of only `#tag1` ![screencast](https://i.imgur.com/5MrMbE6.gif) NOTE: the examples works without `#` character too, because `findByWord()` checks the tags too. --- browser/lib/search.js | 7 ++++--- tests/lib/search-test.js | 7 +++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/browser/lib/search.js b/browser/lib/search.js index dadc9a29..d0cb376c 100644 --- a/browser/lib/search.js +++ b/browser/lib/search.js @@ -4,11 +4,12 @@ export default function searchFromNotes (notes, search) { if (search.trim().length === 0) return [] const searchBlocks = search.split(' ').filter(block => { return block !== '' }) - let foundNotes = findByWord(notes, searchBlocks[0]) + let foundNotes = notes searchBlocks.forEach((block) => { - foundNotes = findByWord(foundNotes, block) if (block.match(/^#.+/)) { - foundNotes = foundNotes.concat(findByTag(notes, block)) + foundNotes = findByTag(foundNotes, block) + } else { + foundNotes = findByWord(foundNotes, block) } }) return foundNotes diff --git a/tests/lib/search-test.js b/tests/lib/search-test.js index 1550a08f..c0cbf6b7 100644 --- a/tests/lib/search-test.js +++ b/tests/lib/search-test.js @@ -20,16 +20,19 @@ test.before(t => { notes = [note1, note2, note3] }) -test('it can find notes by tags or words', t => { +test('it can find notes by tags and words', t => { // [input, expected content (Array)] const testCases = [ ['#tag1', [note1.content, note2.content, note3.content]], ['#tag1 #tag2', [note2.content]], + ['#tag2 #tag1', [note2.content]], ['#tag1 #tag2 #tag3', []], ['content1', [note1.content, note2.content]], ['content1 content2', [note2.content]], ['content1 content2 content3', []], - ['#content4', [note3.content]] + ['#content4', [note3.content]], + ['#tag2 content1', [note2.content]], + ['content1 #tag2', [note2.content]] ] testCases.forEach((testCase) => {