summaryrefslogtreecommitdiff
path: root/lib/url.js
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2017-01-17 09:07:18 -0800
committerJames M Snell <jasnell@gmail.com>2017-02-02 12:35:55 -0800
commitc5e9654b5bdb8495debaff6716d8409bc1b737d1 (patch)
tree1eeed8013b651b06dc2a91f5b71586bcc9f43cf3 /lib/url.js
parenta334252fc8346ae914285b771d26c974d5b564e5 (diff)
downloadandroid-node-v8-c5e9654b5bdb8495debaff6716d8409bc1b737d1.tar.gz
android-node-v8-c5e9654b5bdb8495debaff6716d8409bc1b737d1.tar.bz2
android-node-v8-c5e9654b5bdb8495debaff6716d8409bc1b737d1.zip
url: extend url.format to support WHATWG URL
Removes the non-standard options on WHATWG URL toString and extends the existing url.format() API to support customizable serialization of the WHATWG URL object. This does not yet include the documentation updates because the documentation for the new WHATWG URL object has not yet landed. Example: ```js const url = require('url'); const URL = url.URL; const myURL = new URL('http://example.org/?a=b#c'); const str = url.format(myURL, {fragment: false, search: false}); console.log(str); // Prints: http://example.org/ ``` PR-URL: https://github.com/nodejs/node/pull/10857 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
Diffstat (limited to 'lib/url.js')
-rw-r--r--lib/url.js17
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/url.js b/lib/url.js
index e4ced8e860..2b7dd6e532 100644
--- a/lib/url.js
+++ b/lib/url.js
@@ -538,19 +538,22 @@ function autoEscapeStr(rest) {
}
// format a parsed object into a url string
-function urlFormat(obj) {
+function urlFormat(obj, 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
// to clean up potentially wonky urls.
- if (typeof obj === 'string') obj = urlParse(obj);
-
- else if (typeof obj !== 'object' || obj === null)
+ if (typeof obj === 'string') {
+ obj = urlParse(obj);
+ } else if (typeof obj !== 'object' || obj === null) {
throw new TypeError('Parameter "urlObj" must be an object, not ' +
obj === null ? 'null' : typeof obj);
-
- else if (!(obj instanceof Url)) return Url.prototype.format.call(obj);
-
+ } else if (!(obj instanceof Url)) {
+ var format = obj[internalUrl.formatSymbol];
+ return format ?
+ format.call(obj, options) :
+ Url.prototype.format.call(obj);
+ }
return obj.format();
}