Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto discovery #518

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/util/named-group-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const pattern = [
'[>\']',
// Get everything up to the end of the capture group: this is the RegExp used
// when matching URLs to this route, which we can use for validation purposes.
'([^\\)]*(\\))?)\\??',
'((?:[^)(]|\\((?:[^)(]|\\((?:[^)(]|\\([^)(]*\\))*\\))*\\))*)',
// Capture group end
'\\)',
].join( '' );
Expand Down
34 changes: 16 additions & 18 deletions lib/util/split-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@
*/
'use strict';

const namedGroupPattern = require( './named-group-regexp' ).pattern;

// Convert capture groups to non-matching groups, because all capture groups
// are included in the resulting array when an RE is passed to `.split()`
// (We re-use the existing named group's capture pattern instead of creating
// a new RegExp just for this purpose)
const patternWithoutSubgroups = namedGroupPattern
.replace( /([^\\])\(([^?])/g, '$1(?:$2' );

// Make a new RegExp using the same pattern as one single unified capture group,
// so the match as a whole will be preserved after `.split()`. Permit non-slash
// characters before or after the named capture group, although those components
// will not yield functioning setters.
const namedGroupRE = new RegExp( '([^/]*' + patternWithoutSubgroups + '[^/]*)' );
const namedGroupRE = require( './named-group-regexp' ).namedGroupRE;

/**
* Divide a route string up into hierarchical components by breaking it apart
Expand All @@ -29,14 +16,24 @@ const namedGroupRE = new RegExp( '([^/]*' + patternWithoutSubgroups + '[^/]*)' )
* @param {String} pathStr A route path string to break into components
* @returns {String[]} An array of route component strings
*/
module.exports = pathStr => pathStr
// Divide a string like "/some/path/(?P<with_named_groups>)/etc" into an
module.exports = pathStr => {
let parts = [pathStr];
// Find the named group.
const namedGroupMatch = pathStr.match(namedGroupRE);
if (namedGroupMatch) {
const namedGroup = namedGroupMatch[0];
// Split the string into the parts surrounding the named group.
parts = pathStr.split(namedGroup);
// Add the named group into the array.
parts.splice(1, 0, namedGroup);
}
// This divides a string like "/some/path/(?P<with_named_groups>)/etc" into an
// array `[ "/some/path/", "(?P<with_named_groups>)", "/etc" ]`.
.split( namedGroupRE )

// Then, reduce through the array of parts, splitting any non-capture-group
// parts on forward slashes and discarding empty strings to create the final
// array of path components.
.reduce( ( components, part ) => {
return parts.reduce( ( components, part ) => {
if ( ! part ) {
// Ignore empty strings parts
return components;
Expand All @@ -50,3 +47,4 @@ module.exports = pathStr => pathStr
// Split the part on / and filter out empty strings
return components.concat( part.split( '/' ).filter( Boolean ) );
}, [] );
}