34 lines
1.0 KiB
Kotlin
34 lines
1.0 KiB
Kotlin
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 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
|
|
)
|
|
}
|