* Get the name of the route matching the current window URL, or, given a route name * and parameters, check if the current window URL and parameters match that route. * * @example * // at URL https://ziggy.dev/posts/4 with 'posts.show' route 'posts/{post}' * route().current(
(name, params)
| 122 | * @return {(Boolean|String|undefined)} |
| 123 | */ |
| 124 | current(name, params) { |
| 125 | const { name: current, params: currentParams, query, route } = this._unresolve(); |
| 126 | |
| 127 | // If a name wasn't passed, return the name of the current route |
| 128 | if (!name) return current; |
| 129 | |
| 130 | // Test the passed name against the current route, matching some |
| 131 | // basic wildcards, e.g. passing `events.*` matches `events.show` |
| 132 | const match = new RegExp(`^${name.replace(/\./g, '\\.').replace(/\*/g, '.*')}$`).test( |
| 133 | current, |
| 134 | ); |
| 135 | |
| 136 | if ([null, undefined].includes(params) || !match) return match; |
| 137 | |
| 138 | const routeObject = new Route(current, route, this._config); |
| 139 | |
| 140 | params = this._parse(params, routeObject); |
| 141 | const routeParams = { ...currentParams, ...query }; |
| 142 | |
| 143 | // If the current window URL has no route parameters, and the passed parameters are empty, return true |
| 144 | if ( |
| 145 | Object.values(params).every((p) => !p) && |
| 146 | !Object.values(routeParams).some((v) => v !== undefined) |
| 147 | ) |
| 148 | return true; |
| 149 | |
| 150 | const isSubset = (subset, full) => { |
| 151 | return Object.entries(subset).every(([key, value]) => { |
| 152 | if (Array.isArray(value) && Array.isArray(full[key])) { |
| 153 | return value.every( |
| 154 | (v) => full[key].includes(v) || full[key].includes(decodeURIComponent(v)), |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | if ( |
| 159 | typeof value === 'object' && |
| 160 | typeof full[key] === 'object' && |
| 161 | value !== null && |
| 162 | full[key] !== null |
| 163 | ) { |
| 164 | return isSubset(value, full[key]); |
| 165 | } |
| 166 | |
| 167 | return full[key] == value || full[key] == decodeURIComponent(value); |
| 168 | }); |
| 169 | }; |
| 170 | |
| 171 | // Check that all passed parameters match their values in the current window URL |
| 172 | // Use weak equality because all values in the current window URL will be strings |
| 173 | return isSubset(params, routeParams); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get an object representing the current location (by default this will be |
nothing calls this directly
no test coverage detected