Skip to content

Commit

Permalink
convert command name case improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kattouf committed Sep 26, 2024
1 parent 1643554 commit e91ac70
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/Sake/String+Case.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
extension String {
func toSnakeCase() -> String {
replacingOccurrences(of: "([a-z])([A-Z])", with: "$1_$2", options: .regularExpression)
.replacingOccurrences(of: "-", with: "_")
.lowercased()
}

func toKebabCase() -> String {
replacingOccurrences(of: "([a-z])([A-Z])", with: "$1-$2", options: .regularExpression)
.replacingOccurrences(of: "_", with: "-")
.lowercased()
}
}
42 changes: 42 additions & 0 deletions Tests/SakeTests/CommandNameCaseConverterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ final class CommandNameCaseConverterTests: XCTestCase {
]
let convertedCommands = CommandNameCaseConverter.convert(commands, strategy: .keepOriginal)
XCTAssertEqual(Set(convertedCommands.keys), ["commandOne", "commandTwo"])

let commands2: [String: Command] = [
"command_one": Command(description: "description1"),
"command_two": Command(description: "description2"),
]
let convertedCommands2 = CommandNameCaseConverter.convert(commands2, strategy: .keepOriginal)
XCTAssertEqual(Set(convertedCommands2.keys), ["command_one", "command_two"])

let commands3: [String: Command] = [
"command-one": Command(description: "description1"),
"command-two": Command(description: "description2"),
]
let convertedCommands3 = CommandNameCaseConverter.convert(commands3, strategy: .keepOriginal)
XCTAssertEqual(Set(convertedCommands3.keys), ["command-one", "command-two"])
}

func testToSnakeCaseStrategy() {
Expand All @@ -18,6 +32,20 @@ final class CommandNameCaseConverterTests: XCTestCase {
]
let convertedCommands = CommandNameCaseConverter.convert(commands, strategy: .toSnakeCase)
XCTAssertEqual(Set(convertedCommands.keys), ["command_one", "command_two"])

let commands2: [String: Command] = [
"command_one": Command(description: "description1"),
"command_two": Command(description: "description2"),
]
let convertedCommands2 = CommandNameCaseConverter.convert(commands2, strategy: .toSnakeCase)
XCTAssertEqual(Set(convertedCommands2.keys), ["command_one", "command_two"])

let commands3: [String: Command] = [
"command-one": Command(description: "description1"),
"command-two": Command(description: "description2"),
]
let convertedCommands3 = CommandNameCaseConverter.convert(commands3, strategy: .toSnakeCase)
XCTAssertEqual(Set(convertedCommands3.keys), ["command_one", "command_two"])
}

func testToKebabCaseStrategy() {
Expand All @@ -27,5 +55,19 @@ final class CommandNameCaseConverterTests: XCTestCase {
]
let convertedCommands = CommandNameCaseConverter.convert(commands, strategy: .toKebabCase)
XCTAssertEqual(Set(convertedCommands.keys), ["command-one", "command-two"])

let commands2: [String: Command] = [
"command_one": Command(description: "description1"),
"command_two": Command(description: "description2"),
]
let convertedCommands2 = CommandNameCaseConverter.convert(commands2, strategy: .toKebabCase)
XCTAssertEqual(Set(convertedCommands2.keys), ["command-one", "command-two"])

let commands3: [String: Command] = [
"command-one": Command(description: "description1"),
"command-two": Command(description: "description2"),
]
let convertedCommands3 = CommandNameCaseConverter.convert(commands3, strategy: .toKebabCase)
XCTAssertEqual(Set(convertedCommands3.keys), ["command-one", "command-two"])
}
}

0 comments on commit e91ac70

Please sign in to comment.