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

Feature/to uri #374

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open

Conversation

DoDSoftware
Copy link

When initializing with a string that is missing the protocol, like URI('somesite.com');, the string is parsed as a path and not a URI.

Because of this, domain(), host(), and port() do not function as expected and return "";

See https://jsfiddle.net/Ln9c0jpy/1/

Related issues:
#260
#232
#187

This change adds a toUri method that allows the user to do this:

let uri = new URI(URI.toUri('somesite.com', {scheme: 'http'}));

after which, uri.domain() will return the expected somesite.com

@rodneyrehm
Copy link
Member

Hey there, thanks for chipping in! I'll comment on the code itself as well, but generally you should:

  • point the PR against master not gh-pages
  • add some tests to test/tests.js in order to make sure your changes work as expected

@DoDSoftware
Copy link
Author

@rodneyrehm Makes sense. Do you want to go ahead and close this one and Ill make the changes and re-submit this afternoon?

@@ -487,6 +487,36 @@

URI.encodeReserved = generateAccessor('reserved', 'encode');

URI.toUri = function(string, defaults) {
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were doing doc-blocks, they'd come before the function, not within.

switch (true) {
case defaults.scheme === 'http':
case defaults.scheme === 'https':
defaults.scheme = defaults.scheme+':';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're mutating a an object that was passed into the function, this can have unexpected consequences. consider a simplified version of your code:

function doSomething(defaults) {
  switch (true) {
    case defaults.scheme === 'http':
    case defaults.scheme === 'https':
      defaults.scheme = defaults.scheme+':';
      break;
    default:
      defaults.scheme = '';
  }

  console.log(defaults)
}

var myDefaults = { scheme: 'http' };
doSomething(myDefaults);
doSomething(myDefaults);

and the output:

{scheme: "http:"}
{scheme: ""}

// be parsed as a path
string = defaults.scheme+'//'+string;
}
return URI.build(URI.parse(string));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the benefit of running a parse+serialization at this point?

defaults = defaults || {};
// if the uri doesn't have a scheme, add one
// now so it doesn't get parsed as a path
if (!string.match(/^(https|http)?:\/\//i)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also match URLs of the form ftp://… which would then produce http://ftp://….

You'd be looking for the index of "://" and taking the the substring that comes before. You'd add the default.scheme only if "://" is not part of the string, or the substring before that is the empty string.

https://github.com/medialize/URI.js/blob/gh-pages/src/URI.js#L218 is a ready-to-use expression for validating a scheme, if should that be necessary.

@rodneyrehm
Copy link
Member

Makes sense. Do you want to go ahead and close this one and Ill make the changes and re-submit this afternoon?

you can edit the existing PR, or you can close this PR, whatever you prefer ;)

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

Successfully merging this pull request may close these issues.

2 participants