Skip to content

Commit

Permalink
Add .deletingLastExtension to String
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jun 29, 2024
1 parent f567a7d commit 0033a80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CotEditor/Sources/String+Filename.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@

import Foundation

extension String {

/// The remainder of string after last dot removed.
var deletingPathExtension: String {

self.replacing(/^(.+)\.[^ .]+$/, with: \.1)
}
}


extension Collection<String> {

/// Creates a unique name from the receiver's elements by adding the suffix and also a number if needed.
Expand Down
12 changes: 12 additions & 0 deletions Tests/StringFilenameTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ import Testing

struct StringFilename {

@Test func removeExtension() {

#expect("test".deletingPathExtension == "test")
#expect("test.".deletingPathExtension == "test.")
#expect("test.txt".deletingPathExtension == "test")
#expect("test..txt".deletingPathExtension == "test.")
#expect("test.txt.txt".deletingPathExtension == "test.txt")
#expect(".htaccess".deletingPathExtension == ".htaccess")
#expect("1.2 file".deletingPathExtension == "1.2 file")
}


@Test func createAvailableNames() {

let names = ["foo", "foo 3", "foo copy 3", "foo 4", "foo 7"]
Expand Down

0 comments on commit 0033a80

Please sign in to comment.