Skip to content

Commit

Permalink
Merge pull request #5 from rkukuh/dev
Browse files Browse the repository at this point in the history
Add `before` extension to String library and its test case
  • Loading branch information
rkukuh authored Mar 26, 2023
2 parents ae812db + ba5ee91 commit 1bc627b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/SwiftStringHelper/StringBeforeExtensions.swift
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
}
}

31 changes: 31 additions & 0 deletions Tests/SwiftStringHelperTests/StringBeforeTests.swift
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")
}

}

0 comments on commit 1bc627b

Please sign in to comment.