From 07523d5eb14e258dd7c6e64f297b4cf48aca98d4 Mon Sep 17 00:00:00 2001 From: Romy Date: Thu, 30 Sep 2021 23:33:35 +0800 Subject: [PATCH] Fix string case format --- .../Extensions/String+Case.swift | 19 +++++++++++++++---- .../Extensions/String+CaseTests.swift | 3 ++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftGraphQLCodegen/Extensions/String+Case.swift b/Sources/SwiftGraphQLCodegen/Extensions/String+Case.swift index e01b6def..ad802d5a 100644 --- a/Sources/SwiftGraphQLCodegen/Extensions/String+Case.swift +++ b/Sources/SwiftGraphQLCodegen/Extensions/String+Case.swift @@ -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 } } diff --git a/Tests/SwiftGraphQLCodegenTests/Extensions/String+CaseTests.swift b/Tests/SwiftGraphQLCodegenTests/Extensions/String+CaseTests.swift index d8950a2a..8e00bf10 100644 --- a/Tests/SwiftGraphQLCodegenTests/Extensions/String+CaseTests.swift +++ b/Tests/SwiftGraphQLCodegenTests/Extensions/String+CaseTests.swift @@ -1,4 +1,4 @@ -@testable import SwiftGraphQLCodegen +@testable import SwiftGraphQL import XCTest final class StringExtensionsTest: XCTestCase { @@ -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() {