From 4e5feeaecd720f0accf72951ff66d82af71d56a3 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Aste Kompen Date: Sat, 10 Feb 2018 00:44:46 -0300 Subject: [PATCH 1/5] feature: add query as object constructor and tests --- src/URI.js | 8 ++++++++ test/test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/URI.js b/src/URI.js index 1d7957a0..5b24da0b 100644 --- a/src/URI.js +++ b/src/URI.js @@ -1240,11 +1240,19 @@ this._parts = URI.parse(String(href), this._parts); } else if (_URI || _object) { var src = _URI ? href._parts : href; + var queryObject; for (key in src) { + if (key === 'query' && typeof src[key] === 'object') { + queryObject = true; + continue; + } if (hasOwn.call(this._parts, key)) { this._parts[key] = src[key]; } } + if (queryObject) { + this.query(src['query']); + } } else { throw new TypeError('invalid input'); } diff --git a/test/test.js b/test/test.js index fe62c01c..b6b44d0c 100644 --- a/test/test.js +++ b/test/test.js @@ -34,6 +34,36 @@ ok(u instanceof URI, 'instanceof URI'); ok(u._parts.hostname !== undefined, 'host undefined'); }); + + test('new URI(object)', function() { + var u = new URI({ + protocol: 'http', + hostname: 'example.org', + query: { + foo: 'bar', + bar: 'foo', + }, + }); + ok(u instanceof URI, 'instanceof URI'); + ok(typeof u.query() === 'string', 'query is string'); + ok(u.query() === 'foo=bar&bar=foo', 'query has right value'); + ok(u.search() === '?foo=bar&bar=foo', 'search has right value'); + ok(typeof u.query(true) === 'object', 'query map is object'); + ok(typeof u.search(true) === 'object', 'search map is object'); + }); + test('new URI(object)', function() { + var u = new URI({ + protocol: 'http', + hostname: 'example.org', + query: 'foo=bar&bar=foo', + }); + ok(u instanceof URI, 'instanceof URI'); + ok(typeof u.query() === 'string', 'query is string'); + ok(u.query() === 'foo=bar&bar=foo', 'query has right value'); + ok(u.search() === '?foo=bar&bar=foo', 'search has right value'); + ok(typeof u.query(true) === 'object', 'query map is object'); + ok(typeof u.search(true) === 'object', 'search map is object'); + }); test('new URI(Location)', function () { var u = new URI(location); equal(u.href(), String(location.href), 'location object'); From 63d4df904e6b1cd289f789a3102f4a966b2d3ece Mon Sep 17 00:00:00 2001 From: Pedro Pablo Aste Kompen Date: Sat, 10 Feb 2018 00:52:34 -0300 Subject: [PATCH 2/5] feature: allow construction with a query string starting with ? --- src/URI.js | 8 ++------ test/test.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/URI.js b/src/URI.js index 5b24da0b..b51d2395 100644 --- a/src/URI.js +++ b/src/URI.js @@ -1240,19 +1240,15 @@ this._parts = URI.parse(String(href), this._parts); } else if (_URI || _object) { var src = _URI ? href._parts : href; - var queryObject; for (key in src) { - if (key === 'query' && typeof src[key] === 'object') { - queryObject = true; + if (key === 'query') { continue; } if (hasOwn.call(this._parts, key)) { this._parts[key] = src[key]; } } - if (queryObject) { - this.query(src['query']); - } + this.query(src['query']); } else { throw new TypeError('invalid input'); } diff --git a/test/test.js b/test/test.js index b6b44d0c..565b4546 100644 --- a/test/test.js +++ b/test/test.js @@ -64,6 +64,19 @@ ok(typeof u.query(true) === 'object', 'query map is object'); ok(typeof u.search(true) === 'object', 'search map is object'); }); + test('new URI(object)', function() { + var u = new URI({ + protocol: 'http', + hostname: 'example.org', + query: '?foo=bar&bar=foo', + }); + ok(u instanceof URI, 'instanceof URI'); + ok(typeof u.query() === 'string', 'query is string'); + ok(u.query() === 'foo=bar&bar=foo', 'query has right value'); + ok(u.search() === '?foo=bar&bar=foo', 'search has right value'); + ok(typeof u.query(true) === 'object', 'query map is object'); + ok(typeof u.search(true) === 'object', 'search map is object'); + }); test('new URI(Location)', function () { var u = new URI(location); equal(u.href(), String(location.href), 'location object'); From 78daaee83634263b7da64e234593864659015998 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Aste Kompen Date: Sat, 10 Feb 2018 10:19:32 -0300 Subject: [PATCH 3/5] feature: run query() only when query is in parts --- src/URI.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/URI.js b/src/URI.js index b51d2395..985fb590 100644 --- a/src/URI.js +++ b/src/URI.js @@ -1240,15 +1240,19 @@ this._parts = URI.parse(String(href), this._parts); } else if (_URI || _object) { var src = _URI ? href._parts : href; + var hasQueryPart = false; for (key in src) { if (key === 'query') { + hasQueryPart = true; continue; } if (hasOwn.call(this._parts, key)) { this._parts[key] = src[key]; } } - this.query(src['query']); + if (hasQueryPart) { + this.query(src['query']); + } } else { throw new TypeError('invalid input'); } From 716c3772022891311f5a036402e627c4b02a0ad7 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Aste Kompen Date: Sat, 10 Feb 2018 10:35:05 -0300 Subject: [PATCH 4/5] refactor: rewrite feature to only run query() when there's actually a value as a query --- src/URI.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/URI.js b/src/URI.js index 985fb590..d2cf26d6 100644 --- a/src/URI.js +++ b/src/URI.js @@ -1240,18 +1240,14 @@ this._parts = URI.parse(String(href), this._parts); } else if (_URI || _object) { var src = _URI ? href._parts : href; - var hasQueryPart = false; for (key in src) { - if (key === 'query') { - hasQueryPart = true; - continue; - } + if (key === 'query') { continue; } if (hasOwn.call(this._parts, key)) { this._parts[key] = src[key]; } } - if (hasQueryPart) { - this.query(src['query']); + if (src.query) { + this.query(src.query, false); } } else { throw new TypeError('invalid input'); From 9b8b373ca48fa0b7a32c30654821c7d720e617b9 Mon Sep 17 00:00:00 2001 From: Pedro Pablo Aste Kompen Date: Sat, 10 Feb 2018 11:43:09 -0300 Subject: [PATCH 5/5] chore: improve tests using equal and deepEqual assertions --- test/test.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/test.js b/test/test.js index 565b4546..c29c2ad1 100644 --- a/test/test.js +++ b/test/test.js @@ -46,10 +46,10 @@ }); ok(u instanceof URI, 'instanceof URI'); ok(typeof u.query() === 'string', 'query is string'); - ok(u.query() === 'foo=bar&bar=foo', 'query has right value'); - ok(u.search() === '?foo=bar&bar=foo', 'search has right value'); - ok(typeof u.query(true) === 'object', 'query map is object'); - ok(typeof u.search(true) === 'object', 'search map is object'); + equal(u.query(), 'foo=bar&bar=foo', 'query has right value'); + equal(u.search(), '?foo=bar&bar=foo', 'search has right value'); + deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value'); + deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value'); }); test('new URI(object)', function() { var u = new URI({ @@ -59,10 +59,10 @@ }); ok(u instanceof URI, 'instanceof URI'); ok(typeof u.query() === 'string', 'query is string'); - ok(u.query() === 'foo=bar&bar=foo', 'query has right value'); - ok(u.search() === '?foo=bar&bar=foo', 'search has right value'); - ok(typeof u.query(true) === 'object', 'query map is object'); - ok(typeof u.search(true) === 'object', 'search map is object'); + equal(u.query(), 'foo=bar&bar=foo', 'query has right value'); + equal(u.search(), '?foo=bar&bar=foo', 'search has right value'); + deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value'); + deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value'); }); test('new URI(object)', function() { var u = new URI({ @@ -72,10 +72,10 @@ }); ok(u instanceof URI, 'instanceof URI'); ok(typeof u.query() === 'string', 'query is string'); - ok(u.query() === 'foo=bar&bar=foo', 'query has right value'); - ok(u.search() === '?foo=bar&bar=foo', 'search has right value'); - ok(typeof u.query(true) === 'object', 'query map is object'); - ok(typeof u.search(true) === 'object', 'search map is object'); + equal(u.query(), 'foo=bar&bar=foo', 'query has right value'); + equal(u.search(), '?foo=bar&bar=foo', 'search has right value'); + deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value'); + deepEqual(u.search(true), { foo: 'bar', bar: 'foo' }, 'search(true) value'); }); test('new URI(Location)', function () { var u = new URI(location);