I'm still not fully convinced that it should extend String. There are
a couple of issues I have with it...
- Native APIs that require an Uri all make use of valueOf() and
therefore it is still possible to use an object for window.location =
uri; window.location.href = uri; aswell as XMLHTTPRequest.
- The only issue with not extending String is when you use the URI for
another API which expects a string. But that Uri should call valueOf()
or at least test typeof() and therefore it's a problem on that end and
not a problem in the URI object. That is why we have valueOf() isn't
it?
- typeof(...) and $type(...) will still NOT return 'string' and
therefore if another API uses typeof(...) checks or Array.link, it
won't work anyway.
- If, String is extended with methods with the same name as the ones
in URI. It can lead to confusion in classes that are expecting a
String with those extensions yet receives an object where those
methods have a different meaning. For example: 'Text'.parse(function)
vs. new Uri('http://www.mootools.net/').parse() for example. Possibly
an edge case but I'd still say it's an iffy architecture.
- Some of the verbs of String methods are dubious at best if you use
them in an Uri context. Such as: trim, clean, test, slice, toSource
(FF) etc...
It's tempting to extend the natives but in an object-oriented sense,
an URI isn't really just a string. Just like a string isn't just an
array of characters. It's it's own type with it's own meaning. I think
it could get messy quite quickly if it's going to be extended.
If a String is a must, I think it would be better to let those
features be pure extensions on String. For example:
var uri = 'http://www.mootools.net/example?test';
uri.getUriQuery(); // returns '?test'
uri.getUriPart('hostname'); // returns 'www.mootools.net'
uri.toRelativeUri('http://www.mootools.net/'); // returns 'example?
test'
But I think it would be best to have extendable URI objects and let
valueOf() do it's job. One could easily change them back and forth:
var uri = 'http://www.mootools.net'.asUri();
uri.get('hostname'); // returns 'www.mootools.net'
uri.valueOf().substr(0, 21).getUriPart('hostname'); // returns
'www.mootools.n'