Giter VIP home page Giter VIP logo

Comments (3)

phimage avatar phimage commented on August 22, 2024

relative path from what?
from current path?

there is no cocoa functions I think so we must implement it

I could provide a non tested code

  • maybe we must standardize the path before
  • then some code maybe are not swift3 . compliant
  • return a Path will be more elegant
extension Path {
 
    func stringWithPathRelative(to anchorPath: Path) -> String {
        let pathComponents = self. components
        let anchorComponents = anchorPath. components

        var componentsInCommon = 0
        for (c1, c2) in zip(pathComponents, anchorComponents) {
            if c1 != c2 {
                break
            }
            componentsInCommon += 1
        }

        let numberOfParentComponents = anchorComponents.count - componentsInCommon
        let numberOfPathComponents = pathComponents.count - componentsInCommon

        var relativeComponents = [String]()
        relativeComponents.reserveCapacity(numberOfParentComponents + numberOfPathComponents)
        for _ in 0..<numberOfParentComponents {
            relativeComponents.append("..")
        }
        relativeComponents.appendContentsOf(pathComponents[componentsInCommon..<pathComponents.count])

        return String.pathWithComponents(relativeComponents)
    }
}

from filekit.

ivankolesnik avatar ivankolesnik commented on August 22, 2024

I have something like this in my extension

    func relativeTo(path: Path) -> Path? {
        let root = path.components
        let this = self.components
        
        guard this.starts(with: root) else {
            return nil
        }
        
        let rel = this.suffix(from: root.endIndex)
        let path = rel.map({ $0.rawValue }).joined(separator: "/")
        
        return Path(path)
    }

And I use it like that

let filePath = file.relativeTo(path: Path.userDocuments)

This function does not handle some edge cases, but it suits my needs.

from filekit.

neoneye avatar neoneye commented on August 22, 2024

I have made separate repo, SwiftyRelativePath, for just this purpose with tests for edge cases.

let url0 = URL(fileURLWithPath: "/computer/qubit/17")
let url1 = URL(fileURLWithPath: "/computer/lab")
url0.relativePath(from: url1) // -> "../qubit/17"

Feel free to import it into FileKit.

from filekit.

Related Issues (20)

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.