@tanstack/router-core 0.0.1-alpha.5 → 0.0.1-alpha.7

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.
Files changed (40) hide show
  1. package/build/cjs/packages/router-core/src/index.js +33 -1451
  2. package/build/cjs/packages/router-core/src/index.js.map +1 -1
  3. package/build/cjs/packages/router-core/src/path.js +222 -0
  4. package/build/cjs/packages/router-core/src/path.js.map +1 -0
  5. package/build/cjs/packages/router-core/src/qss.js +1 -1
  6. package/build/cjs/packages/router-core/src/qss.js.map +1 -1
  7. package/build/cjs/packages/router-core/src/route.js +126 -0
  8. package/build/cjs/packages/router-core/src/route.js.map +1 -0
  9. package/build/cjs/packages/router-core/src/routeConfig.js +69 -0
  10. package/build/cjs/packages/router-core/src/routeConfig.js.map +1 -0
  11. package/build/cjs/packages/router-core/src/routeMatch.js +260 -0
  12. package/build/cjs/packages/router-core/src/routeMatch.js.map +1 -0
  13. package/build/cjs/packages/router-core/src/router.js +787 -0
  14. package/build/cjs/packages/router-core/src/router.js.map +1 -0
  15. package/build/cjs/packages/router-core/src/searchParams.js +70 -0
  16. package/build/cjs/packages/router-core/src/searchParams.js.map +1 -0
  17. package/build/cjs/packages/router-core/src/utils.js +118 -0
  18. package/build/cjs/packages/router-core/src/utils.js.map +1 -0
  19. package/build/esm/index.js +1304 -1238
  20. package/build/esm/index.js.map +1 -1
  21. package/build/stats-html.html +1 -1
  22. package/build/stats-react.json +374 -57
  23. package/build/types/index.d.ts +361 -333
  24. package/build/umd/index.development.js +1313 -1238
  25. package/build/umd/index.development.js.map +1 -1
  26. package/build/umd/index.production.js +1 -1
  27. package/build/umd/index.production.js.map +1 -1
  28. package/package.json +2 -3
  29. package/src/frameworks.ts +13 -0
  30. package/src/index.ts +15 -3054
  31. package/src/link.ts +289 -0
  32. package/src/path.ts +236 -0
  33. package/src/qss.ts +1 -1
  34. package/src/route.ts +181 -0
  35. package/src/routeConfig.ts +523 -0
  36. package/src/routeInfo.ts +228 -0
  37. package/src/routeMatch.ts +357 -0
  38. package/src/router.ts +1182 -0
  39. package/src/searchParams.ts +54 -0
  40. package/src/utils.ts +157 -0
@@ -0,0 +1,54 @@
1
+ import { decode, encode } from './qss'
2
+ import { AnySearchSchema } from './routeConfig'
3
+
4
+ export const defaultParseSearch = parseSearchWith(JSON.parse)
5
+ export const defaultStringifySearch = stringifySearchWith(JSON.stringify)
6
+
7
+ export function parseSearchWith(parser: (str: string) => any) {
8
+ return (searchStr: string): AnySearchSchema => {
9
+ if (searchStr.substring(0, 1) === '?') {
10
+ searchStr = searchStr.substring(1)
11
+ }
12
+
13
+ let query: Record<string, unknown> = decode(searchStr)
14
+
15
+ // Try to parse any query params that might be json
16
+ for (let key in query) {
17
+ const value = query[key]
18
+ if (typeof value === 'string') {
19
+ try {
20
+ query[key] = parser(value)
21
+ } catch (err) {
22
+ //
23
+ }
24
+ }
25
+ }
26
+
27
+ return query
28
+ }
29
+ }
30
+
31
+ export function stringifySearchWith(stringify: (search: any) => string) {
32
+ return (search: Record<string, any>) => {
33
+ search = { ...search }
34
+
35
+ if (search) {
36
+ Object.keys(search).forEach((key) => {
37
+ const val = search[key]
38
+ if (typeof val === 'undefined' || val === undefined) {
39
+ delete search[key]
40
+ } else if (val && typeof val === 'object' && val !== null) {
41
+ try {
42
+ search[key] = stringify(val)
43
+ } catch (err) {
44
+ // silent
45
+ }
46
+ }
47
+ })
48
+ }
49
+
50
+ const searchStr = encode(search as Record<string, string>).toString()
51
+
52
+ return searchStr ? `?${searchStr}` : ''
53
+ }
54
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,157 @@
1
+ export type NoInfer<T> = [T][T extends any ? 0 : never]
2
+ export type IsAny<T, Y, N> = 1 extends 0 & T ? Y : N
3
+ export type IsAnyBoolean<T> = 1 extends 0 & T ? true : false
4
+ export type IsKnown<T, Y, N> = unknown extends T ? N : Y
5
+ export type PickAsRequired<T, K extends keyof T> = Omit<T, K> &
6
+ Required<Pick<T, K>>
7
+ export type PickAsPartial<T, K extends keyof T> = Omit<T, K> &
8
+ Partial<Pick<T, K>>
9
+ export type PickUnsafe<T, K> = K extends keyof T ? Pick<T, K> : never
10
+ export type PickExtra<T, K> = Expand<{
11
+ [TKey in keyof K as string extends TKey
12
+ ? never
13
+ : TKey extends keyof T
14
+ ? never
15
+ : TKey]: K[TKey]
16
+ }>
17
+ export type PickRequired<T> = {
18
+ [K in keyof T as undefined extends T[K] ? never : K]: T[K]
19
+ }
20
+
21
+ export type Expand<T> = T extends object
22
+ ? T extends infer O
23
+ ? { [K in keyof O]: O[K] }
24
+ : never
25
+ : T
26
+
27
+ // type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
28
+ // k: infer I,
29
+ // ) => any
30
+ // ? I
31
+ // : never
32
+
33
+ export type Values<O> = O[ValueKeys<O>]
34
+ export type ValueKeys<O> = Extract<keyof O, PropertyKey>
35
+
36
+ export type DeepAwaited<T> = T extends Promise<infer A>
37
+ ? DeepAwaited<A>
38
+ : T extends Record<infer A, Promise<infer B>>
39
+ ? { [K in A]: DeepAwaited<B> }
40
+ : T
41
+
42
+ export type PathParamMask<TRoutePath extends string> =
43
+ TRoutePath extends `${infer L}/:${infer C}/${infer R}`
44
+ ? PathParamMask<`${L}/${string}/${R}`>
45
+ : TRoutePath extends `${infer L}/:${infer C}`
46
+ ? PathParamMask<`${L}/${string}`>
47
+ : TRoutePath
48
+
49
+ export type Timeout = ReturnType<typeof setTimeout>
50
+
51
+ export type Updater<TPrevious, TResult = TPrevious> =
52
+ | TResult
53
+ | ((prev?: TPrevious) => TResult)
54
+
55
+ export type PickExtract<T, U> = {
56
+ [K in keyof T as T[K] extends U ? K : never]: T[K]
57
+ }
58
+
59
+ export type PickExclude<T, U> = {
60
+ [K in keyof T as T[K] extends U ? never : K]: T[K]
61
+ }
62
+
63
+ /**
64
+ * This function returns `a` if `b` is deeply equal.
65
+ * If not, it will replace any deeply equal children of `b` with those of `a`.
66
+ * This can be used for structural sharing between JSON values for example.
67
+ */
68
+ export function replaceEqualDeep(prev: any, next: any) {
69
+ if (prev === next) {
70
+ return prev
71
+ }
72
+
73
+ const array = Array.isArray(prev) && Array.isArray(next)
74
+
75
+ if (array || (isPlainObject(prev) && isPlainObject(next))) {
76
+ const aSize = array ? prev.length : Object.keys(prev).length
77
+ const bItems = array ? next : Object.keys(next)
78
+ const bSize = bItems.length
79
+ const copy: any = array ? [] : {}
80
+
81
+ let equalItems = 0
82
+
83
+ for (let i = 0; i < bSize; i++) {
84
+ const key = array ? i : bItems[i]
85
+ copy[key] = replaceEqualDeep(prev[key], next[key])
86
+ if (copy[key] === prev[key]) {
87
+ equalItems++
88
+ }
89
+ }
90
+
91
+ return aSize === bSize && equalItems === aSize ? prev : copy
92
+ }
93
+
94
+ return next
95
+ }
96
+
97
+ // Copied from: https://github.com/jonschlinkert/is-plain-object
98
+ function isPlainObject(o: any) {
99
+ if (!hasObjectPrototype(o)) {
100
+ return false
101
+ }
102
+
103
+ // If has modified constructor
104
+ const ctor = o.constructor
105
+ if (typeof ctor === 'undefined') {
106
+ return true
107
+ }
108
+
109
+ // If has modified prototype
110
+ const prot = ctor.prototype
111
+ if (!hasObjectPrototype(prot)) {
112
+ return false
113
+ }
114
+
115
+ // If constructor does not have an Object-specific method
116
+ if (!prot.hasOwnProperty('isPrototypeOf')) {
117
+ return false
118
+ }
119
+
120
+ // Most likely a plain Object
121
+ return true
122
+ }
123
+
124
+ function hasObjectPrototype(o: any) {
125
+ return Object.prototype.toString.call(o) === '[object Object]'
126
+ }
127
+
128
+ export function last<T>(arr: T[]) {
129
+ return arr[arr.length - 1]
130
+ }
131
+
132
+ export function warning(cond: any, message: string): cond is true {
133
+ if (cond) {
134
+ if (typeof console !== 'undefined') console.warn(message)
135
+
136
+ try {
137
+ throw new Error(message)
138
+ } catch {}
139
+ }
140
+
141
+ return true
142
+ }
143
+
144
+ function isFunction(d: any): d is Function {
145
+ return typeof d === 'function'
146
+ }
147
+
148
+ export function functionalUpdate<TResult>(
149
+ updater: Updater<TResult>,
150
+ previous: TResult,
151
+ ) {
152
+ if (isFunction(updater)) {
153
+ return updater(previous as TResult)
154
+ }
155
+
156
+ return updater
157
+ }