Skip to content

Commit

Permalink
Remove compileUrl, use compile from path-to-regexp instead
Browse files Browse the repository at this point in the history
  • Loading branch information
willybrauner committed Nov 9, 2023
1 parent 72cd057 commit 28c21b4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/core/LangService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Routers } from "./Routers"
import { compileUrl, createUrl } from "./core"
import { createUrl } from "./core"
import { isSSR, joinPaths, removeLastCharFromString } from "./helpers"
import { TRoute } from "../components/Router"
import debug from "@cher-ami/debug"
import { compile } from "path-to-regexp"

const log = debug(`router:LangService`)

Expand Down Expand Up @@ -183,7 +184,7 @@ class LangService<TLang = any> {
removeLastCharFromString(location.pathname, "/", true) === this.base
) {
// prepare path and build URL
const newUrl = compileUrl(joinPaths([this.base, "/:lang"]), {
const newUrl = compile(joinPaths([this.base, "/:lang"]))({
lang: this.browserLang.key,
})
log("redirect: to browser language >", { newUrl })
Expand Down Expand Up @@ -213,7 +214,7 @@ class LangService<TLang = any> {
removeLastCharFromString(location.pathname, "/", true) === this.base
) {
// prepare path & build new URL
const newUrl = compileUrl(joinPaths([this.base, "/:lang"]), {
const newUrl = compile(joinPaths([this.base, "/:lang"]))({
lang: this.defaultLang.key,
})
log("redirect to default lang >", { newUrl })
Expand Down
17 changes: 2 additions & 15 deletions src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { compile, match } from "path-to-regexp"
import { TRoute } from "../components/Router"
import LangService from "./LangService"
import { joinPaths, removeLastCharFromString } from "./helpers"
import { al, ar } from "vitest/dist/reporters-5f784f42"

const componentName: string = "core"
const log = debug(`router:${componentName}`)
Expand Down Expand Up @@ -509,15 +508,6 @@ export function formatRoutes(

// ----------------------------------------------------------------------------- URLS / PATH

/**
* Compile an URL with path and params via path-to-regex
* ex:
* compile("foo/:id")({id: example-client}) // "foo/example-client"
*/
export function compileUrl(path: string, params?: TParams): string {
return compile(path)(params)
}

/**
* returns lang path
* (a 'langPath' is '/about' or '/a-propos' in path: {en: "/about", fr: "/a-propos", de: "uber", name: "about"})
Expand All @@ -530,11 +520,8 @@ export function getLangPath(
lang: string = Routers.langService?.currentLang.key,
) {
let path
if (typeof langPath === "string") {
path = langPath
} else if (typeof langPath === "object") {
path = langPath?.[lang]
}
if (typeof langPath === "string") path = langPath
else if (typeof langPath === "object") path = langPath?.[lang]

const removeLangFromPath = (path: string): string => {
if (path?.includes(`/:lang`)) {
Expand Down

0 comments on commit 28c21b4

Please sign in to comment.