使用javascript比较路径并获取两个文件之间的相对路径

时间:2013-06-23 17:00:27

标签: javascript

如何动态比较同一域中的两条路径并获得它们之间的相对路径?

var path2 = "http://site.net/test1/test2/img/1.jpg" // test example
var path3 = "http://site.net/test1/img/1.jpg" // test example

获取路径3的返回路径2,例如= "../../img/"

2 个答案:

答案 0 :(得分:3)

不幸的是,JavaScript没有本地方法来执行此操作。但是node.js中的path模块有!此代码取自node.js path module source code

function relative(from, to) {
    function trim(arr) {
      var start = 0;
      for (; start < arr.length; start++) {
        if (arr[start] !== '') break;
      }

      var end = arr.length - 1;
      for (; end >= 0; end--) {
        if (arr[end] !== '') break;
      }

      if (start > end) return [];
      return arr.slice(start, end - start + 1);
    }

    var fromParts = trim(from.split('/'));
    var toParts = trim(to.split('/'));

    var length = Math.min(fromParts.length, toParts.length);
    var samePartsLength = length;
    for (var i = 0; i < length; i++) {
      if (fromParts[i] !== toParts[i]) {
        samePartsLength = i;
        break;
      }
    }

    var outputParts = [];
    for (var i = samePartsLength; i < fromParts.length; i++) {
      outputParts.push('..');
    }

    outputParts = outputParts.concat(toParts.slice(samePartsLength));

    return outputParts.join('/');
}

答案 1 :(得分:1)

更好的方法是使用path.relative

//Define this as class variable
fileprivate lazy var stackView: UIStackView = {
    let sv = UIStackView()
    sv.translatesAutoresizingMaskIntoConstraints = false
    sv.axis = .vertical
    sv.alignment = .fill
    sv.distribution = .fillEqually
    sv.spacing = 10
    return sv
}()

fileprivate lazy var catBg: UIImageView = {
    let iv = UIImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.contentMode = .scaleAspectFill
    iv.image = UIImage(named: "your_img_name")
    return iv
}()

//Put this in viewDidLoad
//Edit

self.view.addSubview(catBg)
//catBg.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
//catBg.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
//catBg.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.7).isActive = true
//catBg.heightAnchor.constraint(equalTo: catBg.widthAnchor, multiplier: 1.78).isActive = true

catBg.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
catBg.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
catBg.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
catBg.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

self.catBg.addSubview(stackView)
stackView.centerXAnchor.constraint(equalTo: catBg.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: catBg.centerYAnchor).isActive = true

for i in 1..<5 {

    let subButton = UIImageView()
    subButton.translatesAutoResizingMaskIntoConstraints = false
    subButton.backgroundColor = UIColor.blue
    subButton.alpha = 1.0
    stackView.addArrangedSubview(subButton)
    subButton.widthAnchor.constraint(equalTo: catBg.widthAnchor, multiplier: 0.3).isActive = true
    //If you have an image you can remove or change this anchor
    subButton.heightAnchor.constraint(equalToConstant: 54).isActive = true
}