Skip to content

μ-library to deal with non-empty strings, filenames, and directories.

License

Notifications You must be signed in to change notification settings

CleanCocoa/PathUtils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PathUtils

μ-library to deal with non-empty strings, filenames, and directories.

Usage

The base types protect important invariants and are as follows:

  • ContentfulString cannot be constructed from an empty string.
  • Filename, e.g. just "foo". The filename is never an empty string.
  • Basename, e.g. "foo.txt". The path extension is optional, but a valid filename is always present.
  • Folder keeps a URL reference, but only to local directory URLs.
  • FileURL only accepts local file: URLs, but rejects directories.

FileURL is the true star of the show: It combines Folder + Basename to represent local file URLs, so access to these never fails. Once you have a FileURL, you get checks for non-directory, local file: URLs for free. There's always going to be a basename for convenient access.

Construct file URLs from components, e.g. to create a destination URL for a new file in an existing folder:

guard let basename = Basename("foo.txt"),
      let folder = Folder(url: URL(fileURLWithPath: "/path/to/success/"))
else { exit }

let fileURL: FileURL = basename.fileURL(relativeTo: folder)

Filter URLs from user input for local filesystem references:

let url: URL = ...

guard let fileURL = FileURL(from: url) else { exit }

print("You selected", fileURL.basename, "in", fileURL.folder)

Construct file URLs from existing primitives by concatenation through custom operators:

// Custom operators!
let path: String = folder + basename
let fileURL: FileURL = folder + basename

Installation

Using Swift Package Manager

.package(url: "https://github.com/CleanCocoa/PathUtils", from: "0.3.2")

License

MIT

About

μ-library to deal with non-empty strings, filenames, and directories.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages