Skip to content

Commit

Permalink
rework pretty filter a bit as discussed
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed Aug 19, 2017
1 parent 5dfe056 commit 418a2bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Documentation/filters-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,21 @@ https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift
| `some$URL` | `Some_URL` |
| `25 Ultra Light` | `_25_Ultra_Light` |
| `26_extra_ultra_light` | `_26_extra_ultra_light` |
| `apples.count` | `Apples_Count` |
| `foo_bar.baz.qux-yay` | `Foo_bar_Baz_Qux_Yay` |

**pretty**: Same as normal, but it will first replace whitespaces by underscores and apply the `snakeToCamelCase` filter.

| Input | Output |
|------------------------|----------------------|
| `hello` | `Hello` |
| `42hello` | `_42hello` |
| `some$URL` | `Some_URL` |
| `some$URL` | `SomeURL` |
| `25 Ultra Light` | `_25UltraLight` |
| `26_extra_ultra_light` | `_26ExtraUltraLight` |
| `apples.count` | `ApplesCount` |
| `foo_bar.baz.qux-yay` | `FooBarBazQuxYay` |


## Filter: `titlecase`

Expand Down
4 changes: 2 additions & 2 deletions Sources/Filters+Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ extension Filters {
case .normal:
return StencilSwiftKit.swiftIdentifier(from: string, replaceWithUnderscores: true)
case .pretty:
string = string.replacingOccurrences(of: " ", with: "_")
string = StencilSwiftKit.swiftIdentifier(from: string, replaceWithUnderscores: true)
string = try snakeToCamelCase(string, stripLeading: true)
return StencilSwiftKit.swiftIdentifier(from: string, replaceWithUnderscores: true)
return StencilSwiftKit.prefixWithUnderscoreIfNeeded(string: string)
}
}

Expand Down
28 changes: 19 additions & 9 deletions Sources/SwiftIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private let tailRanges: [CountableClosedRange<Int>] = [
0x30...0x39, 0x300...0x36F, 0x1dc0...0x1dff, 0x20d0...0x20ff, 0xfe20...0xfe2f
]

private func identifierCharacterSets() -> (head: NSMutableCharacterSet, tail: NSMutableCharacterSet) {
private func identifierCharacterSets(exceptions: String) -> (head: NSMutableCharacterSet, tail: NSMutableCharacterSet) {
let addRange: (NSMutableCharacterSet, CountableClosedRange<Int>) -> Void = { (mcs, range) in
mcs.addCharacters(in: NSRange(location: range.lowerBound, length: range.count))
}
Expand All @@ -41,13 +41,15 @@ private func identifierCharacterSets() -> (head: NSMutableCharacterSet, tail: NS
for range in headRanges {
addRange(head, range)
}
head.removeCharacters(in: exceptions)

guard let tail = head.mutableCopy() as? NSMutableCharacterSet else {
fatalError("Internal error: mutableCopy() should have returned a valid NSMutableCharacterSet")
}
for range in tailRanges {
addRange(tail, range)
}
tail.removeCharacters(in: exceptions)

return (head, tail)
}
Expand All @@ -56,14 +58,8 @@ func swiftIdentifier(from string: String,
forbiddenChars exceptions: String = "",
replaceWithUnderscores underscores: Bool = false) -> String {

let (head, tail) = identifierCharacterSets()
head.removeCharacters(in: exceptions)
tail.removeCharacters(in: exceptions)
let (_, tail) = identifierCharacterSets(exceptions: exceptions)

let chars = string.unicodeScalars
let firstChar = chars[chars.startIndex]

let prefix = !head.longCharacterIsMember(firstChar.value) && tail.longCharacterIsMember(firstChar.value) ? "_" : ""
let parts = string.components(separatedBy: tail.inverted)
let replacement = underscores ? "_" : ""
let mappedParts = parts.map({ (string: String) -> String in
Expand All @@ -78,5 +74,19 @@ func swiftIdentifier(from string: String,
return ""
}
})
return prefix + mappedParts.joined(separator: replacement)

let result = mappedParts.joined(separator: replacement)
return prefixWithUnderscoreIfNeeded(string: result, forbiddenChars: exceptions)
}

func prefixWithUnderscoreIfNeeded(string: String,
forbiddenChars exceptions: String = "") -> String {

let (head, tail) = identifierCharacterSets(exceptions: exceptions)

let chars = string.unicodeScalars
let firstChar = chars[chars.startIndex]
let prefix = !head.longCharacterIsMember(firstChar.value) && tail.longCharacterIsMember(firstChar.value) ? "_" : ""

return prefix + string
}
16 changes: 14 additions & 2 deletions Tests/StencilSwiftKitTests/SwiftIdentifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ extension SwiftIdentifierTests {
"42hello": "_42hello",
"some$URL": "Some_URL",
"with space": "With_Space",
"apples.count": "Apples_Count",
".SFNSDisplay": "_SFNSDisplay",
"Show-NavCtrl": "Show_NavCtrl",
"HEADER_TITLE": "HEADER_TITLE",
"multiLine\nKey": "MultiLine_Key",
"foo_bar.baz.qux-yay": "Foo_bar_Baz_Qux_Yay",
"25 Ultra Light": "_25_Ultra_Light",
"26_extra_ultra_light": "_26_extra_ultra_light",
"12 @ 34 % 56 + 78 Hello world": "_12___34___56___78_Hello_World"
Expand All @@ -85,11 +91,17 @@ extension SwiftIdentifierTests {
let expectations = [
"hello": "Hello",
"42hello": "_42hello",
"some$URL": "Some_URL",
"some$URL": "SomeURL",
"with space": "WithSpace",
"apples.count": "ApplesCount",
".SFNSDisplay": "SFNSDisplay",
"Show-NavCtrl": "ShowNavCtrl",
"HEADER_TITLE": "HeaderTitle",
"multiLine\nKey": "MultiLineKey",
"foo_bar.baz.qux-yay": "FooBarBazQuxYay",
"25 Ultra Light": "_25UltraLight",
"26_extra_ultra_light": "_26ExtraUltraLight",
"12 @ 34 % 56 + 78 Hello world": "_12_34_56_78HelloWorld"
"12 @ 34 % 56 + 78 Hello world": "_12345678HelloWorld"
]

for (input, expected) in expectations {
Expand Down

0 comments on commit 418a2bf

Please sign in to comment.