Skip to content

Commit a10d0cd

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents c38dc52 + 05c05d0 commit a10d0cd

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,40 @@ QueryString.prototype.parse = function (queryStr) {
6363

6464
queryStr.split('&').forEach((param) => {
6565
const components = param.split('=');
66+
const value = decodeURIComponent(components[1]);
67+
var key = decodeURIComponent(components[0]);
6668

67-
obj[decodeURIComponent(components[0])] = decodeURIComponent(components[1]);
69+
//Is the query param an array?
70+
if (key.search(/\[([0-9]*)\]/) !== -1) {
71+
const indexOfArray = key.slice(-2) !== '[]' ? key.charAt(key.length - 2) : undefined;
72+
key = key.slice(0, key.indexOf('['));
73+
74+
//Does the array already exist in the object
75+
if (obj[key]) {
76+
if (indexOfArray) {
77+
obj[key][indexOfArray] = value;
78+
} else {
79+
obj[key].push(value);
80+
}
81+
} else {
82+
if (indexOfArray) {
83+
obj[key] = [];
84+
obj[key][indexOfArray] = value;
85+
} else {
86+
obj[key] = [value];
87+
}
88+
}
89+
} else {
90+
obj[key] = value;
91+
}
6892
});
6993

7094
return obj;
7195
}
7296

97+
QueryString.prototype.extract = function (url) {
98+
return url.substring(url.indexOf('?') + 1);
99+
}
100+
73101
// Export the module
74102
module.exports = new QueryString();

test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,40 @@ describe('query-stringifier', function () {
4545
expect(parsed).to.have.property('foo', 'bar');
4646
expect(parsed).to.have.property('thing', 'thung');
4747
});
48+
it('converts an query string with arrays', function () {
49+
var parsed = qs.parse('&arr[]=1&arr[]=2&arr[]=3');
50+
51+
expect(parsed).to.be.an('object');
52+
expect(parsed).to.have.keys(['arr']);
53+
expect(parsed).to.deep.equal({ arr: ['1', '2', '3'] });
54+
});
55+
it('converts an query string with arrays and indexes', function () {
56+
var parsed = qs.parse('&arr[2]=1&arr[0]=2&arr[1]=3');
57+
58+
expect(parsed).to.be.an('object');
59+
expect(parsed).to.have.keys(['arr']);
60+
expect(parsed).to.deep.equal({ arr: ['2', '3', '1'] });
61+
});
4862
});
63+
64+
describe('#extract', function () {
65+
it('exract the query string of the url', function() {
66+
var url = 'www.dummyurl.com?firstqueryparam=first&secondqueryparam=second';
67+
var result = qs.extract(url);
68+
69+
expect(result).to.equal('firstqueryparam=first&secondqueryparam=second');
70+
});
71+
});
72+
73+
describe('#extract/#parse', function() {
74+
it('extract the query string of the url and parses it', function () {
75+
var url = 'www.dummyurl.com?firstqueryparam=first&secondqueryparam=second';
76+
var result = qs.parse(qs.extract(url));
77+
78+
expect(result).to.be.an('object');
79+
expect(result).to.have.property('firstqueryparam', 'first');
80+
expect(result).to.have.property('secondqueryparam', 'second');
81+
});
82+
});
83+
4984
});

0 commit comments

Comments
 (0)