about summary refs log tree commit diff
path: root/src/lib/strings/helpers.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2024-01-23 15:52:34 -0800
committerPaul Frazee <pfrazee@gmail.com>2024-01-23 15:52:34 -0800
commit68a439d5705f6e1bf7ae89377347d6b6db05d6e8 (patch)
tree624306bb3f69ca22c6207a1af8e48db501a83737 /src/lib/strings/helpers.ts
parent6076b8f73056a7cc04904bd0aa4571b17fd4978d (diff)
parent7f4d3dc999b130cd8552ed97c0c90cf83cbdd13c (diff)
downloadvoidsky-68a439d5705f6e1bf7ae89377347d6b6db05d6e8.tar.zst
Merge branch 'feat/search-from-me' of https://github.com/mary-ext/fork-bsky-app into mary-ext-feat/search-from-me
Diffstat (limited to 'src/lib/strings/helpers.ts')
-rw-r--r--src/lib/strings/helpers.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/lib/strings/helpers.ts b/src/lib/strings/helpers.ts
index 381ae32f3..e2abe9019 100644
--- a/src/lib/strings/helpers.ts
+++ b/src/lib/strings/helpers.ts
@@ -37,3 +37,27 @@ export function countLines(str: string | undefined): number {
   if (!str) return 0
   return str.match(/\n/g)?.length ?? 0
 }
+
+// Augments search query with additional syntax like `from:me`
+export function augmentSearchQuery(query: string, {did}: {did?: string}) {
+  // Don't do anything if there's no DID
+  if (!did) {
+    return query
+  }
+
+  // We don't want to replace substrings that are being "quoted" because those
+  // are exact string matches, so what we'll do here is to split them apart
+
+  // Even-indexed strings are unquoted, odd-indexed strings are quoted
+  const splits = query.split(/("(?:[^"\\]|\\.)*")/g)
+
+  return splits
+    .map((str, idx) => {
+      if (idx % 2 === 0) {
+        return str.replaceAll(/(^|\s)from:me(\s|$)/g, `$1${did}$2`)
+      }
+
+      return str
+    })
+    .join('')
+}