Search now apply to all fields by default
This commit is contained in:
@@ -2,32 +2,40 @@ package be.simplenotes.app.utils
|
||||
|
||||
import be.simplenotes.domain.usecases.search.SearchTerms
|
||||
|
||||
private val titleRe = """title:['"](?<title>.*?)['"]""".toRegex()
|
||||
private val outerTitleRe = """(?<title>title:['"].*?['"])""".toRegex()
|
||||
private fun innerRegex(name: String) = """$name:['"](.*?)['"]""".toRegex()
|
||||
private fun outerRegex(name: String) = """($name:['"].*?['"])""".toRegex()
|
||||
|
||||
private val tagRe = """tag:['"](?<tag>.*?)['"]""".toRegex()
|
||||
private val outerTagRe = """(?<tag>tag:['"].*?['"])""".toRegex()
|
||||
private val titleRe = innerRegex("title")
|
||||
private val outerTitleRe = outerRegex("title")
|
||||
|
||||
private val tagRe = innerRegex("tag")
|
||||
private val outerTagRe = outerRegex("tag")
|
||||
|
||||
private val contentRe = innerRegex("content")
|
||||
private val outerContentRe = outerRegex("content")
|
||||
|
||||
fun parseSearchTerms(input: String): SearchTerms {
|
||||
val title: String? = titleRe.find(input)?.groups?.get(1)?.value
|
||||
val tag: String? = tagRe.find(input)?.groups?.get(1)?.value
|
||||
var c: String = input
|
||||
|
||||
if (title != null) {
|
||||
val titleGroup = outerTitleRe.find(input)?.groups?.get(1)?.value
|
||||
titleGroup?.let { c = c.replace(it, "") }
|
||||
fun extract(innerRegex: Regex, outerRegex: Regex): String? {
|
||||
val match = innerRegex.find(input)?.groups?.get(1)?.value
|
||||
if (match != null) {
|
||||
val group = outerRegex.find(input)?.groups?.get(1)?.value
|
||||
group?.let { c = c.replace(it, " ") }
|
||||
}
|
||||
return match
|
||||
}
|
||||
|
||||
if (tag != null) {
|
||||
val tagGroup = outerTagRe.find(input)?.groups?.get(1)?.value
|
||||
tagGroup?.let { c = c.replace(it, "") }
|
||||
}
|
||||
val title: String? = extract(titleRe, outerTitleRe)
|
||||
val tag: String? = extract(tagRe, outerTagRe)
|
||||
val content: String? = extract(contentRe, outerContentRe)
|
||||
|
||||
val content = c.trim().ifEmpty { null }
|
||||
val all = c.trim().ifEmpty { null }
|
||||
|
||||
return SearchTerms(
|
||||
title = title,
|
||||
tag = tag,
|
||||
content = content
|
||||
content = content,
|
||||
all = all
|
||||
)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ internal class SearchTermsParserKtTest {
|
||||
title: String? = null,
|
||||
tag: String? = null,
|
||||
content: String? = null,
|
||||
): Pair<String, SearchTerms> = input to SearchTerms(title, tag, content)
|
||||
all: String? = null
|
||||
): Pair<String, SearchTerms> = input to SearchTerms(title, tag, content, all)
|
||||
|
||||
@Suppress("Unused")
|
||||
private fun results() = Stream.of(
|
||||
@@ -21,13 +22,13 @@ internal class SearchTermsParserKtTest {
|
||||
createResult("title:'example with words'", title = "example with words"),
|
||||
createResult("title:'example with words'", title = "example with words"),
|
||||
createResult("""title:"double quotes"""", title = "double quotes"),
|
||||
createResult("title:'example' something else", title = "example", content = "something else"),
|
||||
createResult("title:'example' something else", title = "example", all = "something else"),
|
||||
createResult("tag:'example'", tag = "example"),
|
||||
createResult("tag:'example' title:'other'", title = "other", tag = "example"),
|
||||
createResult("blah blah tag:'example' title:'other'", title = "other", tag = "example", content = "blah blah"),
|
||||
createResult("tag:'example' middle title:'other'", title = "other", tag = "example", content = "middle"),
|
||||
createResult("tag:'example' title:'other' end", title = "other", tag = "example", content = "end"),
|
||||
createResult("tag:'example abc' title:'other with words' this is the end ", title = "other with words", tag = "example abc", content = "this is the end"),
|
||||
createResult("blah blah tag:'example' title:'other'", title = "other", tag = "example", all = "blah blah"),
|
||||
createResult("tag:'example' middle title:'other'", title = "other", tag = "example", all = "middle"),
|
||||
createResult("tag:'example' title:'other' end", title = "other", tag = "example", all = "end"),
|
||||
createResult("tag:'example abc' title:'other with words' this is the end ", title = "other with words", tag = "example abc", all = "this is the end"),
|
||||
)
|
||||
|
||||
@ParameterizedTest
|
||||
|
||||
Reference in New Issue
Block a user