Skip to content

Commit

Permalink
Merge pull request #13 from kaliberjs/language-param-name
Browse files Browse the repository at this point in the history
Add support for renaming the language param
  • Loading branch information
EECOLOR authored Dec 28, 2023
2 parents 7358c56 + aaa0afa commit 85bac58
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,16 @@ route1: {
}
```
The name of a parent routes' path param should be set to `language` in order for this pattern to work.
The name of a parent routes' path param should be set to `language` in order for this pattern to work. If you want to use a different name you need to provide this as configuration to the route map:

```js
asRouteMap(
{
...
},
{ languageParamName: 'locale' }
)
```

---
### Reverse route
Expand Down
8 changes: 5 additions & 3 deletions src/routeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ function interpolate(routePath, params) {
function pickFromChildren(pathSegments, children, previousParams = {}) {
const preparedChildren = children
.map(route => {
const path = normalizePath(route.path, previousParams.language)
const { languageParamName = 'language' } = route[routeSymbol].config
const path = normalizePath(route.path, previousParams[languageParamName])
if (path === null) return null

return { route, routeSegments: path.split('/') }
Expand Down Expand Up @@ -163,20 +164,21 @@ function createRoute(config, name, path, data, children, getParent) {
get parent() { return getParent() },
children,
name,
config,
},
})
}

function withReverseRoute(config, route) {
const { trailingSlash = false } = config
const { trailingSlash = false, languageParamName = 'language' } = config
return Object.assign(reverseRoute, route)

function reverseRoute(params = {}) {
const parentPaths = getParents(route).map(x => x.path)

const resolvedPath = [...parentPaths, route.path].reduce(
(base, path) => {
const { language } = params
const { [languageParamName]: language } = params
const normalizedPath = normalizePath(path, language)
if (normalizedPath === null) throwError(`Could not determine path from ${JSON.stringify(path)} with language ${language}`)
return resolve(normalizedPath, base, params)
Expand Down
21 changes: 21 additions & 0 deletions src/routeMap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ describe('asRouteMap', () => {
expect(map.language.a({ language: 'en' })).toBe('/en/b')
expect(pick('nl/b', [map, (_, x) => x])).toBe(null)
})
test('language - custom language param name', () => {
const map = asRouteMap(
{
language: {
path: ':locale',

a: { path: { nl: 'a', en: 'b' } } },
},
{
languageParamName: 'locale'
}
)

expect(pick('nl/a', [map, (_, x) => x])).toBe(map.language.a)
expect(map.language.a({ locale: 'nl' })).toBe('/nl/a')
expect(pick('en/a', [map, (_, x) => x])).toBe(null)

expect(pick('en/b', [map, (_, x) => x])).toBe(map.language.a)
expect(map.language.a({ locale: 'en' })).toBe('/en/b')
expect(pick('nl/b', [map, (_, x) => x])).toBe(null)
})
})
describe('reverse routing', () => {
test('path static', () => {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ type LanguageSupport = { language?: string }

export type Config = {
trailingSlash?: boolean
languageParamName?: string
}

0 comments on commit 85bac58

Please sign in to comment.