@tanstack/react-router 0.0.1-beta.83 → 1.0.0
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/LICENSE +1 -1
- package/build/cjs/CatchBoundary.js +128 -0
- package/build/cjs/CatchBoundary.js.map +1 -0
- package/build/cjs/Matches.js +233 -0
- package/build/cjs/Matches.js.map +1 -0
- package/build/cjs/RouterProvider.js +170 -0
- package/build/cjs/RouterProvider.js.map +1 -0
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +2 -4
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/build/cjs/_virtual/with-selector.development.js +16 -0
- package/build/cjs/_virtual/with-selector.development.js.map +1 -0
- package/build/cjs/_virtual/with-selector.js +16 -0
- package/build/cjs/_virtual/with-selector.js.map +1 -0
- package/build/cjs/_virtual/with-selector.production.min.js +16 -0
- package/build/cjs/_virtual/with-selector.production.min.js.map +1 -0
- package/build/cjs/awaited.js +43 -0
- package/build/cjs/awaited.js.map +1 -0
- package/build/cjs/build/esm/index.js +79 -0
- package/build/cjs/build/esm/index.js.map +1 -0
- package/build/cjs/defer.js +37 -0
- package/build/cjs/defer.js.map +1 -0
- package/build/cjs/fileRoute.js +27 -0
- package/build/cjs/fileRoute.js.map +1 -0
- package/build/cjs/index.js +113 -451
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/lazyRouteComponent.js +54 -0
- package/build/cjs/lazyRouteComponent.js.map +1 -0
- package/build/cjs/link.js +223 -0
- package/build/cjs/link.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/@tanstack_react-store@0.2.1_react-dom@18.2.0_react@18.2.0/node_modules/@tanstack/react-store/build/modern/index.js +47 -0
- package/build/cjs/node_modules/.pnpm/@tanstack_react-store@0.2.1_react-dom@18.2.0_react@18.2.0/node_modules/@tanstack/react-store/build/modern/index.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/@tanstack_store@0.1.3/node_modules/@tanstack/store/build/modern/index.js +70 -0
- package/build/cjs/node_modules/.pnpm/@tanstack_store@0.1.3/node_modules/@tanstack/store/build/modern/index.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js +188 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js +39 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.min.js.map +1 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/shim/with-selector.js +26 -0
- package/build/cjs/node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/shim/with-selector.js.map +1 -0
- package/build/cjs/packages/react-router/src/CatchBoundary.js +123 -0
- package/build/cjs/packages/react-router/src/CatchBoundary.js.map +1 -0
- package/build/cjs/packages/react-router/src/Matches.js +235 -0
- package/build/cjs/packages/react-router/src/Matches.js.map +1 -0
- package/build/cjs/packages/react-router/src/RouterProvider.js +144 -0
- package/build/cjs/packages/react-router/src/RouterProvider.js.map +1 -0
- package/build/cjs/packages/react-router/src/awaited.js +43 -0
- package/build/cjs/packages/react-router/src/awaited.js.map +1 -0
- package/build/cjs/packages/react-router/src/defer.js +37 -0
- package/build/cjs/packages/react-router/src/defer.js.map +1 -0
- package/build/cjs/packages/react-router/src/fileRoute.js +27 -0
- package/build/cjs/packages/react-router/src/fileRoute.js.map +1 -0
- package/build/cjs/packages/react-router/src/index.js +61 -0
- package/build/cjs/packages/react-router/src/index.js.map +1 -0
- package/build/cjs/packages/react-router/src/lazyRouteComponent.js +54 -0
- package/build/cjs/packages/react-router/src/lazyRouteComponent.js.map +1 -0
- package/build/cjs/packages/react-router/src/link.js +148 -0
- package/build/cjs/packages/react-router/src/link.js.map +1 -0
- package/build/cjs/packages/react-router/src/path.js +209 -0
- package/build/cjs/packages/react-router/src/path.js.map +1 -0
- package/build/cjs/packages/react-router/src/qss.js +63 -0
- package/build/cjs/packages/react-router/src/qss.js.map +1 -0
- package/build/cjs/packages/react-router/src/react.js +634 -0
- package/build/cjs/packages/react-router/src/react.js.map +1 -0
- package/build/cjs/packages/react-router/src/redirects.js +25 -0
- package/build/cjs/packages/react-router/src/redirects.js.map +1 -0
- package/build/cjs/packages/react-router/src/route.js +134 -0
- package/build/cjs/packages/react-router/src/route.js.map +1 -0
- package/build/cjs/packages/react-router/src/router.js +1111 -0
- package/build/cjs/packages/react-router/src/router.js.map +1 -0
- package/build/cjs/packages/react-router/src/scroll-restoration.js +53 -0
- package/build/cjs/packages/react-router/src/scroll-restoration.js.map +1 -0
- package/build/cjs/packages/react-router/src/searchParams.js +81 -0
- package/build/cjs/packages/react-router/src/searchParams.js.map +1 -0
- package/build/cjs/packages/react-router/src/useBlocker.js +61 -0
- package/build/cjs/packages/react-router/src/useBlocker.js.map +1 -0
- package/build/cjs/packages/react-router/src/useNavigate.js +75 -0
- package/build/cjs/packages/react-router/src/useNavigate.js.map +1 -0
- package/build/cjs/packages/react-router/src/useParams.js +26 -0
- package/build/cjs/packages/react-router/src/useParams.js.map +1 -0
- package/build/cjs/packages/react-router/src/useSearch.js +25 -0
- package/build/cjs/packages/react-router/src/useSearch.js.map +1 -0
- package/build/cjs/packages/react-router/src/utils.js +239 -0
- package/build/cjs/packages/react-router/src/utils.js.map +1 -0
- package/build/cjs/path.js +214 -0
- package/build/cjs/path.js.map +1 -0
- package/build/cjs/qss.js +63 -0
- package/build/cjs/qss.js.map +1 -0
- package/build/cjs/react/CatchBoundary.js +123 -0
- package/build/cjs/react/CatchBoundary.js.map +1 -0
- package/build/cjs/react/awaited.js +43 -0
- package/build/cjs/react/awaited.js.map +1 -0
- package/build/cjs/react/defer.js +37 -0
- package/build/cjs/react/defer.js.map +1 -0
- package/build/cjs/react.js +650 -0
- package/build/cjs/react.js.map +1 -0
- package/build/cjs/redirects.js +28 -0
- package/build/cjs/redirects.js.map +1 -0
- package/build/cjs/route.js +191 -0
- package/build/cjs/route.js.map +1 -0
- package/build/cjs/router.js +1085 -0
- package/build/cjs/router.js.map +1 -0
- package/build/cjs/routerConfig.js +209 -0
- package/build/cjs/routerConfig.js.map +1 -0
- package/build/cjs/scroll-restoration.js +202 -0
- package/build/cjs/scroll-restoration.js.map +1 -0
- package/build/cjs/searchParams.js +81 -0
- package/build/cjs/searchParams.js.map +1 -0
- package/build/cjs/src/CatchBoundary.js +126 -0
- package/build/cjs/src/CatchBoundary.js.map +1 -0
- package/build/cjs/src/Matches.js +235 -0
- package/build/cjs/src/Matches.js.map +1 -0
- package/build/cjs/src/RouterProvider.js +1051 -0
- package/build/cjs/src/RouterProvider.js.map +1 -0
- package/build/cjs/src/awaited.js +45 -0
- package/build/cjs/src/awaited.js.map +1 -0
- package/build/cjs/src/defer.js +39 -0
- package/build/cjs/src/defer.js.map +1 -0
- package/build/cjs/src/fileRoute.js +29 -0
- package/build/cjs/src/fileRoute.js.map +1 -0
- package/build/cjs/src/index.js +134 -0
- package/build/cjs/src/index.js.map +1 -0
- package/build/cjs/src/lazyRouteComponent.js +57 -0
- package/build/cjs/src/lazyRouteComponent.js.map +1 -0
- package/build/cjs/src/link.js +151 -0
- package/build/cjs/src/link.js.map +1 -0
- package/build/cjs/src/path.js +211 -0
- package/build/cjs/src/path.js.map +1 -0
- package/build/cjs/src/qss.js +65 -0
- package/build/cjs/src/qss.js.map +1 -0
- package/build/cjs/src/redirects.js +27 -0
- package/build/cjs/src/redirects.js.map +1 -0
- package/build/cjs/src/route.js +139 -0
- package/build/cjs/src/route.js.map +1 -0
- package/build/cjs/src/router.js +203 -0
- package/build/cjs/src/router.js.map +1 -0
- package/build/cjs/src/scroll-restoration.js +186 -0
- package/build/cjs/src/scroll-restoration.js.map +1 -0
- package/build/cjs/src/searchParams.js +83 -0
- package/build/cjs/src/searchParams.js.map +1 -0
- package/build/cjs/src/useBlocker.js +64 -0
- package/build/cjs/src/useBlocker.js.map +1 -0
- package/build/cjs/src/useNavigate.js +78 -0
- package/build/cjs/src/useNavigate.js.map +1 -0
- package/build/cjs/src/useParams.js +28 -0
- package/build/cjs/src/useParams.js.map +1 -0
- package/build/cjs/src/useSearch.js +27 -0
- package/build/cjs/src/useSearch.js.map +1 -0
- package/build/cjs/src/utils.js +230 -0
- package/build/cjs/src/utils.js.map +1 -0
- package/build/cjs/useBlocker.js +55 -0
- package/build/cjs/useBlocker.js.map +1 -0
- package/build/cjs/useNavigate.js +86 -0
- package/build/cjs/useNavigate.js.map +1 -0
- package/build/cjs/useParams.js +26 -0
- package/build/cjs/useParams.js.map +1 -0
- package/build/cjs/useSearch.js +25 -0
- package/build/cjs/useSearch.js.map +1 -0
- package/build/cjs/useStore.js +99 -0
- package/build/cjs/useStore.js.map +1 -0
- package/build/cjs/utils.js +241 -0
- package/build/cjs/utils.js.map +1 -0
- package/build/esm/index.js +2581 -337
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +3494 -2700
- package/build/stats-react.json +1134 -87
- package/build/types/CatchBoundary.d.ts +36 -0
- package/build/types/Matches.d.ts +64 -0
- package/build/types/RouteMatch.d.ts +23 -0
- package/build/types/RouterProvider.d.ts +35 -0
- package/build/types/awaited.d.ts +9 -0
- package/build/types/defer.d.ts +19 -0
- package/build/types/fileRoute.d.ts +38 -0
- package/build/types/history.d.ts +7 -0
- package/build/types/index.d.ts +911 -88
- package/build/types/injectHtml.d.ts +0 -0
- package/build/types/lazyRouteComponent.d.ts +2 -0
- package/build/types/link.d.ts +93 -0
- package/build/types/location.d.ts +12 -0
- package/build/types/path.d.ts +17 -0
- package/build/types/qss.d.ts +2 -0
- package/build/types/react/CatchBoundary.d.ts +33 -0
- package/build/types/react/awaited.d.ts +9 -0
- package/build/types/react/defer.d.ts +19 -0
- package/build/types/react.d.ts +141 -0
- package/build/types/redirects.d.ts +11 -0
- package/build/types/route.d.ts +283 -0
- package/build/types/routeInfo.d.ts +31 -0
- package/build/types/router.d.ts +186 -0
- package/build/types/scroll-restoration.d.ts +18 -0
- package/build/types/searchParams.d.ts +7 -0
- package/build/types/useBlocker.d.ts +9 -0
- package/build/types/useNavigate.d.ts +19 -0
- package/build/types/useParams.d.ts +7 -0
- package/build/types/useSearch.d.ts +7 -0
- package/build/types/useStore.d.ts +12 -0
- package/build/types/utils.d.ts +69 -0
- package/build/umd/index.development.js +2829 -1796
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +4 -24
- package/build/umd/index.production.js.map +1 -1
- package/package.json +9 -7
- package/src/CatchBoundary.tsx +101 -0
- package/src/Matches.tsx +423 -0
- package/src/RouterProvider.tsx +252 -0
- package/src/awaited.tsx +40 -0
- package/src/defer.ts +55 -0
- package/src/fileRoute.ts +152 -0
- package/src/history.ts +8 -0
- package/src/index.tsx +28 -747
- package/src/lazyRouteComponent.tsx +33 -0
- package/src/link.tsx +603 -0
- package/src/location.ts +13 -0
- package/src/path.ts +261 -0
- package/src/qss.ts +53 -0
- package/src/redirects.ts +39 -0
- package/src/route.ts +882 -0
- package/src/routeInfo.ts +84 -0
- package/src/router.ts +1671 -0
- package/src/scroll-restoration.tsx +230 -0
- package/src/searchParams.ts +79 -0
- package/src/useBlocker.tsx +27 -0
- package/src/useNavigate.tsx +111 -0
- package/src/useParams.tsx +25 -0
- package/src/useSearch.tsx +25 -0
- package/src/utils.ts +360 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { useMatch } from './Matches'
|
|
3
|
+
import { RouteMatch } from './Matches'
|
|
4
|
+
import { AnyRoute } from './route'
|
|
5
|
+
import { ParseRoute, RouteIds, RoutesById, RouteById } from './routeInfo'
|
|
6
|
+
import { RegisteredRouter } from './router'
|
|
7
|
+
|
|
8
|
+
export type NoInfer<T> = [T][T extends any ? 0 : never]
|
|
9
|
+
export type IsAny<T, Y, N = T> = 1 extends 0 & T ? Y : N
|
|
10
|
+
export type IsAnyBoolean<T> = 1 extends 0 & T ? true : false
|
|
11
|
+
export type IsKnown<T, Y, N> = unknown extends T ? N : Y
|
|
12
|
+
export type PickAsRequired<T, K extends keyof T> = Omit<T, K> &
|
|
13
|
+
Required<Pick<T, K>>
|
|
14
|
+
export type PickAsPartial<T, K extends keyof T> = Omit<T, K> &
|
|
15
|
+
Partial<Pick<T, K>>
|
|
16
|
+
export type PickUnsafe<T, K> = K extends keyof T ? Pick<T, K> : never
|
|
17
|
+
export type PickExtra<T, K> = {
|
|
18
|
+
[TKey in keyof K as string extends TKey
|
|
19
|
+
? never
|
|
20
|
+
: TKey extends keyof T
|
|
21
|
+
? never
|
|
22
|
+
: TKey]: K[TKey]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type PickRequired<T> = {
|
|
26
|
+
[K in keyof T as undefined extends T[K] ? never : K]: T[K]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// export type Expand<T> = T
|
|
30
|
+
export type Expand<T> = T extends object
|
|
31
|
+
? T extends infer O
|
|
32
|
+
? { [K in keyof O]: O[K] }
|
|
33
|
+
: never
|
|
34
|
+
: T
|
|
35
|
+
|
|
36
|
+
export type UnionToIntersection<U> = (
|
|
37
|
+
U extends any ? (k: U) => void : never
|
|
38
|
+
) extends (k: infer I) => any
|
|
39
|
+
? I
|
|
40
|
+
: never
|
|
41
|
+
|
|
42
|
+
// type Compute<T> = { [K in keyof T]: T[K] } | never
|
|
43
|
+
|
|
44
|
+
// type AllKeys<T> = T extends any ? keyof T : never
|
|
45
|
+
|
|
46
|
+
// export type MergeUnion<T, Keys extends keyof T = keyof T> = Compute<
|
|
47
|
+
// {
|
|
48
|
+
// [K in Keys]: T[Keys]
|
|
49
|
+
// } & {
|
|
50
|
+
// [K in AllKeys<T>]?: T extends any
|
|
51
|
+
// ? K extends keyof T
|
|
52
|
+
// ? T[K]
|
|
53
|
+
// : never
|
|
54
|
+
// : never
|
|
55
|
+
// }
|
|
56
|
+
// >
|
|
57
|
+
|
|
58
|
+
export type Assign<Left, Right> = Omit<Left, keyof Right> & Right
|
|
59
|
+
|
|
60
|
+
export type AssignAll<T extends any[]> = T extends [infer Left, ...infer Right]
|
|
61
|
+
? Right extends any[]
|
|
62
|
+
? Assign<Left, AssignAll<Right>>
|
|
63
|
+
: Left
|
|
64
|
+
: {}
|
|
65
|
+
|
|
66
|
+
// // Sample types to merge
|
|
67
|
+
// type TypeA = {
|
|
68
|
+
// shared: string
|
|
69
|
+
// onlyInA: string
|
|
70
|
+
// nested: {
|
|
71
|
+
// shared: string
|
|
72
|
+
// aProp: string
|
|
73
|
+
// }
|
|
74
|
+
// array: string[]
|
|
75
|
+
// }
|
|
76
|
+
|
|
77
|
+
// type TypeB = {
|
|
78
|
+
// shared: number
|
|
79
|
+
// onlyInB: number
|
|
80
|
+
// nested: {
|
|
81
|
+
// shared: number
|
|
82
|
+
// bProp: number
|
|
83
|
+
// }
|
|
84
|
+
// array: number[]
|
|
85
|
+
// }
|
|
86
|
+
|
|
87
|
+
// type TypeC = {
|
|
88
|
+
// shared: boolean
|
|
89
|
+
// onlyInC: boolean
|
|
90
|
+
// nested: {
|
|
91
|
+
// shared: boolean
|
|
92
|
+
// cProp: boolean
|
|
93
|
+
// }
|
|
94
|
+
// array: boolean[]
|
|
95
|
+
// }
|
|
96
|
+
|
|
97
|
+
// type Test = Expand<Assign<TypeA, TypeB>>
|
|
98
|
+
|
|
99
|
+
// // Using DeepMerge to merge TypeA and TypeB
|
|
100
|
+
// type MergedType = Expand<AssignAll<[TypeA, TypeB, TypeC]>>
|
|
101
|
+
|
|
102
|
+
export type Values<O> = O[ValueKeys<O>]
|
|
103
|
+
export type ValueKeys<O> = Extract<keyof O, PropertyKey>
|
|
104
|
+
|
|
105
|
+
export type DeepAwaited<T> = T extends Promise<infer A>
|
|
106
|
+
? DeepAwaited<A>
|
|
107
|
+
: T extends Record<infer A, Promise<infer B>>
|
|
108
|
+
? { [K in A]: DeepAwaited<B> }
|
|
109
|
+
: T
|
|
110
|
+
|
|
111
|
+
export type PathParamMask<TRoutePath extends string> =
|
|
112
|
+
TRoutePath extends `${infer L}/$${infer C}/${infer R}`
|
|
113
|
+
? PathParamMask<`${L}/${string}/${R}`>
|
|
114
|
+
: TRoutePath extends `${infer L}/$${infer C}`
|
|
115
|
+
? PathParamMask<`${L}/${string}`>
|
|
116
|
+
: TRoutePath
|
|
117
|
+
|
|
118
|
+
export type Timeout = ReturnType<typeof setTimeout>
|
|
119
|
+
|
|
120
|
+
export type Updater<TPrevious, TResult = TPrevious> =
|
|
121
|
+
| TResult
|
|
122
|
+
| ((prev?: TPrevious) => TResult)
|
|
123
|
+
|
|
124
|
+
export type NonNullableUpdater<TPrevious, TResult = TPrevious> =
|
|
125
|
+
| TResult
|
|
126
|
+
| ((prev: TPrevious) => TResult)
|
|
127
|
+
|
|
128
|
+
export type PickExtract<T, U> = {
|
|
129
|
+
[K in keyof T as T[K] extends U ? K : never]: T[K]
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type PickExclude<T, U> = {
|
|
133
|
+
[K in keyof T as T[K] extends U ? never : K]: T[K]
|
|
134
|
+
}
|
|
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
|
+
|
|
146
|
+
//
|
|
147
|
+
|
|
148
|
+
export const isServer = typeof document === 'undefined'
|
|
149
|
+
|
|
150
|
+
export function last<T>(arr: T[]) {
|
|
151
|
+
return arr[arr.length - 1]
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function isFunction(d: any): d is Function {
|
|
155
|
+
return typeof d === 'function'
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function functionalUpdate<TResult>(
|
|
159
|
+
updater: Updater<TResult> | NonNullableUpdater<TResult>,
|
|
160
|
+
previous: TResult,
|
|
161
|
+
): TResult {
|
|
162
|
+
if (isFunction(updater)) {
|
|
163
|
+
return updater(previous as TResult)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return updater
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K> {
|
|
170
|
+
return keys.reduce((obj: any, key: K) => {
|
|
171
|
+
obj[key] = parent[key]
|
|
172
|
+
return obj
|
|
173
|
+
}, {} as any)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* This function returns `a` if `b` is deeply equal.
|
|
178
|
+
* If not, it will replace any deeply equal children of `b` with those of `a`.
|
|
179
|
+
* This can be used for structural sharing between immutable JSON values for example.
|
|
180
|
+
* Do not use this with signals
|
|
181
|
+
*/
|
|
182
|
+
export function replaceEqualDeep<T>(prev: any, _next: T): T {
|
|
183
|
+
if (prev === _next) {
|
|
184
|
+
return prev
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const next = _next as any
|
|
188
|
+
|
|
189
|
+
const array = Array.isArray(prev) && Array.isArray(next)
|
|
190
|
+
|
|
191
|
+
if (array || (isPlainObject(prev) && isPlainObject(next))) {
|
|
192
|
+
const prevSize = array ? prev.length : Object.keys(prev).length
|
|
193
|
+
const nextItems = array ? next : Object.keys(next)
|
|
194
|
+
const nextSize = nextItems.length
|
|
195
|
+
const copy: any = array ? [] : {}
|
|
196
|
+
|
|
197
|
+
let equalItems = 0
|
|
198
|
+
|
|
199
|
+
for (let i = 0; i < nextSize; i++) {
|
|
200
|
+
const key = array ? i : nextItems[i]
|
|
201
|
+
copy[key] = replaceEqualDeep(prev[key], next[key])
|
|
202
|
+
if (copy[key] === prev[key]) {
|
|
203
|
+
equalItems++
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return prevSize === nextSize && equalItems === prevSize ? prev : copy
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return next
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Copied from: https://github.com/jonschlinkert/is-plain-object
|
|
214
|
+
export function isPlainObject(o: any) {
|
|
215
|
+
if (!hasObjectPrototype(o)) {
|
|
216
|
+
return false
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// If has modified constructor
|
|
220
|
+
const ctor = o.constructor
|
|
221
|
+
if (typeof ctor === 'undefined') {
|
|
222
|
+
return true
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// If has modified prototype
|
|
226
|
+
const prot = ctor.prototype
|
|
227
|
+
if (!hasObjectPrototype(prot)) {
|
|
228
|
+
return false
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// If constructor does not have an Object-specific method
|
|
232
|
+
if (!prot.hasOwnProperty('isPrototypeOf')) {
|
|
233
|
+
return false
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Most likely a plain Object
|
|
237
|
+
return true
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function hasObjectPrototype(o: any) {
|
|
241
|
+
return Object.prototype.toString.call(o) === '[object Object]'
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function deepEqual(a: any, b: any, partial: boolean = false): boolean {
|
|
245
|
+
if (a === b) {
|
|
246
|
+
return true
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (typeof a !== typeof b) {
|
|
250
|
+
return false
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
254
|
+
const aKeys = Object.keys(a)
|
|
255
|
+
const bKeys = Object.keys(b)
|
|
256
|
+
|
|
257
|
+
if (!partial && aKeys.length !== bKeys.length) {
|
|
258
|
+
return false
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return !bKeys.some(
|
|
262
|
+
(key) => !(key in a) || !deepEqual(a[key], b[key], partial),
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
267
|
+
return !a.some((item, index) => !deepEqual(item, b[index], partial))
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return false
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function useStableCallback<T extends (...args: any[]) => any>(fn: T): T {
|
|
274
|
+
const fnRef = React.useRef(fn)
|
|
275
|
+
fnRef.current = fn
|
|
276
|
+
|
|
277
|
+
const ref = React.useRef((...args: any[]) => fnRef.current(...args))
|
|
278
|
+
return ref.current as T
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function shallow<T>(objA: T, objB: T) {
|
|
282
|
+
if (Object.is(objA, objB)) {
|
|
283
|
+
return true
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (
|
|
287
|
+
typeof objA !== 'object' ||
|
|
288
|
+
objA === null ||
|
|
289
|
+
typeof objB !== 'object' ||
|
|
290
|
+
objB === null
|
|
291
|
+
) {
|
|
292
|
+
return false
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const keysA = Object.keys(objA)
|
|
296
|
+
if (keysA.length !== Object.keys(objB).length) {
|
|
297
|
+
return false
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
for (let i = 0; i < keysA.length; i++) {
|
|
301
|
+
if (
|
|
302
|
+
!Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||
|
|
303
|
+
!Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])
|
|
304
|
+
) {
|
|
305
|
+
return false
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return true
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export type StrictOrFrom<TFrom> =
|
|
312
|
+
| {
|
|
313
|
+
from: TFrom
|
|
314
|
+
strict?: true
|
|
315
|
+
}
|
|
316
|
+
| {
|
|
317
|
+
from?: never
|
|
318
|
+
strict: false
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export type RouteFromIdOrRoute<
|
|
322
|
+
T,
|
|
323
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
324
|
+
> = T extends ParseRoute<TRouteTree>
|
|
325
|
+
? T
|
|
326
|
+
: T extends RouteIds<TRouteTree>
|
|
327
|
+
? RoutesById<TRouteTree>[T]
|
|
328
|
+
: T extends string
|
|
329
|
+
? RouteIds<TRouteTree>
|
|
330
|
+
: never
|
|
331
|
+
|
|
332
|
+
export function useRouteContext<
|
|
333
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
334
|
+
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
|
|
335
|
+
TStrict extends boolean = true,
|
|
336
|
+
TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'],
|
|
337
|
+
TSelected = TRouteContext,
|
|
338
|
+
>(
|
|
339
|
+
opts: StrictOrFrom<TFrom> & {
|
|
340
|
+
select?: (search: TRouteContext) => TSelected
|
|
341
|
+
},
|
|
342
|
+
): TStrict extends true ? TSelected : TSelected | undefined {
|
|
343
|
+
return useMatch({
|
|
344
|
+
...(opts as any),
|
|
345
|
+
select: (match: RouteMatch) =>
|
|
346
|
+
opts?.select
|
|
347
|
+
? opts.select(match.context as TRouteContext)
|
|
348
|
+
: match.context,
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export const useLayoutEffect =
|
|
353
|
+
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
|
|
354
|
+
|
|
355
|
+
export function escapeJSON(jsonString: string) {
|
|
356
|
+
return jsonString
|
|
357
|
+
.replace(/\\/g, '\\\\') // Escape backslashes
|
|
358
|
+
.replace(/'/g, "\\'") // Escape single quotes
|
|
359
|
+
.replace(/"/g, '\\"') // Escape double quotes
|
|
360
|
+
}
|