Skip to content

Commit

Permalink
lib: fix urlObject parameter name in url.format
Browse files Browse the repository at this point in the history
Documentation, error message, and code now use the same argument name.

PR-URL: #14031
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
leggiero authored and Trott committed Jul 5, 2017
1 parent e36917b commit 8520e6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
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({}), '');

0 comments on commit 8520e6f

Please sign in to comment.