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

[WIP] Add caseSensitive option for query param keys matching #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions Uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
this.uriParts = parseUri(str);
this.queryPairs = parseQuery(this.uriParts.query);
this.hasAuthorityPrefixUserPref = null;
this.isCaseSensitive = true;
}

/**
Expand Down Expand Up @@ -163,6 +164,16 @@
}
};

/**
* Sets case sensitivity for matching query params keys.
* @param {Boolean} val
* @return {Uri}
*/
Uri.prototype.caseSensitive = function(val) {
this.isCaseSensitive = val;
return this;
};

Uri.prototype.isColonUri = function (val) {
if (typeof val !== 'undefined') {
this.uriParts.isColonUri = !!val;
Expand Down Expand Up @@ -210,7 +221,11 @@
var param, i, l;
for (i = 0, l = this.queryPairs.length; i < l; i++) {
param = this.queryPairs[i];
if (key === param[0]) {
if (this.isCaseSensitive) {
if (key === param[0]) {
return param[1];
}
} else if (key.toLowerCase() === param[0].toLowerCase()) {
return param[1];
}
}
Expand All @@ -225,8 +240,12 @@
var arr = [], i, param, l;
for (i = 0, l = this.queryPairs.length; i < l; i++) {
param = this.queryPairs[i];
if (key === param[0]) {
arr.push(param[1]);
if (this.isCaseSensitive) {
if (key === param[0]) {
arr.push(param[1]);
}
} else if (key.toLowerCase() === param[0].toLowerCase()) {
arr.push(param[1]);
}
}
return arr;
Expand Down
13 changes: 13 additions & 0 deletions test/uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,17 @@ describe('Uri', function() {
u = new Uri('http://username:[email protected]:8080/[email protected]/page')
assert.equal(u.port(), '8080')
})

it('Should be able to match query param keys as case insensitive', function () {
u = new Uri('http://example.com/search?qI=a&qi=a').caseSensitive(false)
assert.equal(u.getQueryParamValue('qi'), 'a')
assert.equal(u.getQueryParamValue('QI'), 'a')
assert.deepEqual(u.getQueryParamValues('QI'), ['a', 'a'])
})

// it('should be able delete query params when case insensitive', function() {
// u = new Uri('http://example.com/search?q=a&stupid=yes').caseSensitive(false)
// u.deleteQueryParam('stupId')
// assert.equal(u.toString(), 'http://example.com/search?q=a')
// })
})