Skip to content

Commit

Permalink
feature(constructor): fixing behavior of URI() to match that of new U…
Browse files Browse the repository at this point in the history
…RI() - closing #205
  • Loading branch information
rodneyrehm committed Apr 1, 2015
1 parent 141c04d commit 1f9a4c1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

### master (will become 1.15.1) ###

* fixing `URI()` to match behavior of `new URI()` (caused by [#196](https://github.com/medialize/URI.js/issues/196)) - [Issue #205](https://github.com/medialize/URI.js/issues/205)

### 1.15.0 (April 1st 2015 - no joke, promise!) ###

* fixing `URI(undefined)` to throw TypeError - ([Issue #189](https://github.com/medialize/URI.js/issues/189), [Issue #196](https://github.com/medialize/URI.js/issues/196), [eakron](https://github.com/eakron)) - *tiny backward-compatibility-break*
Expand Down
15 changes: 13 additions & 2 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@
var _URI = root && root.URI;

function URI(url, base) {
var _urlSupplied = arguments.length >= 1;
var _baseSupplied = arguments.length >= 2;

// Allow instantiation without the 'new' keyword
if (!(this instanceof URI)) {
return new URI(url, base);
if (_urlSupplied) {
if (_baseSupplied) {
return new URI(url, base);
}

return new URI(url);
}

return new URI();
}

if (url === undefined) {
if (arguments.length) {
if (_urlSupplied) {
throw new TypeError('undefined is not a valid argument for URI');
}

Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
});

module('constructing');
test('URI()', function() {
var u = URI();
ok(u instanceof URI, 'instanceof URI');
equal(u.toString(), window.location && window.location.href || '', 'is location (browser) or empty string (node)');
});
test('URI(undefined)', function() {
raises(function() {
URI(undefined);
}, TypeError, 'Failing undefined input');
});
test('new URI(string)', function() {
var u = new URI('http://example.org/');
ok(u instanceof URI, 'instanceof URI');
Expand All @@ -23,6 +33,14 @@
var u = new URI(location);
equal(u.href(), String(location.href), 'location object');
});
test('new URI(undefined)', function() {
var u = new URI();
ok(u instanceof URI, 'instanceof URI');
equal(u.toString(), window.location && window.location.href || '', 'is location (browser) or empty string (node)');
raises(function() {
new URI(undefined);
}, TypeError, 'Failing undefined input');
});
(function() {
var element;

Expand Down

0 comments on commit 1f9a4c1

Please sign in to comment.