Add search terms parser + tests
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package be.simplenotes.app.utils
|
||||
|
||||
import be.simplenotes.search.SearchTerms
|
||||
|
||||
private val titleRe = """title:['"](?<title>.*?)['"]""".toRegex()
|
||||
private val outerTitleRe = """(?<title>title:['"].*?['"])""".toRegex()
|
||||
|
||||
private val tagRe = """tag:['"](?<tag>.*?)['"]""".toRegex()
|
||||
private val outerTagRe = """(?<tag>tag:['"].*?['"])""".toRegex()
|
||||
|
||||
fun parseSearchTerms(input: String): SearchTerms {
|
||||
val title: String? = titleRe.find(input)?.groups?.get("title")?.value
|
||||
val tag: String? = tagRe.find(input)?.groups?.get("tag")?.value
|
||||
var c: String = input
|
||||
|
||||
if (title != null) {
|
||||
val titleGroup = outerTitleRe.find(input)?.groups?.get("title")?.value
|
||||
titleGroup?.let { c = c.replace(it, "") }
|
||||
}
|
||||
|
||||
if (tag != null) {
|
||||
val tagGroup = outerTagRe.find(input)?.groups?.get("tag")?.value
|
||||
tagGroup?.let { c = c.replace(it, "") }
|
||||
}
|
||||
|
||||
val content = c.trim().ifEmpty { null }
|
||||
|
||||
return SearchTerms(
|
||||
title = title,
|
||||
tag = tag,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user