/
githubmirror
/
wp-calypso
Обзор
Документация
Войти
/
githubmirror
/
wp-calypso
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
client/lib/url/http-utils.ts
58 строк
2 KB
valterlorran
SSH Migration: Add celebration screen to the dashboard (#106629)
22 окт 2025, 16:14
Не верифицирован
22 окт 2025, 16:14
081b1fb
Код
Авторство
О чём код?
import { Falsy } from 'utility-types'; import { URL as TypedURL, SiteSlug } from 'calypso/types'; const urlWithoutHttpRegex = /^https?:\/\//; /** * Returns the supplied URL without the initial http(s). * @param url The URL to remove http(s) from * @returns URL without the initial http(s) */ export function withoutHttp( url: '' ): ''; export function withoutHttp( url: Falsy ): null; export function withoutHttp( url: TypedURL ): TypedURL; export function withoutHttp( url: TypedURL | Falsy ): TypedURL | null { if ( url === '' ) { return ''; } if ( ! url ) { return null; } return url.replace( urlWithoutHttpRegex, '' ); } export function urlToSlug( url: Falsy ): null; export function urlToSlug( url: TypedURL ): SiteSlug; export function urlToSlug( url: TypedURL | Falsy ): SiteSlug | null { if ( ! url ) { return null; } return withoutHttp( url ).replace( /\//g, '::' ); } /** * Removes the `http(s)://` part and the trailing slash from a URL. * "http://blog.wordpress.com" will be converted into "blog.wordpress.com". * "https://www.wordpress.com/blog/" will be converted into "www.wordpress.com/blog". * @param urlToConvert The URL to convert * @returns The URL's domain and path */ export function urlToDomainAndPath( urlToConvert: TypedURL ): TypedURL { return withoutHttp( urlToConvert ).replace( /\/$/, '' ); } /** * Removes the protocol and path from a URL, returning only the domain. * Removes http(s):// prefix and all trailing slashes. * "http://blog.wordpress.com" will be converted into "blog.wordpress.com". * "https://www.wordpress.com/blog/" will be converted into "www.wordpress.com". * "https://example.com///" will be converted into "example.com". * @param urlToConvert The URL to convert * @returns The URL without protocol and trailing slashes */ export function urlToDomain( urlToConvert: TypedURL ): TypedURL { return withoutHttp( urlToConvert ).replace( /\/+$/, '' ); }