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

Support objects with numeric keys. #62

Open
wants to merge 2 commits 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
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function createObject() {
*/

var isint = /^[0-9]+$/;
var isnumstr = /^['"][0-9]+['"]$/;

function promote(parent, key) {
if (parent[key].length == 0) return parent[key] = createObject();
Expand All @@ -92,6 +93,9 @@ function promote(parent, key) {

function parse(parts, parent, key, val) {
var part = parts.shift();

key = isnumstr.test(key) ? key.replace(/['"]/g, '') : key;

// end
if (!part) {
if (isArray(parent[key])) {
Expand Down Expand Up @@ -311,15 +315,18 @@ function stringifyArray(arr, prefix) {
function stringifyObject(obj, prefix) {
var ret = []
, keys = objectKeys(obj)
, key;
, key, val;

for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
val = obj[key];

if ('' == key) continue;
if (null == obj[key]) {
if (null == val) {
ret.push(encodeURIComponent(key) + '=');
} else {
ret.push(stringify(obj[key], prefix
key = isint.test(key) ? "'" + key + "'" : key;
ret.push(stringify(val, prefix
? prefix + '[' + encodeURIComponent(key) + ']'
: encodeURIComponent(key)));
}
Expand All @@ -340,6 +347,7 @@ function stringifyObject(obj, prefix) {
*/

function set(obj, key, val) {
key = isnumstr.test(key) ? key.replace(/['"]/g, '') : key;
var v = obj[key];
if (undefined === v) {
obj[key] = val;
Expand Down
7 changes: 7 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ describe('qs.parse()', function(){
expect(q).to.eql({ a: ['2', '1'] });
})

it('should support objects with numbers as keys', function(){
expect(qs.parse("a['111']=1")).to.eql({ a: { '111': '1' }});
expect(qs.parse("'123'=hello")).to.eql({ '123': 'hello' });
expect(qs.parse("a['111'][b]=222")).to.eql({ a: { '111': { b: '222' }}});
expect(qs.parse('a["111"][b]=222')).to.eql({ a: { '111': { b: '222' }}});
})

if ('undefined' == typeof window) {
it('should not be possible to access Object prototype', function() {
qs.parse('constructor[prototype][bad]=bad');
Expand Down
4 changes: 3 additions & 1 deletion test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ var str_identities = {
],
'numbers': [
{ str: 'limit[0]=1&limit[1]=2&limit[2]=3', obj: { limit: [1, 2, '3'] }},
{ str: 'limit=1', obj: { limit: 1 }}
{ str: 'limit=1', obj: { limit: 1 }},
{ str: "a['111']=1", obj: { a: { '111': 1 } } },
{ str: "'123'=hello", obj: { '123': "hello" } }
],
'others': [
{ str: 'at=' + encodeURIComponent(date), obj: { at: date } }
Expand Down