Comments (2)
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.
@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:
- What is a correct file extension?
- 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)
- 'NSWorkspace' is unavailable in Mac Catalyst HOT 3
- Examples are buggy HOT 2
- Using the Swift Package Manager - Getting Error HOT 2
- Throwing function never throws
- Hello. Why there is no .documents and .library folder for iOS builds? HOT 4
- How can I move a file to a new folder with a new name atomically?
- Subfolder on iOS is returning empty while on Mac returns content HOT 1
- Get File Size HOT 1
- Example of use with (or instead of) fileImporter?
- Is there a way to get file metadata such as modification date?
- Hidden subfolders are not listed in `subfolders` property HOT 1
- Unable to get hidden files
- "\" characters confuse "Folder.subfolders" HOT 2
- Usb
- This may reduce the quality of type information available
- Try? thread exception issue with XCode 14.3 and Release
- Heads-up: The next major version (5.0) will drop CocoaPods + Carthage support HOT 1
- Folder.documents doesn't seem to work HOT 7
- Folder(path: <path>) doesn't seem to work HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from files.