@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
71 lines (69 loc) • 3.22 kB
JavaScript
import { checkIsURLAbsolute } from "../utils/checkIsURLAbsolute.mjs";
import { internationalization } from "@intlayer/config/built";
//#region src/localization/getPathWithoutLocale.ts
/**
* True when the build-time routing mode is known and is not a prefix-based
* mode (neither 'prefix-all' nor 'prefix-no-default').
*/
const TREE_SHAKE_PREFIX_MODES = process.env["INTLAYER_ROUTING_MODE"] && process.env["INTLAYER_ROUTING_MODE"] !== "prefix-all" && process.env["INTLAYER_ROUTING_MODE"] !== "prefix-no-default";
/**
* True when the build-time routing mode is known and is NOT 'search-params'.
*/
const TREE_SHAKE_SEARCH_PARAMS = process.env["INTLAYER_ROUTING_MODE"] && process.env["INTLAYER_ROUTING_MODE"] !== "search-params";
/**
* Removes the locale segment from the given URL or pathname if present.
* Also removes locale from search parameters if present.
*
* This function get the locales from the configuration if not provided.
*
* Example:
*
* ```ts
* getPathWithoutLocale('/en/dashboard') // Returns '/dashboard'
* getPathWithoutLocale('/fr/dashboard') // Returns '/dashboard'
* getPathWithoutLocale('/dashboard') // Returns '/dashboard'
* getPathWithoutLocale('dashboard') // Returns 'dashboard'
* getPathWithoutLocale('/dashboard?locale=fr') // Returns '/dashboard'
* getPathWithoutLocale('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'
* getPathWithoutLocale('https://example.com/fr/dashboard') // Returns 'https://example.com/dashboard'
* getPathWithoutLocale('https://example.com/dashboard') // Returns 'https://example.com/dashboard'
* getPathWithoutLocale('https://example.com/dashboard?locale=fr') // Returns 'https://example.com/dashboard'
* ```
*
* @param inputUrl - The complete URL string or pathname to process.
* @param locales - Optional array of supported locales. Defaults to `localesDefault`.
* @returns The URL string or pathname without the locale segment or locale search parameter.
*/
/**
* The return type is narrowed to a template-literal type when both `inputUrl`
* and `locales` are string literals known at compile time. Absolute URLs fall
* back to `string`.
*/
const getPathWithoutLocale = (inputUrl, locales = internationalization?.locales) => {
const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);
let fixedInputUrl = inputUrl;
if (inputUrl?.endsWith("/")) fixedInputUrl = inputUrl.slice(0, -1);
const url = isAbsoluteUrl ? new URL(fixedInputUrl) : new URL(fixedInputUrl, "http://example.com");
const pathname = url.pathname;
if (!pathname.startsWith("/")) url.pathname = `/${pathname}`;
if (!TREE_SHAKE_PREFIX_MODES) {
const pathSegments = pathname.split("/");
const firstSegment = pathSegments[1];
if (locales?.includes(firstSegment)) {
pathSegments.splice(1, 1);
url.pathname = pathSegments.join("/") ?? "/";
}
}
if (!TREE_SHAKE_SEARCH_PARAMS) {
const searchParams = new URLSearchParams(url.search);
if (searchParams.has("locale")) {
searchParams.delete("locale");
url.search = searchParams.toString();
}
}
if (isAbsoluteUrl) return url.toString();
return url.toString().replace("http://example.com", "");
};
//#endregion
export { getPathWithoutLocale };
//# sourceMappingURL=getPathWithoutLocale.mjs.map