Skip to content

Commit

Permalink
Fix convert to snake case algorithm (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
subdan authored Feb 2, 2022
1 parent 280ed9e commit 08ac8e5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "d2930e8fcf9c33162b9fcc1d522bc975e2d4179b",
"version": "1.0.1"
"revision": "e1465042f195f374b94f915ba8ca49de24300a0d",
"version": "1.0.2"
}
},
{
Expand All @@ -60,8 +60,8 @@
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "173f567a2dfec11d74588eea82cecea555bdc0bc",
"version": "1.4.0"
"revision": "5d66f7ba25daf4f94100e7022febf3c75e37a6c7",
"version": "1.4.2"
}
},
{
Expand All @@ -87,8 +87,8 @@
"repositoryURL": "https://github.com/jpsim/Yams.git",
"state": {
"branch": null,
"revision": "138cf1b701cf825233b92ceac919152d5aba8a3f",
"version": "4.0.1"
"revision": "9ff1cc9327586db4e0c8f46f064b6a82ec1566fa",
"version": "4.0.6"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let package = Package(
// Exports resources to Xcode project
.target(
name: "XcodeExport",
dependencies: ["FigmaExportCore", "Stencil"]
dependencies: ["FigmaExportCore", .product(name: "Stencil", package: "Stencil")]
),

// Exports resources to Android project
Expand Down
6 changes: 4 additions & 2 deletions Sources/FigmaExportCore/Extensions/StringCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public extension String {
///
/// - Returns: A snake case copy of the string.
func snakeCased() -> String {
if self.isSnakeCase { return self }
return lowercasedStrings().map{ $0.lowercased() }.joined(separator: "_")
guard !self.isSnakeCase else { return self }
let result = self.split(separator: " ").joined(separator: "_")
guard !result.isSnakeCase else { return result }
return result.lowercasedStrings().map{ $0.lowercased() }.joined(separator: "_")
}
}
19 changes: 19 additions & 0 deletions Tests/FigmaExportTests/AssetsProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,23 @@ final class AssetsProcessorTests: XCTestCase {

XCTAssertThrowsError(try processor.process(light: lights, dark: darks).get())
}

func testProcess() throws {
let images = [
ImagePack(image: Image(name: "icons / 24 / arrow back", url: URL(string: "/")!, format: "png"), platform: .android)
]

let processor = ImagesProcessor(
platform: .android,
nameValidateRegexp: #"^icons _ (\d\d) _ ([A-Za-z0-9 /_-]+)$"#,
nameReplaceRegexp: #"n2_icon_$2_$1"#,
nameStyle: .snakeCase
)

let result = processor.process(assets: images)

let extracted = try result.get()

XCTAssertEqual(extracted[0].name, "n2_icon_arrow_back_24")
}
}

0 comments on commit 08ac8e5

Please sign in to comment.