From c5e9654b5bdb8495debaff6716d8409bc1b737d1 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 17 Jan 2017 09:07:18 -0800 Subject: 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 Reviewed-By: Anna Henningsen Reviewed-By: Timothy Gu Reviewed-By: Brian White --- lib/url.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'lib/url.js') 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(); } -- cgit v1.2.3