Skip to content

Commit

Permalink
Merge pull request #12 from rkukuh/dev
Browse files Browse the repository at this point in the history
Add camel case extension, its test cases, and update the readme
  • Loading branch information
rkukuh authored Mar 27, 2023
2 parents bcae954 + dc57178 commit 24ec4c5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```
19 changes: 19 additions & 0 deletions Sources/SwiftStringHelper/StringCamelCaseExtensions.swift
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()
}
}
31 changes: 31 additions & 0 deletions Tests/SwiftStringHelperTests/StringCamelCasedTests.swift
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")
}

}

0 comments on commit 24ec4c5

Please sign in to comment.