-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from rkukuh/dev
Add `before` extension to String library and its test case
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// StringBeforeExtensions.swift | ||
// | ||
// | ||
// Created by R. Kukuh on 26/03/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension String { | ||
func before(_ value: String) -> String { | ||
if let range = self.range(of: value) { | ||
return String(self[..<range.lowerBound]) | ||
} | ||
|
||
return self | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// StringBeforeTests.swift | ||
// | ||
// | ||
// Created by R. Kukuh on 26/03/23. | ||
// | ||
|
||
import XCTest | ||
@testable import SwiftStringHelper | ||
|
||
class StringBeforeTests: XCTestCase { | ||
|
||
func testBefore() { | ||
let originalString = "[email protected]" | ||
let result = originalString.before("@") | ||
XCTAssertEqual(result, "hello", "The string before '@' should be 'hello'") | ||
} | ||
|
||
func testBeforeWhenNotFound() { | ||
let originalString = "[email protected]" | ||
let result = originalString.before("#") | ||
XCTAssertEqual(result, originalString, "The string before '#' should be the same as the original string when '#' is not found") | ||
} | ||
|
||
func testBeforeWhenEmpty() { | ||
let originalString = "" | ||
let result = originalString.before("@") | ||
XCTAssertEqual(result, originalString, "The string before '@' should be the same as the original string when the original string is empty") | ||
} | ||
|
||
} |