Code Monkey home page Code Monkey logo

Comments (2)

clayellis avatar clayellis commented on June 1, 2024

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

from files.

marwey avatar marwey commented on June 1, 2024

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

from files.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.