0
# Utilities
1
2
Path resolution and routing utility functions for advanced routing scenarios and custom integrations.
3
4
## Capabilities
5
6
### Path Resolution
7
8
Resolves relative paths against base paths, following directory-like navigation semantics.
9
10
```javascript { .api }
11
/**
12
* Resolves URIs as though every path is a directory, no files
13
* @param to - Target path (relative or absolute)
14
* @param base - Base path to resolve against
15
* @returns Resolved absolute path
16
*/
17
function resolve(to: string, base: string): string;
18
```
19
20
**Usage Examples:**
21
22
```javascript
23
import { resolve } from "@reach/router";
24
25
// Absolute paths pass through unchanged
26
resolve("/foo/bar", "/baz/qux"); // => "/foo/bar"
27
28
// Relative paths resolve against base
29
resolve("profile", "/users/123"); // => "/users/123/profile"
30
31
// Parent directory navigation
32
resolve("../settings", "/users/123/profile"); // => "/users/123/settings"
33
resolve("../../admin", "/users/123/profile"); // => "/admin"
34
35
// Query string handling
36
resolve("?tab=settings", "/users/123"); // => "/users/123?tab=settings"
37
```
38
39
### Parameter Interpolation
40
41
Inserts parameters into path templates with dynamic segments.
42
43
```javascript { .api }
44
/**
45
* Interpolates parameters into a path template
46
* @param path - Path template with parameter placeholders
47
* @param params - Object containing parameter values
48
* @returns Path with parameters interpolated
49
*/
50
function insertParams(path: string, params: Record<string, any>): string;
51
```
52
53
**Usage Examples:**
54
55
```javascript
56
import { insertParams } from "@reach/router";
57
58
// Basic parameter insertion
59
insertParams("/users/:userId", { userId: "123" });
60
// => "/users/123"
61
62
// Multiple parameters
63
insertParams("/users/:userId/posts/:postId", {
64
userId: "123",
65
postId: "456"
66
});
67
// => "/users/123/posts/456"
68
69
// With query parameters
70
insertParams("/search/:query", {
71
query: "react",
72
location: { search: "?type=posts" }
73
});
74
// => "/search/react?type=posts"
75
```
76
77
### Redirect Validation
78
79
Validates that redirect routes have matching dynamic segments.
80
81
```javascript { .api }
82
/**
83
* Validates that redirect from/to paths have matching dynamic segments
84
* @param from - Source path pattern
85
* @param to - Target path pattern
86
* @returns True if redirect is valid, false otherwise
87
*/
88
function validateRedirect(from: string, to: string): boolean;
89
```
90
91
**Usage Examples:**
92
93
```javascript
94
import { validateRedirect } from "@reach/router";
95
96
// Valid redirects - matching dynamic segments
97
validateRedirect("/old/:id", "/new/:id"); // => true
98
validateRedirect("/users/:userId/posts/:postId", "/u/:userId/p/:postId"); // => true
99
100
// Invalid redirects - mismatched segments
101
validateRedirect("/old/:id", "/new/:otherId"); // => false
102
validateRedirect("/users/:userId", "/posts/:postId"); // => false
103
104
// Static paths
105
validateRedirect("/old-path", "/new-path"); // => true
106
```
107
108
### Object Comparison
109
110
Performs shallow comparison of two objects for equality.
111
112
```javascript { .api }
113
/**
114
* Shallow compares two objects for equality
115
* @param obj1 - First object to compare
116
* @param obj2 - Second object to compare
117
* @returns True if objects have same keys and values, false otherwise
118
*/
119
function shallowCompare(obj1: object, obj2: object): boolean;
120
```
121
122
**Usage Examples:**
123
124
```javascript
125
import { shallowCompare } from "@reach/router";
126
127
// Equal objects
128
shallowCompare({ a: 1, b: 2 }, { a: 1, b: 2 }); // => true
129
130
// Different values
131
shallowCompare({ a: 1, b: 2 }, { a: 1, b: 3 }); // => false
132
133
// Different keys
134
shallowCompare({ a: 1, b: 2 }, { a: 1, c: 2 }); // => false
135
136
// Nested objects (shallow comparison)
137
shallowCompare(
138
{ a: { nested: true } },
139
{ a: { nested: true } }
140
); // => false (different object references)
141
```