UNPKG

next

Version:

The React Framework

92 lines (91 loc) 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatUrl = formatUrl; exports.formatWithValidation = formatWithValidation; exports.urlObjectKeys = void 0; var querystring = _interopRequireWildcard(require("./querystring")); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for(var key in obj){ if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } 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', ]; exports.urlObjectKeys = urlObjectKeys; function formatWithValidation(url) { if (process.env.NODE_ENV === 'development') { if (url !== null && typeof url === 'object') { Object.keys(url).forEach((key)=>{ if (urlObjectKeys.indexOf(key) === -1) { console.warn(`Unknown key passed via urlObject into url.format: ${key}`); } }); } } return formatUrl(url); } //# sourceMappingURL=format-url.js.map