From 4f9a0b00403630181d01ebed8ee849952e246e5b Mon Sep 17 00:00:00 2001 From: bimlas Date: Tue, 20 Mar 2018 19:01:09 +0100 Subject: [PATCH] Merge `findByTag()` and `findByWord()` into one Both looked for word in tags and content too, the only difference is when searched for `#tag`, the prefix (`#`) was truncated before compared with list of tags. --- browser/lib/search.js | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/browser/lib/search.js b/browser/lib/search.js index 13d7c736..46d47677 100644 --- a/browser/lib/search.js +++ b/browser/lib/search.js @@ -6,38 +6,28 @@ export default function searchFromNotes (notes, search) { let foundNotes = notes searchBlocks.forEach((block) => { - if (block.match(/^#.+/)) { - foundNotes = findByTag(foundNotes.slice(0), block).concat(findByWord(foundNotes.slice(0), block)) - } else { - foundNotes = findByWord(foundNotes, block) - } + foundNotes = findByWordOrTag(foundNotes, block) }) return foundNotes } -function findByTag (notes, block) { - const tag = block.match(/#(.+)/)[1] - const regExp = new RegExp(_.escapeRegExp(tag), 'i') - return notes.filter((note) => { - if (!_.isArray(note.tags)) return false - return note.tags.some((_tag) => { - return _tag.match(regExp) - }) - }) -} - -function findByWord (notes, block) { - const regExp = new RegExp(_.escapeRegExp(block), 'i') +function findByWordOrTag (notes, block) { + let tag = block + if (tag.match(/^#.+/)) { + tag = tag.match(/#(.+)/)[1] + } + const tagRegExp = new RegExp(_.escapeRegExp(tag), 'i') + const wordRegExp = new RegExp(_.escapeRegExp(block), 'i') return notes.filter((note) => { if (_.isArray(note.tags) && note.tags.some((_tag) => { - return _tag.match(regExp) + return _tag.match(tagRegExp) })) { return true } if (note.type === 'SNIPPET_NOTE') { - return note.description.match(regExp) + return note.description.match(wordRegExp) } else if (note.type === 'MARKDOWN_NOTE') { - return note.content.match(regExp) + return note.content.match(wordRegExp) } return false })