Skip to content

Commit

Permalink
push/replace accepts option for hash, assembles on url
Browse files Browse the repository at this point in the history
  • Loading branch information
stackoverfloweth committed Sep 22, 2024
1 parent 9814f1f commit 7fb8ee6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/services/createRouterResolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ test('when given an external route with params in host, interpolates param value
const url = resolve('external', { 'subdomain': 'router' })

expect(url).toBe('https://router.kitbag.dev/')
})

test('given a route with hash, interpolates hash value', () => {
const resolve = createRouterResolve(routes)

expect(resolve('parentA', { paramA: 'bar' }, { hash: 'foo' })).toBe('/parentA/bar#foo')
})
2 changes: 2 additions & 0 deletions src/services/createRouterResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AllPropertiesAreOptional } from '@/types/utilities'

export type RouterResolveOptions = {
query?: Record<string, string>,
hash?: string,
}

type RouterResolveArgs<
Expand Down Expand Up @@ -51,6 +52,7 @@ export function createRouterResolve<const TRoutes extends Routes>(routes: TRoute
const url = assembleUrl(match, {
params,
query: options.query,
hash: options.hash,
})

return url
Expand Down
1 change: 1 addition & 0 deletions src/services/createRouterRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type RouterRoute<TRoute extends ResolvedRoute = ResolvedRoute> = Readonly
matches: TRoute['matches'],
state: TRoute['state'],
query: ResolvedRouteQuery,
hash: TRoute['hash'],

Check failure on line 16 in src/services/createRouterRoute.ts

View workflow job for this annotation

GitHub Actions / Types

Type '"hash"' cannot be used to index type 'TRoute'.
params: Writable<TRoute['params']>,
update: RouteUpdate<TRoute>,
}>
Expand Down
11 changes: 11 additions & 0 deletions src/services/urlAssembly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,15 @@ describe('host params', () => {

expect(url).toBe('ABC.kitbag.dev/')
})
})

test('given route with hash, returns url with hash value interpolated', () => {
const route = createRoute({
name: 'simple',
path: '/',
})

const url = assembleUrl(route, { hash: 'foo' })

expect(url).toBe('/#foo')
})
6 changes: 5 additions & 1 deletion src/services/urlAssembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Route } from '@/types/route'
type AssembleUrlOptions = {
params?: Record<string, unknown>,
query?: Record<string, string>,
hash?: string,
}

export function assembleUrl(route: Route, options: AssembleUrlOptions = {}): string {
Expand All @@ -20,7 +21,10 @@ export function assembleUrl(route: Route, options: AssembleUrlOptions = {}): str
const pathWithParamsSet = assemblePathParamValues(route.path, paramValues)
const queryWithParamsSet = assembleQueryParamValues(route.query, paramValues)

return withQuery(`${hostWithParamsSet}${pathWithParamsSet}`, queryWithParamsSet, queryValues)
const url = withQuery(`${hostWithParamsSet}${pathWithParamsSet}`, queryWithParamsSet, queryValues)
const hash = options.hash ? `#${options.hash.replace(/^#/, '')}` : ''

return `${url}${hash}`
}

function assembleHostParamValues(host: Host, paramValues: Record<string, unknown>): string {
Expand Down
1 change: 1 addition & 0 deletions src/types/routerPush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type RouterPushOptions<
TState = unknown
> = {
query?: Record<string, string>,
hash?: string,
replace?: boolean,
state?: Partial<TState>,
}
Expand Down
1 change: 1 addition & 0 deletions src/types/routerReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type RouterReplaceOptions<
TState = unknown
> = {
query?: Record<string, string>,
hash?: string,
state?: Partial<TState>,
}

Expand Down

0 comments on commit 7fb8ee6

Please sign in to comment.