function expectQuery( url, expected ) { const searchParams = new URL( url ).searchParams; for ( const key of Object.keys( expected ) ) { expect( searchParams.get( key ) ).toBe( expected[ key ] ); } }
const app = function (req, res) { const parsedUrl = new URL(req.url) res.writeHead(200, { 'Content-Type': 'text/plain charset=UTF-8' }) switch (parsedUrl.pathname) { case '/users': return res.end('List of users') default: return res.end('No path match. Try /users') } }
rules.forEach(function (rule) { var name = rule.names[0].toLowerCase(); rule.information = new URL(homepage + "/blob/v" + version + "/doc/Rules.md#" + name); });
it('should support URL instances', async function() { const { server } = this.polly; server.any(this.recordUrl()).intercept((_, res) => res.sendStatus(200)); const res = await this.fetch(new URL(this.recordUrl())); expect(res.status).to.equal(200); });
export function useSite() { const site = useSelect( ( select ) => select( SITE_STORE ).getSite( window._currentSiteId ) ); const launchStatus = useSelect( ( select ) => select( SITE_STORE ).isLaunched( window._currentSiteId ) ); return { isPaidPlan: site && ! site.plan?.is_free, // sometimes plan will not be available: https://github.com/Automattic/wp-calypso/pull/44895 launchStatus, currentDomainName: site?.URL && new URL( site?.URL ).hostname, }; }
export default function buildUrl(...paths) { const url = new URL( paths .map(p => p && (p + '').trim()) // Trim each string .filter(Boolean) // Remove empty strings or other falsy paths .join('/') ); // Replace 2+ consecutive slashes with 1. (e.g. `///` --> `/`) url.set('pathname', url.pathname.replace(/\/{2,}/g, '/')); return url.href; }
export function imageSizeFromAttachments( post, imageUrl ) { if ( ! post.attachments ) { return; } const found = find( post.attachments, ( attachment ) => attachment.URL === imageUrl ); if ( found ) { return { width: found.width, height: found.height, }; } }
async loadNetworkResource(url, headers) { try { if (url.startsWith('file://')) { const fileURL = new URL(url); return await fsReadFile(fileURL, 'utf8'); } else { return null; } } catch (e) { return null; } }
export function getPermalinkBasePath( post ) { if ( ! post ) { return; } let path = post.URL; // if we have a permalink_URL, utlize that if ( ! isPublished( post ) && post.other_URLs && post.other_URLs.permalink_URL ) { path = post.other_URLs.permalink_URL; } return removeSlug( path ); }
function parseUrl(url) { const parsedUrl = new URL(url); /* Use the full origin (http://hostname:port) if the host exists. If there is no host, URL.origin returns "null" (null as a string) so set host to '/' */ const host = parsedUrl.host ? parsedUrl.origin : CHARS.SLASH; const path = parsedUrl.pathname || CHARS.SLASH; return { host, path }; }
export function getPagePath( post ) { if ( ! post ) { return; } if ( ! isPublished( post ) ) { return getPermalinkBasePath( post ); } return removeSlug( post.URL ); }
function attributeUrl(url, { source, medium }) { const attributedUrl = new URL(url); // Adding source attribution to the URL for Unsplash to track usage // https://help.unsplash.com/articles/2511315-guideline-attribution if (source) { attributedUrl.searchParams.set('utm_source', source); if (medium) { attributedUrl.searchParams.set('utm_medium', medium); } } return attributedUrl; }
function safeParseURL(url) { try { return new URL(url); } catch (err) { return null; } }
export default function buildUrl(...paths) { const url = new URL( paths .map(p => p && (p + '').trim()) // Trim each string .filter(Boolean) // Remove empty strings or other falsy paths .join('/') ); // Replace 2+ consecutive slashes with 1. (e.g. `///` --> `/`) url.set('pathname', url.pathname.replace(/\/{2,}/g, '/')); return url.href; }
const isUrl = function(path) { try { new URL(path); return true; } catch (err) { return false; } }