Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add utility to compose paths #285

Closed
rodneyrehm opened this issue Mar 10, 2016 · 0 comments
Closed

add utility to compose paths #285

rodneyrehm opened this issue Mar 10, 2016 · 0 comments
Labels

Comments

@rodneyrehm
Copy link
Member

URI.js is currently lacking a utility like Node's path.join() to simply concatenate a few tokens to generate a path. Here's a naive (and not quite accurate) approach:

URI.joinPaths = function() {
  var segments = [].reduce.call(arguments, function(previous, current) {
    var _segments = new URI(current).segment();
    return previous.concat(_segments);
  }, []).filter(Boolean);

  return new URI('').segment(segments);
};

URI.joinPaths('/a/b', '/c', 'd', '/e').toString();
// "a/b/c/d/e"
// (should have leading "/")

URI.joinPaths('a/b', 'http://example.com/c', new URI('d/'), '/e').toString();
// "a/b/c/d/e"

URI.joinPaths('a').toString();
// "a"

URI.joinPaths('').toString();
// "/"
// (should be empty string "")

URI.joinPaths('a', '', '', 'b', '', '').toString();
// "a/b"
// (should have trailing "/")

Somewhat related is the need to resolve paths similarly to Node's path.resolve() but that should probably be a separate PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant