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: add query as object constructor and tests #367

Merged
merged 5 commits into from Feb 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/URI.js
Expand Up @@ -1240,11 +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;
Copy link
Contributor Author

@wachunei wachunei Feb 10, 2018

Choose a reason for hiding this comment

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

wait a second, this can be true even if src['query'] is undefined, isnt it?
maybe I should have changed it to

if (key === 'query' && src[key] !== undefined) {

Copy link
Member

Choose a reason for hiding this comment

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

you can keep the if (key === 'query') { continue; } part and add if (src.query) { this.query(src.query, false); }. you only need to execute .query() if there actually is a value (other than null, undefined, empty-string)

continue;
}
if (hasOwn.call(this._parts, key)) {
this._parts[key] = src[key];
}
}
if (hasQueryPart) {
this.query(src['query']);
}
} else {
throw new TypeError('invalid input');
}
Expand Down
43 changes: 43 additions & 0 deletions test/test.js
Expand Up @@ -34,6 +34,49 @@
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');
Copy link
Member

Choose a reason for hiding this comment

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

equal(u.query(), 'foo=bar&bar=foo', 'query() 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');
Copy link
Member

Choose a reason for hiding this comment

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

deepEqual(u.query(true), { foo: 'bar', bar: 'foo' }, 'query(true) value');

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');
Expand Down