Skip to content

Commit

Permalink
fix when emphasizing the empty string (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolsinga committed Jun 15, 2024
1 parent 86d6b3d commit 552914b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/Site/Utility/String+EmphasizedMatching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import RegexBuilder

extension String {
func emphasized(matching fragment: String) -> String {
guard !fragment.isEmpty else { return self }

let regex = Regex {
Capture {
fragment
Expand Down
8 changes: 8 additions & 0 deletions Tests/SiteTests/EmphasizedMatchingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ final class EmphasizedMatchingTests: XCTestCase {
XCTAssertEqual("Gregr".emphasized(matching: "Gr"), "**Gr**egr")
XCTAssertEqual("GregR".emphasized(matching: "gr"), "**Gr**egR")
}

func testNoMatches() throws {
XCTAssertEqual("Greg".emphasized(matching: "o"), "Greg")
}

func testEmptyStringMatches() throws {
XCTAssertEqual("Greg".emphasized(matching: ""), "Greg")
}
}

0 comments on commit 552914b

Please sign in to comment.