-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nameExcludingExtension removes all "." #109
Comments
This could prove problematic to solve because names that have dots in them, but don't have an extension ( My initial solution would be this: /// The name of the location, excluding its `extension`.
var nameExcludingExtension: String {
let components = name.split(separator: ".")
guard components.count > 1 else { return name }
return components.dropLast().joined(separator: ".") // Re-join the name with dots
} But it fails on this test: func testNameExcludingExtensionWithFileNameIncludingDots() {
performTest {
let file = try folder.createFile(named: "File.Name.With.Dots.txt")
let subfolder = try folder.createSubfolder(named: "Subfolder.With.Dots")
XCTAssertEqual(file.nameExcludingExtension, "File.Name.With.Dots")
XCTAssertEqual(subfolder.nameExcludingExtension, "Subfolder.With.Dots") // XCTAssertEqual failed: ("Subfolder.With") is not equal to ("Subfolder.With.Dots")
}
} |
@clayellis Thanks for your code examples and test cases which I appreciate, since I ran into this issue as well. I guess the problem is two folded:
For my use case I solved 1) by validating macOS Uniform Type Identifiers and 2) by the following code: extension Location {
var nameWithoutExtension: String {
return self.name
}
}
extension File {
var nameWithoutExtension: String {
guard let ext = self.extension else { return self.name }
return String(self.name.replacingOccurrences(of: ext, with: "", options: .backwards).dropLast())
}
} Happy to submit a PR based on the above snippets, if people feel this is an improvement over the current version? |
For file named "Dots.in.place.of.spaces.txt", nameExcludingExtension should return "Dots.in.place.of.spaces" but instead returns "Dotsinplaceofspaces".
The text was updated successfully, but these errors were encountered: