1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

refactor search function

This commit is contained in:
Sosuke Suzuki
2017-05-30 22:16:27 +09:00
parent 10e75041e8
commit 805c39e60c

View File

@@ -1,47 +1,33 @@
import _ from 'lodash'
export default function searchFromNotes(data, search) { export default function searchFromNotes(data, search) {
let notes = data.noteMap.map((note) => note) let notes = data.noteMap.map((note) => note)
if (search.trim().length === 0) return [] if (search.trim().length === 0) return []
let searchBlocks = search.split(' ') let searchBlocks = search.split(' ')
searchBlocks.forEach((block) => { searchBlocks.forEach((block) => {
if (block.match(/^!#.+/)) { if (block.match(/^#.+/)) {
let tag = block.match(/^!#(.+)/)[1] const tag = block.match(/#(.+)/)[1]
let regExp = new RegExp(_.escapeRegExp(tag), 'i') notes = findByTag(notes, tag)
notes = notes
.filter((note) => {
if (!_.isArray(note.tags)) return false
return note.tags.some((_tag) => {
return _tag.match(regExp)
})
})
} else if (block.match(/^!.+/)) {
let block = block.match(/^!(.+)/)[1]
let regExp = new RegExp(_.escapeRegExp(block), 'i')
notes = notes.filter((note) => {
if (!_.isArray(note.tags) || !note.tags.some((_tag) => {
return _tag.match(regExp)
})) {
return true
}
if (note.type === 'SNIPPET_NOTE') {
return !note.description.match(regExp)
} else if (note.type === 'MARKDOWN_NOTE') {
return !note.content.match(regExp)
}
return false
})
} else if (block.match(/^#.+/)) {
let tag = block.match(/#(.+)/)[1]
let regExp = new RegExp(_.escapeRegExp(tag), 'i')
notes = notes
.filter((note) => {
if (!_.isArray(note.tags)) return false
return note.tags.some((_tag) => {
return _tag.match(regExp)
})
})
} else { } else {
notes = findByWord(notes, block)
}
})
return notes
}
function findByTag(notes, tag) {
let 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) {
let regExp = new RegExp(_.escapeRegExp(block), 'i') let regExp = new RegExp(_.escapeRegExp(block), 'i')
notes = 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(regExp)
})) { })) {
@@ -54,9 +40,4 @@ export default function searchFromNotes(data, search) {
} }
return false return false
}) })
}
})
return notes
} }