next
Version:
The React Framework
111 lines (110 loc) • 3.96 kB
JavaScript
// Format function modified from nodejs
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
;
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
formatUrl: null,
formatWithValidation: null,
urlObjectKeys: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
formatUrl: function() {
return formatUrl;
},
formatWithValidation: function() {
return formatWithValidation;
},
urlObjectKeys: function() {
return urlObjectKeys;
}
});
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
const _querystring = /*#__PURE__*/ _interop_require_wildcard._(require("./querystring"));
const slashedProtocols = /https?|ftp|gopher|file/;
function formatUrl(urlObj) {
let { auth, hostname } = urlObj;
let protocol = urlObj.protocol || '';
let pathname = urlObj.pathname || '';
let hash = urlObj.hash || '';
let query = urlObj.query || '';
let host = false;
auth = auth ? encodeURIComponent(auth).replace(/%3A/i, ':') + '@' : '';
if (urlObj.host) {
host = auth + urlObj.host;
} else if (hostname) {
host = auth + (~hostname.indexOf(':') ? "[" + hostname + "]" : hostname);
if (urlObj.port) {
host += ':' + urlObj.port;
}
}
if (query && typeof query === 'object') {
query = String(_querystring.urlQueryToSearchParams(query));
}
let search = urlObj.search || query && "?" + query || '';
if (protocol && !protocol.endsWith(':')) protocol += ':';
if (urlObj.slashes || (!protocol || slashedProtocols.test(protocol)) && host !== false) {
host = '//' + (host || '');
if (pathname && pathname[0] !== '/') pathname = '/' + pathname;
} else if (!host) {
host = '';
}
if (hash && hash[0] !== '#') hash = '#' + hash;
if (search && search[0] !== '?') search = '?' + search;
pathname = pathname.replace(/[?#]/g, encodeURIComponent);
search = search.replace('#', '%23');
return "" + protocol + host + pathname + search + hash;
}
const urlObjectKeys = [
'auth',
'hash',
'host',
'hostname',
'href',
'path',
'pathname',
'port',
'protocol',
'query',
'search',
'slashes'
];
function formatWithValidation(url) {
if (process.env.NODE_ENV === 'development') {
if (url !== null && typeof url === 'object') {
Object.keys(url).forEach((key)=>{
if (!urlObjectKeys.includes(key)) {
console.warn("Unknown key passed via urlObject into url.format: " + key);
}
});
}
}
return formatUrl(url);
}
//# sourceMappingURL=format-url.js.map