Giter Site home page Giter Site logo

simplepath's Introduction

Build

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:

  1. Implement such functionality as an extension.
  2. 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.

simplepath's People

Contributors

raymondjavaxx avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.