-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update logic to improve variableIdentifiers generated for strings con…
…taining accronyms (#116)
- Loading branch information
1 parent
a9c476b
commit f8f99ed
Showing
2 changed files
with
59 additions
and
4 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 |
---|---|---|
@@ -1,14 +1,28 @@ | ||
import Foundation | ||
|
||
private extension String { | ||
var lowercaseFirst: String { | ||
guard let first = unicodeScalars.first else { return self } | ||
return String(first).lowercased() + String(unicodeScalars.dropFirst()) | ||
func lowercaseFirst(_ n: Int) -> String { | ||
guard unicodeScalars.count >= n else { return self } | ||
return String(unicodeScalars.prefix(n)).lowercased() + String(unicodeScalars.dropFirst(n)) | ||
} | ||
} | ||
|
||
extension SwiftIdentifier { | ||
public static func variableIdentifier(for string: String) -> String { | ||
identifier(from: string).lowercaseFirst | ||
let identifier = identifier(from: string) | ||
let uppercasedPrefix = identifier.prefix(while: \.isUppercase) | ||
|
||
if uppercasedPrefix.isEmpty { | ||
return identifier | ||
} else if uppercasedPrefix.unicodeScalars.count == 1 { | ||
// i.e "Localizable" >> "localizable" or "MyStrings" >> "myStrings" | ||
return identifier.lowercaseFirst(1) | ||
} else if uppercasedPrefix.count == identifier.count { | ||
// i.e "LOCALIZABLE" >> "localizable" | ||
return identifier.lowercased() | ||
} else { | ||
// i.e "URLStrings" >> "urlStrings" or "URLStringCollection" >> "urlStringCollection" | ||
return identifier.lowercaseFirst(uppercasedPrefix.unicodeScalars.count - 1) | ||
} | ||
} | ||
} |
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,41 @@ | ||
import SwiftIdentifier | ||
import XCTest | ||
|
||
final class SwiftIdentifierTests: XCTestCase { | ||
func testSwiftIdentifier() { | ||
assert(value: "Localizable", escapesTo: "Localizable", "localizable") | ||
assert(value: "localizable", escapesTo: "Localizable", "localizable") | ||
assert(value: "LOCALIZABLE", escapesTo: "LOCALIZABLE", "localizable") | ||
|
||
assert(value: "URL Strings", escapesTo: "URLStrings", "urlStrings") | ||
assert(value: "URLStrings", escapesTo: "URLStrings", "urlStrings") | ||
|
||
assert(value: "FeatureFoo", escapesTo: "FeatureFoo", "featureFoo") | ||
assert(value: "Feature Foo", escapesTo: "FeatureFoo", "featureFoo") | ||
assert(value: "Feature URL", escapesTo: "FeatureURL", "featureURL") | ||
} | ||
|
||
private func assert( | ||
value: String, | ||
escapesTo expectedIdentifier: String, | ||
_ expectedVariableIdentifier: String, | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) { | ||
XCTAssertEqual( | ||
SwiftIdentifier.identifier(from: value), | ||
expectedIdentifier, | ||
"identifier", | ||
file: file, | ||
line: line | ||
) | ||
|
||
XCTAssertEqual( | ||
SwiftIdentifier.variableIdentifier(for: value), | ||
expectedVariableIdentifier, | ||
"variableIdentifier", | ||
file: file, | ||
line: line | ||
) | ||
} | ||
} |