Skip to content

Commit

Permalink
Support RegExp for hasQuery parameter name
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrodala committed Jan 8, 2016
1 parent 0b747a0 commit 879a42a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
36 changes: 27 additions & 9 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,18 +795,36 @@
}
};
URI.hasQuery = function(data, name, value, withinArray) {
if (typeof name === 'object') {
for (var key in name) {
if (hasOwn.call(name, key)) {
if (!URI.hasQuery(data, key, name[key])) {
return false;
switch (getType(name)) {
case 'String':
// Nothing to do here
break;

case 'RegExp':
for (var key in data) {
if (hasOwn.call(data, key)) {
if (name.test(key) && (value === undefined || URI.hasQuery(data, key, value))) {
return true;
}
}
}
}

return true;
} else if (typeof name !== 'string') {
throw new TypeError('URI.hasQuery() accepts an object, string as the name parameter');
return false;
break;

case 'Object':
for (var key in name) {
if (hasOwn.call(name, key)) {
if (!URI.hasQuery(data, key, name[key])) {
return false;
}
}
}

return true;

default:
throw new TypeError('URI.hasQuery() accepts a string, regular expression or object as the name parameter');
}

switch (getType(value)) {
Expand Down
5 changes: 4 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,11 +985,14 @@
URI.escapeQuerySpace = true;
});
test('hasQuery', function() {
var u = URI('?string=bar&list=one&list=two&number=123&null&empty=');
var u = URI('?string=bar&list=one&list=two&number=123&null&empty=&nested[one]=1&nested[two]=2');

// exists
equal(u.hasQuery('string'), true, 'simple exists check - passing');
equal(u.hasQuery('nono'), false, 'simple exists check - failing');
equal(u.hasQuery(/^nested/), true, 'RegExp name exists check - passing');
equal(u.hasQuery(/^nested/, /2/), true, 'RegExp name and value exists check - passing');
equal(u.hasQuery(/^nested/, /3/), false, 'RegExp name and value exists check - failing');

// truthy value
equal(u.hasQuery('string', true), true, 'has truthy value check - passing string');
Expand Down

0 comments on commit 879a42a

Please sign in to comment.