File size: 767 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Given an absolute or relative URL and search terms, returns a relative URL including
 * the search query parameter and preserving existing parameters.
 * @param  uri    URL or path to modify
 * @param  search Search terms
 * @returns        Path including search terms
 */
export function buildRelativeSearchUrl( uri: string, search: string ): string {
	// We only care about the relative part, but the URL API only deals with absolute URLs,
	// so we need to provide a dummy domain.
	const parsedUrl = new URL( uri, 'http://dummy.example' );

	if ( search ) {
		parsedUrl.searchParams.set( 's', search );
	} else {
		parsedUrl.searchParams.delete( 's' );
	}

	return `${ parsedUrl.pathname }${ parsedUrl.search }${ parsedUrl.hash }`.replace( /%20/g, '+' );
}