JohnSundell / Files

A nicer way to handle files & folders in Swift
MIT License
2.53k stars 182 forks source link

Location.nameExcludingExtension removes dots from filenames #124

Open maximkrouk opened 3 years ago

jflow commented 3 years ago

The dot is part of the extension. Why would you want the dot at the end???

honghaoz commented 2 years ago

alternatively, use

(self as NSString).deletingPathExtension
maximkrouk commented 2 years ago

@jflow, didn't see your comment at the time 😅

Not in the end, but in the middle For example R.generated.swift for Rswift - the extension is swift, the name is R.generated, but the current implementation of nameExcludingExtension returns Rgenerated, and an empty string for hidden files like .swiftlint 🌚

maximkrouk commented 2 years ago

@honghaoz, not sure that casting to NSString is a better option, as for the library it might be better to use something like

extension String {
  var nameExcludingExtension: String {
    guard
      let index = lastIndex(of: "."),
      index != startIndex 
    else { return self }
    return String(self[startIndex..<index])
  }
}

That should work faster, so you don't have to go through the whole string to find the dots, but only iterate from the end to the first dot, if my assumption about lastIndex(of:) implementation is correct (yeaah, microoptimisations 😎)

And also this implementation will handle hidden files correctly like .swiftlint (=> ".swiftlint" instead of ""), I may update the PR if @JohnSundell is interested (a bunch of PRs are here kinda unnoticed btw, so maybe it's time to review them 😅)