@tanstack/react-router 0.0.1-beta.284 → 0.0.1-beta.286

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.284",
4
+ "version": "0.0.1-beta.286",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -44,7 +44,7 @@
44
44
  "@tanstack/store": "^0.1.3",
45
45
  "tiny-invariant": "^1.3.1",
46
46
  "tiny-warning": "^1.0.3",
47
- "@tanstack/history": "0.0.1-beta.284"
47
+ "@tanstack/history": "0.0.1-beta.286"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "rollup --config rollup.config.js"
package/src/Matches.tsx CHANGED
@@ -289,6 +289,12 @@ function getRenderedMatches(state: RouterState) {
289
289
  : state.matches
290
290
  }
291
291
 
292
+ function removeUnderscores(s?: string) {
293
+ if (s === rootRouteId) return s
294
+
295
+ return s?.replace(/(^_|_$)/, '').replace(/(\/_|_\/)/, '/')
296
+ }
297
+
292
298
  export function useMatch<
293
299
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
294
300
  TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
@@ -309,11 +315,18 @@ export function useMatch<
309
315
 
310
316
  const matchRouteId = (() => {
311
317
  const matches = getRenderedMatches(router.state)
312
- const match = opts?.from
313
- ? matches.find((d) => d.routeId === opts?.from)
314
- : matches.find((d) => d.id === nearestMatchId)
315
- return match!.routeId
316
- })()
318
+
319
+ if (!opts.from) {
320
+ return matches.find((d) => d.id === nearestMatchId)
321
+ }
322
+
323
+ const from = removeUnderscores(opts.from)
324
+
325
+ return (
326
+ matches.find((d) => d.routeId === from) ??
327
+ matches.find((d) => d.routeId === from?.replace(/\/$/, ''))
328
+ )
329
+ })()?.routeId
317
330
 
318
331
  if (opts?.strict ?? true) {
319
332
  invariant(
package/src/link.tsx CHANGED
@@ -172,11 +172,9 @@ export type SearchParamOptions<
172
172
  TFromSearchEnsured = '/' extends TFrom
173
173
  ? FullSearchSchema<TRouteTree>
174
174
  : Expand<
175
- UnionToIntersection<
176
175
  PickRequired<
177
176
  RouteByPath<TRouteTree, TFrom>['types']['fullSearchSchema']
178
177
  >
179
- >
180
178
  >,
181
179
  TFromSearchOptional = Omit<
182
180
  FullSearchSchema<TRouteTree>,
package/src/routeInfo.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AnyRoute, Route } from './route'
2
- import { Expand, UnionToIntersection } from './utils'
2
+ import { Expand, UnionToIntersection, UnionToTuple } from './utils'
3
3
 
4
4
  export type ParseRoute<TRouteTree extends AnyRoute> =
5
5
  | TRouteTree
@@ -59,9 +59,23 @@ export type RoutePaths<TRouteTree extends AnyRoute> =
59
59
  | ParseRoute<TRouteTree>['fullPath']
60
60
  | '/'
61
61
 
62
+ type UnionizeCollisions<T, U> = {
63
+ [P in keyof T & keyof U]: T[P] extends U[P] ? T[P] : T[P] | U[P]
64
+ }
65
+ type Reducer<T, U, C = UnionizeCollisions<T, U>> = C &
66
+ Omit<T, keyof C> &
67
+ Omit<U, keyof C>
68
+
69
+ type Reduce<T extends any[], Result = unknown> = T extends [
70
+ infer First,
71
+ ...infer Rest,
72
+ ]
73
+ ? Reduce<Rest, Reducer<Result, First>>
74
+ : Result
75
+
62
76
  export type FullSearchSchema<TRouteTree extends AnyRoute> = Partial<
63
77
  Expand<
64
- UnionToIntersection<ParseRoute<TRouteTree>['types']['fullSearchSchema']>
78
+ Reduce<UnionToTuple<ParseRoute<TRouteTree>['types']['fullSearchSchema']>>
65
79
  >
66
80
  >
67
81
 
package/src/utils.ts CHANGED
@@ -133,6 +133,16 @@ export type PickExclude<T, U> = {
133
133
  [K in keyof T as T[K] extends U ? never : K]: T[K]
134
134
  }
135
135
 
136
+ // from https://github.com/type-challenges/type-challenges/issues/737
137
+ type LastInUnion<U> = UnionToIntersection<
138
+ U extends unknown ? (x: U) => 0 : never
139
+ > extends (x: infer L) => 0
140
+ ? L
141
+ : never
142
+ export type UnionToTuple<U, Last = LastInUnion<U>> = [U] extends [never]
143
+ ? []
144
+ : [...UnionToTuple<Exclude<U, Last>>, Last]
145
+
136
146
  //
137
147
 
138
148
  export const isServer = typeof document === 'undefined'