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

removeQuery accepts RegExp that filters the keys #204

Merged
merged 1 commit into from
Apr 2, 2015
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
7 changes: 6 additions & 1 deletion docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,12 @@ <h3 id="search-remove">removeSearch(), removeQuery()</h3>
// remove multiple values with value filter
uri.search("?hello=world&amp;hello=mars&amp;foo=bar&amp;mine=true&amp;a=1&amp;a=2&amp;a=3");
uri.removeSearch({hello: "world", foo: undefined, a: ["1", "3"]});
// uri == "?hello=mars&amp;mine=true&amp;a=2"</pre>
// uri == "?hello=mars&amp;mine=true&amp;a=2"

// remove multiple values with regexp
uri.search("?hello=world&amp;hello=mars&amp;foo=bar&amp;mine=true&amp;a=1&amp;a=2&amp;a=3");
uri.removeSearch(/^hello/);
// uri == "?foo=bar&amp;mine=true&amp;a=1&amp;a=2&amp;a=3"</pre>

<h3 id="search-has">hasSearch(), hasQuery()</h3>
<p>.hasQuery() is an alias of .hasSearch(). The method checks the existence and value of a given parameter within the query string.</p>
Expand Down
8 changes: 7 additions & 1 deletion src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,12 @@
for (i = 0, length = name.length; i < length; i++) {
data[name[i]] = undefined;
}
} else if (getType(name) === 'RegExp') {
for (key in data) {
if (name.test(key)) {
data[key] = undefined;
}
}
} else if (typeof name === 'object') {
for (key in name) {
if (hasOwn.call(name, key)) {
Expand All @@ -750,7 +756,7 @@
data[name] = undefined;
}
} else {
throw new TypeError('URI.removeQuery() accepts an object, string as the first parameter');
throw new TypeError('URI.removeQuery() accepts an object, string, RegExp as the first parameter');
}
};
URI.hasQuery = function(data, name, value, withinArray) {
Expand Down
10 changes: 5 additions & 5 deletions src/URI.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,10 @@
u.query('?foo=bar&foo=baz&foo=bam&obj=bam&bar=1&bar=2&bar=3');
u.removeQuery({foo: 'bar', obj: undefined, bar: ['1', '2']});
equal(u.query(), 'foo=baz&foo=bam&bar=3', 'removing object');

u.query('?foo=bar&foo=baz&foo=bam&obj=bam&bar=1&bar=2&bar=3');
u.removeQuery(/^bar/);
equal(u.query(), 'foo=bar&foo=baz&foo=bam&obj=bam', 'removing by RegExp');
});
test('duplicateQueryParameters', function() {
var u = new URI('?bar=1&bar=1&bar=1');
Expand Down