url#UrlObject TypeScript Examples
The following examples show how to use
url#UrlObject.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Routing.utils.ts From frontend.ro with MIT License | 7 votes |
static isRouteActive(router: NextRouter, url: UrlObject) {
// This is a naive implementation based on what we need right now.
// In other words, we found out that only those 2 checks get the job done.
// We may need to add additional checks, depending on future usecases.
if (url.pathname !== undefined && router.pathname !== url.pathname) {
return false;
}
if (
url.query !== undefined
&& Object.keys(url.query).some((key) => url.query[key] !== router.query[key])
) {
return false;
}
return true;
}
Example #2
Source File: translateUrl.ts From next-translate-routes with MIT License | 6 votes |
translateUrl: TTranslateUrl = ((url, locale, options) => {
const { defaultLocale } = getNtrData()
// Handle external urls
const parsedUrl: UrlObject = typeof url === 'string' ? parseUrl(url) : url
if (parsedUrl.host) {
if (typeof window === 'undefined' || parsedUrl.host !== parseUrl(window.location.href).host) {
return url
}
}
const translatedPath = translatePath(url, locale, options)
if (typeof translatedPath === 'object') {
return translatedPath
}
const prefix = locale === defaultLocale || options?.withoutLangPrefix ? '' : `/${locale}`
return normalizePathTrailingSlash(prefix + translatedPath)
}) as typeof translatePath
Example #3
Source File: translateUrl.ts From next-translate-routes with MIT License | 5 votes |
export function translatePath(url: Url, locale?: string, { format }: Options = {}): Url {
const { routesTree } = getNtrData()
const returnFormat = format || typeof url
const urlObject = typeof url === 'object' ? (url as UrlObject) : parseUrl(url, true)
const { pathname, query, hash } = urlObject
if (!pathname || !locale) {
return returnFormat === 'object' ? url : formatUrl(url)
}
const pathParts = removeLangPrefix(pathname, true)
const { translatedPathParts, augmentedQuery = {} } = translatePathParts({
locale,
pathParts,
query: parseQuery(typeof query === 'string' ? query : stringifyQuery(query || {})),
routeBranch: routesTree,
})
const path = translatedPathParts.join('/')
const compiledPath = compilePath(path, { validate: false })(augmentedQuery)
const paramNames = (parsePath(path).filter((token) => typeof token === 'object') as Key[]).map((token) => token.name)
const remainingQuery = Object.keys(augmentedQuery).reduce(
(acc, key) => ({
...acc,
...(paramNames.includes(key)
? {}
: { [key]: (typeof query === 'object' && query?.[key]) || augmentedQuery[key] }),
}),
{},
)
const translatedPathname = `${routesTree.paths[locale] ? `/${routesTree.paths[locale]}` : ''}/${compiledPath}`
const translatedUrlObject = {
...urlObject,
hash,
pathname: translatedPathname,
query: remainingQuery,
}
return returnFormat === 'object' ? translatedUrlObject : formatUrl(translatedUrlObject)
}