Skip to content

Commit

Permalink
[WIP] Add caseSensitive option for query param keys
Browse files Browse the repository at this point in the history
  • Loading branch information
faridnsh committed Feb 7, 2017
1 parent 8d85d31 commit 0112c76
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
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:password@example.com:8080/username@email.com/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')
// })
})

0 comments on commit 0112c76

Please # to comment.