| 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 |
1×
31×
13×
18×
18×
1×
3×
17×
16×
21×
21×
2×
1×
19×
5×
14×
1×
9×
1×
1×
70×
5×
1×
96×
96×
201×
201×
201×
102×
102×
102×
201×
198×
198×
198×
198×
201×
68×
28×
1×
96×
96×
28×
68×
40×
28×
1×
55×
55×
41×
14×
1×
96×
96×
41×
55×
| import { matchPattern } from './PatternUtils'
function deepEqual(a, b) {
if (a == b)
return true
Iif (a == null || b == null)
return false
if (Array.isArray(a)) {
return Array.isArray(b) && a.length === b.length && a.every(function (item, index) {
return deepEqual(item, b[index])
})
}
if (typeof a === 'object') {
for (let p in a) {
Iif (!a.hasOwnProperty(p)) {
continue
}
if (a[p] === undefined) {
if (b[p] !== undefined) {
return false
}
} else if (!b.hasOwnProperty(p)) {
return false
} else if (!deepEqual(a[p], b[p])) {
return false
}
}
return true
}
return String(a) === String(b)
}
function paramsAreActive(paramNames, paramValues, activeParams) {
// FIXME: This doesn't work on repeated params in activeParams.
return paramNames.every(function (paramName, index) {
return String(paramValues[index]) === String(activeParams[paramName])
})
}
function getMatchingRouteIndex(pathname, activeRoutes, activeParams) {
let remainingPathname = pathname, paramNames = [], paramValues = []
for (let i = 0, len = activeRoutes.length; i < len; ++i) {
const route = activeRoutes[i]
const pattern = route.path || ''
if (pattern.charAt(0) === '/') {
remainingPathname = pathname
paramNames = []
paramValues = []
}
if (remainingPathname !== null) {
const matched = matchPattern(pattern, remainingPathname)
remainingPathname = matched.remainingPathname
paramNames = [ ...paramNames, ...matched.paramNames ]
paramValues = [ ...paramValues, ...matched.paramValues ]
}
if (
remainingPathname === '' &&
route.path &&
paramsAreActive(paramNames, paramValues, activeParams)
)
return i
}
return null
}
/**
* Returns true if the given pathname matches the active routes
* and params.
*/
function routeIsActive(pathname, routes, params, indexOnly) {
const i = getMatchingRouteIndex(pathname, routes, params)
if (i === null) {
// No match.
return false
} else if (!indexOnly) {
// Any match is good enough.
return true
}
// If any remaining routes past the match index have paths, then we can't
// be on the index route.
return routes.slice(i + 1).every(route => !route.path)
}
/**
* Returns true if all key/value pairs in the given query are
* currently active.
*/
function queryIsActive(query, activeQuery) {
Iif (activeQuery == null)
return query == null
if (query == null)
return true
return deepEqual(query, activeQuery)
}
/**
* Returns true if a <Link> to the given pathname/query combination is
* currently active.
*/
function isActive(pathname, query, indexOnly, location, routes, params) {
Iif (location == null)
return false
if (!routeIsActive(pathname, routes, params, indexOnly))
return false
return queryIsActive(query, location.query)
}
export default isActive
|