* @param on {string} current url * @param when {string} route when template to match the url against * @return {?Object}
(on, when)
| 7366 | * @return {?Object} |
| 7367 | */ |
| 7368 | function switchRouteMatcher(on, when) { |
| 7369 | // TODO(i): this code is convoluted and inefficient, we should construct the route matching |
| 7370 | // regex only once and then reuse it |
| 7371 | |
| 7372 | // Escape regexp special characters. |
| 7373 | when = '^' + when.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&") + '$'; |
| 7374 | var regex = '', |
| 7375 | params = [], |
| 7376 | dst = {}; |
| 7377 | |
| 7378 | var re = /:(\w+)/g, |
| 7379 | paramMatch, |
| 7380 | lastMatchedIndex = 0; |
| 7381 | |
| 7382 | while ((paramMatch = re.exec(when)) !== null) { |
| 7383 | // Find each :param in `when` and replace it with a capturing group. |
| 7384 | // Append all other sections of when unchanged. |
| 7385 | regex += when.slice(lastMatchedIndex, paramMatch.index); |
| 7386 | regex += '([^\\/]*)'; |
| 7387 | params.push(paramMatch[1]); |
| 7388 | lastMatchedIndex = re.lastIndex; |
| 7389 | } |
| 7390 | // Append trailing path part. |
| 7391 | regex += when.substr(lastMatchedIndex); |
| 7392 | |
| 7393 | var match = on.match(new RegExp(regex)); |
| 7394 | if (match) { |
| 7395 | forEach(params, function(name, index) { |
| 7396 | dst[name] = match[index + 1]; |
| 7397 | }); |
| 7398 | } |
| 7399 | return match ? dst : null; |
| 7400 | } |
| 7401 | |
| 7402 | function updateRoute() { |
| 7403 | var next = parseRoute(), |
no test coverage detected