Skip to content

Commit

Permalink
fix: fix string formatting when there are multiple capitalised letter…
Browse files Browse the repository at this point in the history
…s together

Fix string case formatting.
  • Loading branch information
maticzav authored Nov 14, 2021
2 parents 5d75fc4 + 07523d5 commit 046a165
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions Sources/SwiftGraphQLCodegen/Extensions/String+Case.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,22 @@ extension String {
if lowerCaseRange.lowerBound == nextCharacterAfterDelimiter {
continue
} else {
// There was a range of >1 capital letters. Turn those into a word, stopping at the capital before the lower case character.
words.append(wordStart ..< lowerCaseRange.lowerBound)
// There was a range of >1 capital letters.

// If the next character after the capital letters is not a letter, turn all the capital letters into a word.
// Else turn all the capital letters up the second last index into a word.
if !self[lowerCaseRange.lowerBound].isLetter {
words.append(wordStart ..< lowerCaseRange.lowerBound)

// Next word starts after capital letters we just found
wordStart = lowerCaseRange.lowerBound
} else {
words.append(wordStart ..< index(before: lowerCaseRange.lowerBound))

// Next word starts at the last capital letters we just found
wordStart = index(before: lowerCaseRange.lowerBound)
}

// Next word starts at the capital before the lowercase we just found
wordStart = index(after: lowerCaseRange.lowerBound)
searchRange = wordStart ..< searchRange.upperBound
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testable import SwiftGraphQLCodegen
@testable import SwiftGraphQL
import XCTest

final class StringExtensionsTest: XCTestCase {
Expand All @@ -7,6 +7,7 @@ final class StringExtensionsTest: XCTestCase {
XCTAssertEqual("ENUM".camelCase, "enum")
XCTAssertEqual("linkToURL".camelCase, "linkToUrl")
XCTAssertEqual("grandfather_father.son grandson".camelCase, "grandfatherFatherSonGrandson")
XCTAssertEqual("GRAndFATHER_Father.son".camelCase, "grAndFatherFatherSon")
}

func testPascalCase() {
Expand Down

0 comments on commit 046a165

Please sign in to comment.