SimplePath
SimplePath is a library for working with file paths in Swift. It borrows heavily from the path libraries of other languages such as Go, PHP, C and Perl, and runtimes such as Node.js.
This library doesn't try to hide the fact that file paths are strings. Most of its functions take strings as arguments and return strings.
Functions
Path.join([components])
Builds a path string by joining components, adding directory separators where necessary.
let path = Path.join([
"/var/www",
"website",
"robots.txt"
])
// path -> /var/www/website/robots.txt
Path.split(path)
Returns all components of a given path.
let components = Path.split("/storage/images/0001.jpg")
// components -> [
// "/",
// "storage",
// "images",
// "0001.jpg"
// ]
Path.basename(path)
Returns the last component of a path. Usually a file name.
let base = Path.basename("assets/images/logo.png")
// base -> "logo.png"
let base = Path.basename("assets/images/logo.png", ext: "png")
// base -> "logo"
Path.dirname(path)
Returns the parent directory of a path.
let dir = Path.dirname("/var/data/map.bin")
// dir -> "/var/data"
Path.extname(path)
Gets the extension of a path.
let ext = Path.extname("assets/sfx/drumroll.wav")
// ext -> "wav"
let ext = Path.extname("assets/sfx")
// ext -> nil
Path.format([elements])
Builds a path, given named elements of the path.
let path = Path.format([
.dir: "assets/icons",
.base: "settings.png"
])
// path -> "assets/icons/settings.png"
let path = Path.format([
.dir: "assets/vector",
.base: "logo"
.ext: "svg"
])
// path -> "assets/vector/logo.svg"
Path.isAbsolute(path) and Path.isRelative(path)
Can be used for checking if a path is absolute or relative.
Path.isAbsolute("/var/logs/test.log") // -> true
Path.isRelative("cache/images/1.bin") // -> true
Path.exists(path)
Returns true if the path exists.
Path.exists("/path/to/existing/file.txt") // -> true
Path.isFile(path)
Returns true if the path exists and is a regular file.
Path.isFile("/path/to/existing/file.txt") // -> true
Path.isDir(path)
Returns true if the path exists and is a directory.
Path.isDir("/path/to/existing/dir") // -> true
Extending
If you think SimplePath is missing important functionalty I suggest you follow these steps:
- Implement such functionality as an extension.
- And if you believe that the functionality is highly reusable, feel free to contribute it back to the project by opening a pull request.
License
This library is licensed under the MIT license.