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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1241,10 +1241,14 @@
} else if (_URI || _object) {
var src = _URI ? href._parts : href;
for (key in src) {
if (key === 'query') {
continue;
}
if (hasOwn.call(this._parts, key)) {
this._parts[key] = src[key];
}
}
this.query(src['query']);
Copy link
Member

Choose a reason for hiding this comment

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

this should probably only be executed if src.query is actually defined

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.

definitely! will update.

edit: should it be a test that fail in this case? all tests were passing anyway.

} else {
throw new TypeError('invalid input');
}
Expand Down
43 changes: 43 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
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