UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

73 lines (71 loc) 3.45 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('../_virtual/_rolldown/runtime.cjs'); const require_utils_checkIsURLAbsolute = require('../utils/checkIsURLAbsolute.cjs'); let _intlayer_config_built = require("@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 = _intlayer_config_built.internationalization?.locales) => { const isAbsoluteUrl = require_utils_checkIsURLAbsolute.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 exports.getPathWithoutLocale = getPathWithoutLocale; //# sourceMappingURL=getPathWithoutLocale.cjs.map