-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.ts
315 lines (276 loc) · 8.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* `meiosis-routing/state`
*
* The `state` module contains functions for managing routes in the application state.
*
* @module state
*/
/**
* Route segment params.
*/
export type Params = Record<string, any>;
/**
* A route segment.
*/
export interface RouteSegment {
id: string;
params: Params;
}
/**
* A function that creates a [[RouteSegment]] with optional params.
*/
export type RouteParamFn = (params?: Record<string, any>) => RouteSegment;
/**
* A Route is an array of route segments.
*/
export type Route = RouteSegment[];
/**
* Convenience route segment lookup for route transitions.
*/
export interface RouteLookup {
[key: string]: RouteSegment;
}
/**
* The route transition indicates the [[Route]] that the user is leaving, to which they are
* arriving, and for which parameters have changed.
*/
export interface RouteTransition {
leave: RouteLookup;
arrive: RouteLookup;
}
/**
* Routing instance with navigation objects and functions.
*/
export interface RoutingObject {
/** the current [[Route]]. */
route: Route;
/** the route index, used internally. */
index: number;
/** the [[RouteSegment]] for the local route. */
localSegment: RouteSegment;
/** the [[RouteSegment]] for the child route. */
childSegment: RouteSegment;
/** returns the [[RoutingObject]] for the next child. */
next: () => RoutingObject;
/** returns the parent [[Route]]. */
parentRoute: () => Route;
/** returns the [[Route]] for the current route plus the given child route. */
childRoute: (child: Route | RouteSegment) => Route;
/** returns the [[Route]] for the current route plus the given sibling route. */
siblingRoute: (sibling: Route | RouteSegment) => Route;
/** returns the same route with the given params. */
sameRoute: (params: Record<string, any>) => Route;
}
////////
// fastDeepEqual credit: https://github.com/epoberezkin/fast-deep-equal
// This version does not handle Date and RegExp, because we shouldn't have those types when
// parsing a URL string.
const isArray = Array.isArray;
const keyList = Object.keys;
const hasProp = Object.prototype.hasOwnProperty;
const fastDeepEqual = (a, b): boolean => {
if (a === b) return true;
if (a && b && typeof a == "object" && typeof b == "object") {
const arrA = isArray(a),
arrB = isArray(b);
let i, length, key;
if (arrA && arrB) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0; ) if (!fastDeepEqual(a[i], b[i])) return false;
return true;
}
if (arrA != arrB) return false;
const keys = keyList(a);
length = keys.length;
if (length !== keyList(b).length) return false;
for (i = length; i-- !== 0; ) if (!hasProp.call(b, keys[i])) return false;
for (i = length; i-- !== 0; ) {
key = keys[i];
if (!fastDeepEqual(a[key], b[key])) return false;
}
return true;
}
return a !== a && b !== b;
};
const defaultEmpty = (route: Route | null): Route => (Array.isArray(route) ? route : []);
/**
* Creates a `Route` helper with functions to create route segments.
*
* @param routeNames the list of route names.
* @returns a `Route` object with constructor functions.
*
* @example
*
* ```
*
* const Route = createRouteSegments(["Home", "User"]);
*
* Route.Home()
* // { id: "Home", params: {} }
*
* Route.User({ name: "duck" })
* // { id: "User", params: { name: "duck" } }
* ```
*/
export function createRouteSegments(routeNames: string[]): Record<string, RouteParamFn> {
return routeNames.reduce((result, id): Record<string, RouteParamFn> => {
result[id] = (params: Record<string, any>): RouteSegment => ({
id,
params: params == null ? {} : params
});
return result;
}, {});
}
/**
* Looks for a route segment, with matching params, in a route.
*
* @param route the route to search.
* @param routeSegmentWithParams the route segment to search for in the route.
* @returns the matching Route segment, or `undefined` if `route` is empty or the route segment
* was not found.
*/
export function findRouteSegmentWithParams(
route: Route | null,
routeSegmentWithParams: RouteSegment
): RouteSegment | undefined {
return defaultEmpty(route).find(
(routeSegment: RouteSegment): boolean =>
routeSegment.id === routeSegmentWithParams.id &&
fastDeepEqual(routeSegment.params, routeSegmentWithParams.params)
);
}
/**
* Looks for a Route segment, regardless of the params, in a route.
*
* @param route the route to search.
* @param id the route segment, or just the id of the route segment, to search for in the route.
* @returns the matching Route segment, or `undefined` if `route` is empty or a route segment with
* the given id was not found.
*/
export function findRouteSegment(
route: Route | null,
id: RouteSegment | string
): RouteSegment | undefined {
const findId = (id as RouteSegment).id || id;
return defaultEmpty(route).find((routeSegment): boolean => routeSegment.id === findId);
}
/**
* Calculates the difference between two routes.
*
* @param from
* @param to
* @returns the route representing the segments that are in the `from` route but not in the `to`
* route.
*/
export function diffRoute(from: Route | null, to: Route | null): RouteLookup {
const init: RouteLookup = {};
return defaultEmpty(from).reduce(
(result: RouteLookup, fromRouteSegment: RouteSegment): RouteLookup => {
if (findRouteSegmentWithParams(to, fromRouteSegment) === undefined) {
result[fromRouteSegment.id] = fromRouteSegment;
}
return result;
},
init
);
}
/**
* Calculates route transitions, providing `leave`, `arrive`, and `params` to indicate the route
* segments for the route that we are leaving, the route to which we are arriving, and the route
* for which params have changed, respectively.
*
* @param currentRoute the current route, before navigation.
* @param nextRoute the route to which we are navigating.
* @returns an object with `leave`, `arrive`, and `params` properties.
*/
export function routeTransition(currentRoute: Route, nextRoute: Route): RouteTransition {
return {
leave: diffRoute(currentRoute, nextRoute),
arrive: diffRoute(nextRoute, currentRoute)
};
}
/**
* Calls a function with a value only if the value is not `null` or `undefined`.
*
* @param value the value to check.
* @param fn the function to call if `value` is present.
* @returns the result of calling `fn(value)`, or `null` if `value` is absent.
*/
export function whenPresent(value: any, fn: (x: any) => any): any {
return value != null ? fn(value) : null;
}
/**
* @constructor [[RoutingObject]]
*
* @param route the current route, for example `state.route.current`.
* @param index the route segment index. This is used internally and you should not specify a value
* for this parameter.
*
* @example
*
* ```
*
* // in root component
* const Root = ({ state }) => {
* const routing = Routing(state.route.current);
* const Component = componentMap[routing.localSegment.id];
*
* return (
* <div>
* <Component // other props... // routing={routing} />
* </div>
* );
* };
*
* // in child component
* const Child = ({ state, routing }) => {
* const Component = componentMap[routing.childSegment.id];
* const params = routing.localSegment.params;
*
* return (
* <div>
* <a href={router.toPath(routing.parentRoute())}>...</a>
* <a href={router.toPath(routing.childRoute(Route.Child()))}>...</a>
* <a href={router.toPath(
* routing.siblingRoute([Route.Sibling(), Route.Details()])
* )}>...</a>
*
* <Component // other props... // routing={routing.next()} />
* </div>
* );
* };
* ```
*/
export function Routing(route: Route = [], index = 0): RoutingObject {
return {
route,
index,
localSegment: route[index] === undefined ? { id: "", params: {} } : route[index],
childSegment: route[index + 1] === undefined ? { id: "", params: {} } : route[index + 1],
next: (): RoutingObject => Routing(route, index + 1),
parentRoute: (): Route => route.slice(0, index),
childRoute: (child: Route | RouteSegment): Route => route.slice(0, index + 1).concat(child),
siblingRoute: (sibling: Route | RouteSegment): Route => route.slice(0, index).concat(sibling),
sameRoute: (params: Record<string, any>): Route =>
route
.slice(0, index)
.concat({ id: route[index].id, params })
.concat(route.slice(index + 1))
};
}
/**
* Convenience function which puts the given route into an object of the form
* `{ route }`.
*/
export function navigateTo(route: Route | RouteSegment): any {
return { route: Array.isArray(route) ? route : [route] };
}
/**
* Convenience function which creates a `navigateTo` action.
*/
export function Actions(update: (any) => void): any {
return {
navigateTo: (route: Route | RouteSegment): void => update(navigateTo(route))
};
}