Skip to content

Commit

Permalink
removeQuery accepts RegExp that filters the keys
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwillis committed Apr 1, 2015
1 parent 141c04d commit 765fb20
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
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

0 comments on commit 765fb20

Please sign in to comment.