Skip to content
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

Open
jpmhouston opened this issue Mar 15, 2020 · 2 comments
Open

nameExcludingExtension removes all "." #109

jpmhouston opened this issue Mar 15, 2020 · 2 comments

Comments

@jpmhouston
Copy link

For file named "Dots.in.place.of.spaces.txt", nameExcludingExtension should return "Dots.in.place.of.spaces" but instead returns "Dotsinplaceofspaces".

@clayellis
Copy link
Contributor

clayellis commented Mar 16, 2020

This could prove problematic to solve because names that have dots in them, but don't have an extension (Some.Image.Without.An.Extension, or worse Some.File_With_Mixed_Delimeters), can't easily be recognized.

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")
        }
    }

@marwey
Copy link

marwey commented May 3, 2020

@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:

  1. What is a correct file extension?
  2. When provided a filename with a correct extension, does the code return the correct name excluding the extension?

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants