Skip to content

Commit

Permalink
refactor: simplify replaceDomain()
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Nov 6, 2024
1 parent ae7f9d4 commit 67189c9
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/commands/dev-env-sync-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,12 @@ import { getReadInterface } from '../lib/validations/line-by-line';
/**
* Replaces the domain in the given URL
*
* @param string str The URL to replace the domain in. Must be a valid URL.
* @param string str The URL to replace the domain in.
* @param string domain The new domain
* @return The URL with the new domain
*/
function replaceDomain( str: string, domain: string ): string {
const url = new URL( str );
url.hostname = domain;

// `URL.href()` always adds `/` for an empty path; we don't want that
const retval = url.href;
return retval.endsWith( '/' ) && ! str.endsWith( '/' )
? retval.substring( 0, retval.length - 1 )
: retval;
}
const replaceDomain = ( str: string, domain: string ): string =>
str.replace( /^([^:]+:\/\/)([^:/]+)/, `$1${ domain }` );

/**
* Strips the protocol from the URL
Expand Down

0 comments on commit 67189c9

Please sign in to comment.