Skip to content

Commit

Permalink
Update logic to improve variableIdentifiers generated for strings con…
Browse files Browse the repository at this point in the history
…taining accronyms (#116)
  • Loading branch information
liamnichols authored Oct 13, 2024
1 parent a9c476b commit f8f99ed
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Sources/SwiftIdentifier/SwiftIdentifier+Extra.swift
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)
}
}
}
41 changes: 41 additions & 0 deletions Tests/XCStringsToolTests/SwiftIdentifierTests.swift
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
)
}
}

0 comments on commit f8f99ed

Please sign in to comment.