Skip to main content

RoutingHelper

The RoutingHelper object offers util functions for URL paths.

import { RoutingHelper } from '@archibald/core';

addLeadingSlash

This method adds a leading slash to the path if none is there.

RoutingHelper.addLeadingSlash(path);
NameTypeOptionalDescription
pathstring✔️URL path (e.g. en/users).

hasLeadingSlash

This method returns whether the path has a leading slash.

RoutingHelper.hasLeadingSlash(path);
NameTypeOptionalDescription
pathstring✔️URL path (e.g. /en/users).

fixTrailingSlash

This method fixes the trailing slash of a path. It is expected that the path does not contain a query.

RoutingHelper.fixTrailingSlash(path);
NameTypeDescription
pathstringPath which should be fixed.

startsWithPrefix

This method returns whether the path starts with the given route prefix.

RoutingHelper.startsWithPrefix(path, routePrefix);
NameTypeOptionalDescription
pathstring✔️URL path to check.
routePrefixstring✔️Route prefix if available.

endsWithSlash

This method returns whether the path ends with a slash.

RoutingHelper.endsWithSlash(path);
NameTypeOptionalDescription
pathstring✔️URL path to check.

addPrefixToPath

This method prepends the route prefix to the path (unless the path already starts with it) and cleans up the result.

RoutingHelper.addPrefixToPath(routePrefix, path, removeTrailingSlash);
NameTypeOptionalDescription
routePrefixstring✔️Route prefix if available.
pathstring✔️URL path.
removeTrailingSlashboolean✔️Toggle if the trailing slash should be removed. Default: true.

removePrefixFromPath

This method cleans the pathname from a configured route prefix.

RoutingHelper.removePrefixFromPath(path, routePrefix, removeTrailingSlash);
NameTypeOptionalDescription
pathstringURL path (e.g. /en/users).
routePrefixstring✔️The prefix string of the route.
removeTrailingSlashboolean✔️Toggle if the trailing slash should be removed. Default: true.

cleanUpPath

This method removes multiple neighbouring slashes from the path (e.g. /de//p////123/ becomes /de/p/123) and removes the trailing slash by default.

RoutingHelper.cleanUpPath(path, removeTrailingSlash);
NameTypeOptionalDescription
pathstring✔️URL path which should be cleaned.
removeTrailingSlashboolean✔️Toggle if the trailing slash should be removed. Default: true.

extractLanguage

This method extracts the language of a given url.

RoutingHelper.extractLanguage(path, routePrefix);
NameTypeOptionalDescription
pathstring✔️Path of which language should be extracted.
routePrefixstring✔️Route prefix if available.

fixLanguagePath

This method checks if the path contains a supported language, otherwise redirects to a supported one. It returns { url, language }.

RoutingHelper.fixLanguagePath(languages, path, preferredLanguage, routePrefix);
NameTypeOptionalDescription
languagesstring[]Accepted languages.
pathstringPath which should be fixed.
preferredLanguagestring✔️Preferred language which should be redirected to.
routePrefixstring✔️Route prefix if available.