-
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 #12 from rkukuh/dev
Add camel case extension, its test cases, and update the readme
- Loading branch information
Showing
3 changed files
with
74 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 |
---|---|---|
|
@@ -31,6 +31,8 @@ These following methods are available for working with and manipulating `String` | |
|
||
- [`after`](https://github.com/rkukuh/Swift-StringHelper#after) | ||
- [`before`](https://github.com/rkukuh/Swift-StringHelper#before) | ||
- [`between`](https://github.com/rkukuh/Swift-StringHelper#between) | ||
- [`camel case`](https://github.com/rkukuh/Swift-StringHelper#camel-case) | ||
|
||
## Usage | ||
|
||
|
@@ -55,3 +57,25 @@ let result = originalString.before("@") | |
|
||
print(result) // john | ||
``` | ||
|
||
### `between` | ||
|
||
The `before` helper returns the portion of a string between two values. | ||
|
||
```swift | ||
let originalString = "[email protected]" | ||
let result = originalString.between("john@", and: ".com") | ||
|
||
print(result) // Optional("apple") | ||
``` | ||
|
||
### `camel case` | ||
|
||
The `camelCased` helper converts the given string to camelCaseLikeThis. | ||
|
||
```swift | ||
var originalString = "Everyone CAN Code" | ||
var result = originalString.camelCased() | ||
|
||
print(result) // everyoneCanCode | ||
``` |
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 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by R. Kukuh on 27/03/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension String { | ||
func camelCased() -> String { | ||
let words = self.components(separatedBy: CharacterSet.alphanumerics.inverted).filter { !$0.isEmpty } | ||
|
||
let firstWord = words.first?.lowercased() ?? "" | ||
let remainingWords = words.dropFirst().map { $0.capitalized } | ||
|
||
return ([firstWord] + remainingWords).joined() | ||
} | ||
} |
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 @@ | ||
// | ||
// StringCamelCasedTests.swift | ||
// | ||
// | ||
// Created by R. Kukuh on 27/03/23. | ||
// | ||
|
||
import XCTest | ||
@testable import SwiftStringHelper | ||
|
||
class StringCamelCasedTests: XCTestCase { | ||
|
||
func testCamelCased() { | ||
let originalString = "hello_world" | ||
let result = originalString.camelCased() | ||
XCTAssertEqual(result, "helloWorld", "The camel-cased string should be 'helloWorld'") | ||
} | ||
|
||
func testCamelCasedWithMultipleSeparators() { | ||
let originalString = "hello-world_example@text" | ||
let result = originalString.camelCased() | ||
XCTAssertEqual(result, "helloWorldExampleText", "The camel-cased string should be 'helloWorldExampleText'") | ||
} | ||
|
||
func testCamelCasedWhenEmpty() { | ||
let originalString = "" | ||
let result = originalString.camelCased() | ||
XCTAssertEqual(result, "", "The camel-cased string should be an empty string when the original string is empty") | ||
} | ||
|
||
} |