1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00

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.
This commit is contained in:
bimlas
2018-03-20 19:01:09 +01:00
parent aae584106a
commit 4f9a0b0040

View File

@@ -6,38 +6,28 @@ export default function searchFromNotes (notes, search) {
let foundNotes = notes let foundNotes = notes
searchBlocks.forEach((block) => { searchBlocks.forEach((block) => {
if (block.match(/^#.+/)) { foundNotes = findByWordOrTag(foundNotes, block)
foundNotes = findByTag(foundNotes.slice(0), block).concat(findByWord(foundNotes.slice(0), block))
} else {
foundNotes = findByWord(foundNotes, block)
}
}) })
return foundNotes return foundNotes
} }
function findByTag (notes, block) { function findByWordOrTag (notes, block) {
const tag = block.match(/#(.+)/)[1] let tag = block
const regExp = new RegExp(_.escapeRegExp(tag), 'i') if (tag.match(/^#.+/)) {
return notes.filter((note) => { tag = tag.match(/#(.+)/)[1]
if (!_.isArray(note.tags)) return false }
return note.tags.some((_tag) => { const tagRegExp = new RegExp(_.escapeRegExp(tag), 'i')
return _tag.match(regExp) const wordRegExp = new RegExp(_.escapeRegExp(block), 'i')
})
})
}
function findByWord (notes, block) {
const regExp = new RegExp(_.escapeRegExp(block), 'i')
return notes.filter((note) => { return notes.filter((note) => {
if (_.isArray(note.tags) && note.tags.some((_tag) => { if (_.isArray(note.tags) && note.tags.some((_tag) => {
return _tag.match(regExp) return _tag.match(tagRegExp)
})) { })) {
return true return true
} }
if (note.type === 'SNIPPET_NOTE') { if (note.type === 'SNIPPET_NOTE') {
return note.description.match(regExp) return note.description.match(wordRegExp)
} else if (note.type === 'MARKDOWN_NOTE') { } else if (note.type === 'MARKDOWN_NOTE') {
return note.content.match(regExp) return note.content.match(wordRegExp)
} }
return false return false
}) })