-
-
Notifications
You must be signed in to change notification settings - Fork 32k
url: simplify and improve url formatting #46736
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
'use strict'; | ||
|
||
const { | ||
Boolean, | ||
Int8Array, | ||
ObjectKeys, | ||
SafeSet, | ||
|
@@ -37,7 +38,10 @@ const { | |
ERR_INVALID_ARG_TYPE, | ||
ERR_INVALID_URL, | ||
} = require('internal/errors').codes; | ||
const { validateString } = require('internal/validators'); | ||
const { | ||
validateString, | ||
validateObject, | ||
} = require('internal/validators'); | ||
|
||
// This ensures setURLConstructor() is called before the native | ||
// URL::ToObject() method is used. | ||
|
@@ -50,11 +54,14 @@ const { | |
domainToASCII, | ||
domainToUnicode, | ||
fileURLToPath, | ||
formatSymbol, | ||
pathToFileURL, | ||
urlToHttpOptions, | ||
} = require('internal/url'); | ||
|
||
const { | ||
formatUrl, | ||
} = internalBinding('url'); | ||
|
||
// Original url.parse() API | ||
|
||
function Url() { | ||
|
@@ -587,13 +594,36 @@ function urlFormat(urlObject, options) { | |
} else if (typeof urlObject !== 'object' || urlObject === null) { | ||
throw new ERR_INVALID_ARG_TYPE('urlObject', | ||
['Object', 'string'], urlObject); | ||
} else if (!(urlObject instanceof Url)) { | ||
const format = urlObject[formatSymbol]; | ||
return format ? | ||
format.call(urlObject, options) : | ||
Url.prototype.format.call(urlObject); | ||
} else if (urlObject instanceof URL) { | ||
let fragment = true; | ||
let unicode = false; | ||
let search = true; | ||
let auth = true; | ||
|
||
if (options) { | ||
validateObject(options, 'options'); | ||
|
||
if (options.fragment != null) { | ||
fragment = Boolean(options.fragment); | ||
} | ||
|
||
if (options.unicode != null) { | ||
unicode = Boolean(options.unicode); | ||
} | ||
|
||
if (options.search != null) { | ||
search = Boolean(options.search); | ||
} | ||
|
||
if (options.auth != null) { | ||
auth = Boolean(options.auth); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting these up as a set of bit flags may be a bit nicer. e.g. something like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That does not cover passing |
||
} | ||
|
||
return formatUrl(urlObject.href, fragment, unicode, search, auth); | ||
} | ||
return urlObject.format(); | ||
|
||
return Url.prototype.format.call(urlObject); | ||
} | ||
|
||
// These characters do not need escaping: | ||
|
Uh oh!
There was an error while loading. Please reload this page.