Skip to content

Commit 11a5bd7

Browse files
MadManMathewljharb
authored andcommitted
Attempt to fix #122
1 parent 126dcb8 commit 11a5bd7

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

lib/utils.js

+21-35
Original file line numberDiff line numberDiff line change
@@ -30,51 +30,37 @@ exports.merge = function (target, source, options) {
3030
if (typeof source !== 'object') {
3131
if (Array.isArray(target)) {
3232
target.push(source);
33+
return target;
3334
} else if (typeof target === 'object') {
34-
if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
35-
target[source] = true;
36-
}
35+
source = exports.arrayToObject([source], options);
3736
} else {
38-
return [target, source];
37+
target = [target, source];
38+
return target;
3939
}
40-
41-
return target;
42-
}
43-
44-
if (typeof target !== 'object') {
45-
return [target].concat(source);
46-
}
47-
48-
var mergeTarget = target;
49-
if (Array.isArray(target) && !Array.isArray(source)) {
50-
mergeTarget = exports.arrayToObject(target, options);
51-
}
52-
53-
if (Array.isArray(target) && Array.isArray(source)) {
54-
source.forEach(function (item, i) {
55-
if (has.call(target, i)) {
56-
if (target[i] && typeof target[i] === 'object') {
57-
target[i] = exports.merge(target[i], item, options);
58-
} else {
59-
target.push(item);
60-
}
61-
} else {
62-
target[i] = item;
63-
}
64-
});
40+
} else if (Array.isArray(target) &&
41+
!Array.isArray(source)) {
42+
target = exports.arrayToObject(target, options);
43+
} else if (Array.isArray(source) && typeof target !== 'object') {
44+
source.splice(0,0,target);
45+
target = source;
6546
return target;
47+
} else if (typeof target !== 'object') {
48+
target = exports.arrayToObject([target], options);
6649
}
6750

68-
return Object.keys(source).reduce(function (acc, key) {
51+
var keys = Object.keys(source);
52+
for (var i = 0; i < keys.length; ++i) {
53+
var key = keys[i];
6954
var value = source[key];
7055

71-
if (has.call(acc, key)) {
72-
acc[key] = exports.merge(acc[key], value, options);
56+
if (Object.prototype.hasOwnProperty.call(target, key)) {
57+
target[key] = exports.merge(target[key], value, options);
7358
} else {
74-
acc[key] = value;
59+
target[key] = value;
7560
}
76-
return acc;
77-
}, mergeTarget);
61+
}
62+
63+
return target;
7864
};
7965

8066
exports.assign = function assignSingleSource(target, source) {

test/parse.js

+20
Original file line numberDiff line numberDiff line change
@@ -535,5 +535,25 @@ test('parse()', function (t) {
535535
st.end();
536536
});
537537

538+
t.test('parses string/object combination', function (st) {
539+
540+
var expectObj = { a: { 0: 'val1', b: 'val2' } };
541+
st.deepEqual(qs.parse('a=val1&a.b=val2', { allowDots: true }),expectObj);
542+
st.deepEqual(qs.parse('a.b=val2&a=val1', { allowDots: true }),expectObj);
543+
expectObj = { a: { 0: 'val1', b: 'val2', c: 'val3' } };
544+
st.deepEqual(qs.parse('a.b=val2&a=val1&a.c=val3', { allowDots: true }),expectObj);
545+
expectObj = { a: { 0: 'val1', 1: 'val3', b: 'val2' } };
546+
st.deepEqual(qs.parse('a.b=val2&a=val1&a=val3', { allowDots: true }),expectObj);
547+
st.deepEqual(qs.parse('a=val1&a.b=val2&a=val3', { allowDots: true }),expectObj);
548+
expectObj = { a: { 0: 'val1', 1: 'val2', 2 :'val3', b: 'val4' } };
549+
st.deepEqual(qs.parse('a=val1&a=val2&a=val3&a.b=val4', { allowDots: true }),expectObj);
550+
expectObj = { a: { 0: 'val1', b: ['val2', 'val3'] } };
551+
st.deepEqual(qs.parse('a=val1&a.b=val2&a.b=val3', { allowDots: true }),expectObj);
552+
expectObj = { a: { 0: 'val1', b: { 0: 'val2', c: 'val3' } } };
553+
st.deepEqual(qs.parse('a=val1&a.b=val2&a.b.c=val3', { allowDots: true }),expectObj);
554+
st.deepEqual(qs.parse('a.b.c=val3&a.b=val2&a=val1', { allowDots: true }),expectObj);
555+
st.end();
556+
});
557+
538558
t.end();
539559
});

0 commit comments

Comments
 (0)