diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift index 617995b9..c99d3464 100644 --- a/damus/Views/NoteContentView.swift +++ b/damus/Views/NoteContentView.swift @@ -346,3 +346,14 @@ struct TruncatedText: View { return AttributedString(truncatedAttributedString) + "..." } } + + +// trim suffix whitespace and newlines +func trim_suffix(_ str: String) -> String { + return str.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) +} + +// trim prefix whitespace and newlines +func trim_prefix(_ str: String) -> String { + return str.replacingOccurrences(of: "^\\s+", with: "", options: .regularExpression) +} diff --git a/damusTests/damusTests.swift b/damusTests/damusTests.swift index faa5299f..d89f68d3 100644 --- a/damusTests/damusTests.swift +++ b/damusTests/damusTests.swift @@ -40,6 +40,13 @@ class damusTests: XCTestCase { XCTAssertEqual(bytes.count, 32) } + func testTrimmingFunctions() { + let txt = " bobs " + + XCTAssertEqual(trim_prefix(txt), "bobs ") + XCTAssertEqual(trim_suffix(txt), " bobs") + } + func testParseMentionWithMarkdown() { let md = """ Testing markdown in damus