Skip to content

Wildcard matching option for query string parameters #115

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

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules

# webstorm files
.idea
*.iml
28 changes: 22 additions & 6 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
var toPath = to.path,
from = $state.$current, fromParams = $state.params, fromPath = from.path;

var reloadWild = ($state.current.reloadWild === undefined || $state.current.reloadWild);

// Starting from the root of the path, keep all levels that haven't changed
var keep, state, locals = root.locals, toLocals = [];
for (keep = 0, state = toPath[keep];
state && state === fromPath[keep] && equalForKeys(toParams, fromParams, state.ownParams);
state && state === fromPath[keep] && equalForKeys(toParams, fromParams, state.ownParams, reloadWild);
keep++, state = toPath[keep]) {
locals = toLocals[keep] = state.locals;
}
Expand All @@ -182,12 +184,17 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
$state.transition = null;
return $q.when($state.current);
}

// Normalize/filter parameters before we pass them to event handlers etc.
var normalizedToParams = {};
forEach(to.params, function (name) {
var value = toParams[name];
normalizedToParams[name] = (value != null) ? String(value) : null;
if(name === '*'){
for(var key in toParams) {
normalizedToParams[key] = toParams[key];
}
} else {
var value = toParams[name];
normalizedToParams[name] = (value != null) ? String(value) : null;
}
});
toParams = normalizedToParams;

Expand Down Expand Up @@ -286,7 +293,13 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
else {
$stateParams = {};
forEach(state.params, function (name) {
$stateParams[name] = params[name];
if(name === '*'){
for(var key in params){
$stateParams[key] = params[key];
}
} else {
$stateParams[name] = params[name];
}
});
}
var locals = { $stateParams: $stateParams };
Expand Down Expand Up @@ -345,7 +358,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
});
}

function equalForKeys(a, b, keys) {
function equalForKeys(a, b, keys, reloadWild) {
if(keys.indexOf('*') > 0 && a != b && reloadWild){
return false;
}
for (var i=0; i<keys.length; i++) {
var k = keys[i];
if (a[k] != b[k]) return false; // Not '===', values aren't necessarily normalized
Expand Down
22 changes: 18 additions & 4 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function UrlMatcher(pattern) {
params = this.params = [];

function addParameter(id) {
if (!/^\w+$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
if (!/^(\w+|\*)$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
if (names[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'");
names[id] = true;
params.push(id);
Expand Down Expand Up @@ -96,8 +96,14 @@ function UrlMatcher(pattern) {
segment = segment.substring(0, i);
this.sourcePath = pattern.substring(0, last+i);

// Allow parameters to be separated by '?' as well as '&' to make concat() easier
forEach(search.substring(1).split(/[&?]/), addParameter);
var searchString = search.substring(1);
if(searchString === '*'){
this.params.push('*');
} else {
// Allow parameters to be separated by '?' as well as '&' to make concat() easier
forEach(searchString.split(/[&?]/), addParameter);
}

} else {
this.sourcePath = pattern;
this.sourceSearch = '';
Expand Down Expand Up @@ -162,7 +168,15 @@ UrlMatcher.prototype.exec = function (path, searchParams) {
values = {}, i;

for (i=0; i<nPath; i++) values[params[i]] = decodeURIComponent(m[i+1]);
for (/**/; i<nTotal; i++) values[params[i]] = searchParams[params[i]];
for (/**/; i<nTotal; i++) {
if(params[i] === '*'){
for(var key in searchParams){
values[key] = searchParams[key];
}
} else {
values[params[i]] = searchParams[params[i]];
}
}

return values;
};
Expand Down
7 changes: 7 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ describe("UrlMatcher", function () {
.toEqual({ id:'123', type:'', repeat:'0' });
});

it(".exec() captures parameter values from wildcard", function () {
expect(
new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?*')
.exec('/users/123/details//0', {match: '', match2: 'wildcard'}))
.toEqual({ id:'123', type:'', repeat:'0', match: '', match2: 'wildcard'});
});

it(".exec() captures catch-all parameters", function () {
var m = new UrlMatcher('/document/*path');
expect(m.exec('/document/a/b/c', {})).toEqual({ path: 'a/b/c' });
Expand Down