Skip to content

Instantly share code, notes, and snippets.

@yesmar
Created January 30, 2018 21:19
Show Gist options
  • Save yesmar/b45088c49c339222e0ecd31ab7da0363 to your computer and use it in GitHub Desktop.
Save yesmar/b45088c49c339222e0ecd31ab7da0363 to your computer and use it in GitHub Desktop.
Standardize construction of pathnames with extensions when multiple periods are involved
import Foundation
import PlaygroundSupport
func constructPathname(pathname: URL, name: String, ext: String) -> URL {
let n = name.hasSuffix(".") ? String(name.dropLast()) : name
let x = ext.hasPrefix(".") ? String(ext.dropFirst()) : ext
return pathname.appendingPathComponent("\(n).\(x)")
}
print(constructPathname(pathname: playgroundSharedDataDirectory, name: "Tea, Inc.", ext: ".json"))
print(constructPathname(pathname: playgroundSharedDataDirectory, name: "Tea, Inc.", ext: "json"))
print(constructPathname(pathname: playgroundSharedDataDirectory, name: "Tea, Inc", ext: "json"))
print(constructPathname(pathname: playgroundSharedDataDirectory, name: "Tea, Inc", ext: ".json"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment