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

lib: fix urlObject parameter name in url.format #14031

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
25 changes: 13 additions & 12 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,22 +550,23 @@ function autoEscapeStr(rest) {
}

// format a parsed object into a url string
function urlFormat(obj, options) {
function urlFormat(urlObject, options) {
// ensure it's an object, and not a string url.
// If it's an obj, this is a no-op.
// this way, you can call url_format() on strings
// If it's an object, this is a no-op.
// this way, you can call urlParse() on strings
// to clean up potentially wonky urls.
if (typeof obj === 'string') {
obj = urlParse(obj);
} else if (typeof obj !== 'object' || obj === null) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'urlObj', 'object', obj);
} else if (!(obj instanceof Url)) {
var format = obj[formatSymbol];
if (typeof urlObject === 'string') {
urlObject = urlParse(urlObject);
} else if (typeof urlObject !== 'object' || urlObject === null) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'urlObject',
['object', 'string'], urlObject);
} else if (!(urlObject instanceof Url)) {
var format = urlObject[formatSymbol];
return format ?
format.call(obj, options) :
Url.prototype.format.call(obj);
format.call(urlObject, options) :
Url.prototype.format.call(urlObject);
}
return obj.format();
return urlObject.format();
}

Url.prototype.format = function format() {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-url-format-invalid-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const throwsObjsAndReportTypes = new Map([
[Symbol('foo'), 'symbol']
]);

for (const [obj, type] of throwsObjsAndReportTypes) {
for (const [urlObject, type] of throwsObjsAndReportTypes) {
const error = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "urlObj" argument must be of type object. ' +
message: 'The "urlObject" argument must be one of type object or string. ' +
`Received type ${type}`
});
assert.throws(function() { url.format(obj); }, error);
assert.throws(function() { url.format(urlObject); }, error);
}
assert.strictEqual(url.format(''), '');
assert.strictEqual(url.format({}), '');