Skip to content

Commit

Permalink
feature(query): extending .removeQuery() to accept RegExp to filter v…
Browse files Browse the repository at this point in the history
…alues, following #204
  • Loading branch information
rodneyrehm committed Apr 2, 2015
1 parent f9ca9b0 commit c7f0d49
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
9 changes: 7 additions & 2 deletions docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,15 @@ <h3 id="search-remove">removeSearch(), removeQuery()</h3>
uri.removeSearch({hello: "world", foo: undefined, a: ["1", "3"]});
// uri == "?hello=mars&amp;mine=true&amp;a=2"

// remove multiple values with regexp
// 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>
// uri == "?foo=bar&amp;mine=true&amp;a=1&amp;a=2&amp;a=3"

// filter values with RegExp
uri.search("?foo=bar&amp;foo=baz&amp;foo=bam&amp;obj=bam&amp;bar=bar&amp;bar=baz&amp;bar=bam");
uri.removeSearch('foo', /[rz]$/);
// uri == "?foo=bam&amp;obj=bam&amp;bar=bar&amp;bar=baz&amp;bar=bam"</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
18 changes: 15 additions & 3 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@
var lookup = {};
var i, length;

if (isArray(value)) {
if (getType(value) === 'RegExp') {
lookup = null;
} else if (isArray(value)) {
for (i = 0, length = value.length; i < length; i++) {
lookup[value[i]] = true;
}
Expand All @@ -108,7 +110,11 @@
}

for (i = 0, length = data.length; i < length; i++) {
if (lookup[data[i]] !== undefined) {
/*jshint laxbreak: true */
var _match = lookup && lookup[data[i]] !== undefined
|| !lookup && value.test(data[i]);
/*jshint laxbreak: false */
if (_match) {
data.splice(i, 1);
length--;
i--;
Expand Down Expand Up @@ -758,7 +764,13 @@
}
} else if (typeof name === 'string') {
if (value !== undefined) {
if (data[name] === value) {
if (getType(value) === 'RegExp') {
if (!isArray(data[name]) && value.test(data[name])) {
data[name] = undefined;
} else {
data[name] = filterArrayValues(data[name], value);
}
} else if (data[name] === value) {
data[name] = undefined;
} else if (isArray(data[name])) {
data[name] = filterArrayValues(data[name], value);
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,10 @@
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');

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

0 comments on commit c7f0d49

Please sign in to comment.