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

Support RegExp for hasQuery parameter name #277

Merged
merged 1 commit into from
Jan 8, 2016
Merged
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
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