Skip to content

Commit 17ed3f9

Browse files
authored
Merge pull request #19 from catorda/master
Add support for more than one value per key
2 parents 41b2b25 + 509367d commit 17ed3f9

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

docs/QueryString.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ <h4 class="name" id="extract">
176176

177177
<div class="container-source members">
178178
<code><a href="index.js.html">index.js</a></code>,
179-
<code><a href="index.js.html#line102">line 102</a></code>
179+
<code><a href="index.js.html#line116">line 116</a></code>
180180
</div>
181181

182182
</h4>

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ <h1><a href="index.html" class="link">QueryString</a></h1>
5555

5656

5757
<section>
58-
<article class="readme"><h1>Query-Stringifier <a href="https://travis-ci.org/joshghent/query-stringifier"><img src="https://travis-ci.org/joshghent/query-stringifier.svg?branch=master" alt="Build Status"></a> <a href="https://badge.fury.io/js/query-stringifier"><img src="https://badge.fury.io/js/query-stringifier.svg" alt="npm version"></a> <img src="https://david-dm.org/joshghent/query-stringifier.svg" alt="devDependencies"></h1><p>A small library for build query strings</p>
58+
<article class="readme"><h1>Query-Stringifier <a href="https://travis-ci.org/joshghent/query-stringifier"><img src="https://travis-ci.org/joshghent/query-stringifier.svg?branch=master" alt="Build Status"></a> <a href="https://badge.fury.io/js/query-stringifier"><img src="https://badge.fury.io/js/query-stringifier.svg" alt="npm version"></a> <a href="https://greenkeeper.io/"><img src="https://badges.greenkeeper.io/joshghent/query-stringifier.svg" alt="Greenkeeper badge"></a> <img src="https://david-dm.org/joshghent/query-stringifier.svg" alt="devDependencies"></h1><p>A small library for build query strings</p>
5959
<h2>Install</h2><pre class="prettyprint source lang-bash"><code>$ npm install query-stringifier</code></pre><h2>Usage</h2><pre class="prettyprint source lang-js"><code>
6060
const qs = require('query-stringifier');
6161

docs/index.js.html

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,24 @@ <h1><a href="index.html" class="link">QueryString</a></h1>
132132
obj[key][indexOfArray] = value;
133133
} else {
134134
obj[key] = [value];
135-
}
135+
}
136136
}
137137
} else {
138-
obj[key] = value;
138+
// Check if the key is already in the object
139+
if (Object.keys(obj).indexOf(key) > -1) {
140+
if (Array.isArray(obj[key])) {
141+
obj[key].push(value);
142+
} else {
143+
let newArray = [];
144+
newArray.push(obj[key]);
145+
newArray.push(value);
146+
obj[key] = newArray;
147+
}
148+
149+
} else {
150+
obj[key] = value;
151+
}
152+
139153
}
140154
});
141155

index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,24 @@ QueryString.prototype.parse = function (queryStr) {
8484
obj[key][indexOfArray] = value;
8585
} else {
8686
obj[key] = [value];
87-
}
87+
}
8888
}
8989
} else {
90-
obj[key] = value;
90+
// Check if the key is already in the object
91+
if (Object.keys(obj).indexOf(key) > -1) {
92+
if (Array.isArray(obj[key])) {
93+
obj[key].push(value);
94+
} else {
95+
let newArray = [];
96+
newArray.push(obj[key]);
97+
newArray.push(value);
98+
obj[key] = newArray;
99+
}
100+
101+
} else {
102+
obj[key] = value;
103+
}
104+
91105
}
92106
});
93107

test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ describe('query-stringifier', function () {
5959
expect(parsed).to.have.keys(['arr']);
6060
expect(parsed).to.deep.equal({ arr: ['2', '3', '1'] });
6161
});
62+
it('converts query string with duplicate keys', function() {
63+
var parsed = qs.parse('itemIds=1&itemIds=2');
64+
expect(parsed).to.be.an('object');
65+
expect(parsed).to.have.keys(['itemIds']);
66+
expect(parsed).to.deep.equal({ itemIds: ['1', '2'] })
67+
})
6268
});
6369

6470
describe('#extract', function () {

0 commit comments

Comments
 (0)