Skip to content

Commit 6afb20c

Browse files
author
isaacs
committed
Merge branch 'master' of git://github.com/jazzychad/querystring.node.js
Conflicts: querystring-parse.js querystring-stringify.js util.js
2 parents 1bc769e + dfaf313 commit 6afb20c

File tree

4 files changed

+79
-11
lines changed

4 files changed

+79
-11
lines changed

querystring-parse.js

+63
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ exports.parse = querystring_parse;
4242
* @for querystring
4343
* @static
4444
*/
45+
<<<<<<< HEAD
4546
function querystring_parse (qs, sep, eq, unesc) {
4647
return qs.split(sep || "&")
4748
.map(pieceParser(eq || "=", unesc || unescape))
@@ -50,6 +51,22 @@ function querystring_parse (qs, sep, eq, unesc) {
5051

5152
function unescape (s) {
5253
return decodeURIComponent(s.replace(/\+/g, ' '));
54+
=======
55+
var parse = function (qs, sep, eq) {
56+
// wouldn't Array(qs.split()).map(pieceParser(eq)).reduce(mergeParams) be prettier?
57+
return util.reduce(
58+
util.map(
59+
qs.split(sep || "&"),
60+
pieceParser(eq || "=")
61+
),
62+
{},
63+
mergeParams
64+
);
65+
};
66+
67+
var unescape = function (s) {
68+
return decodeURIComponent(s.replace(/\+/g, ' '));
69+
>>>>>>> dfaf3130e7ad0c25f7464dc46bb862c9f0bfb9ee
5370
};
5471

5572

@@ -62,24 +79,40 @@ function unescape (s) {
6279
// return parse(foo[bar], [{bla:"baz"}])
6380
// return parse(foo, {bar:[{bla:"baz"}]})
6481
// return {foo:{bar:[{bla:"baz"}]}}
82+
<<<<<<< HEAD
6583
function pieceParser (eq, unesc) {
6684
return function parsePiece (key, val) {
6785

86+
=======
87+
var pieceParser = function (eq) {
88+
return function parsePiece (key, val) {
89+
>>>>>>> dfaf3130e7ad0c25f7464dc46bb862c9f0bfb9ee
6890
if (arguments.length !== 2) {
6991
// key=val, called from the map/reduce
7092
key = key.split(eq);
7193
return parsePiece(
94+
<<<<<<< HEAD
7295
unesc(key.shift()),
7396
unesc(key.join(eq))
7497
);
98+
=======
99+
unescape(key.shift()),
100+
unescape(key.join(eq))
101+
);
102+
>>>>>>> dfaf3130e7ad0c25f7464dc46bb862c9f0bfb9ee
75103
}
76104
key = key.replace(/^\s+|\s+$/g, '');
77105
if (util.isString(val)) {
78106
val = val.replace(/^\s+|\s+$/g, '');
79107
// convert numerals to numbers
80108
if (!isNaN(val)) {
109+
<<<<<<< HEAD
81110
var numVal = +val;
82111
if (val === numVal.toString(10)) val = numVal;
112+
=======
113+
var numVal = +val;
114+
if (val === numVal.toString(10)) val = numVal;
115+
>>>>>>> dfaf3130e7ad0c25f7464dc46bb862c9f0bfb9ee
83116
}
84117
}
85118
var sliced = /(.*)\[([^\]]*)\]$/.exec(key);
@@ -89,8 +122,12 @@ function pieceParser (eq, unesc) {
89122
return ret;
90123
}
91124
// ["foo[][bar][][baz]", "foo[][bar][]", "baz"]
125+
<<<<<<< HEAD
92126
var tail = sliced[2],
93127
head = sliced[1];
128+
=======
129+
var tail = sliced[2], head = sliced[1];
130+
>>>>>>> dfaf3130e7ad0c25f7464dc46bb862c9f0bfb9ee
94131

95132
// array: key[]=val
96133
if (!tail) return parsePiece(head, [val]);
@@ -104,6 +141,7 @@ function pieceParser (eq, unesc) {
104141

105142
// the reducer function that merges each query piece together into one set of params
106143
function mergeParams (params, addition) {
144+
<<<<<<< HEAD
107145
var ret;
108146

109147
if (!params){
@@ -120,14 +158,39 @@ function mergeParams (params, addition) {
120158
ret = mergeObjects(params, addition);
121159
}
122160
return ret;
161+
=======
162+
var ret;
163+
164+
if (!params){
165+
// if it's uncontested, then just return the addition.
166+
ret = addition;
167+
} else if (util.isArray(params)) {
168+
// if the existing value is an array, then concat it.
169+
ret = params.concat(addition);
170+
} else if (!util.isObject(params) || !util.isObject(addition)) {
171+
// if the existing value is not an array, and either are not objects, arrayify it.
172+
ret = [params].concat(addition);
173+
} else {
174+
// else merge them as objects, which is a little more complex
175+
ret = mergeObjects(params, addition);
176+
}
177+
return ret;
178+
>>>>>>> dfaf3130e7ad0c25f7464dc46bb862c9f0bfb9ee
123179
};
124180

125181

126182
// Merge two *objects* together. If this is called, we've already ruled
127183
// out the simple cases, and need to do the for-in business.
128184
function mergeObjects (params, addition) {
185+
<<<<<<< HEAD
129186
for (var i in addition) if (i && addition.hasOwnProperty(i)) {
130187
params[i] = mergeParams(params[i], addition[i]);
188+
=======
189+
for (var i in addition) {
190+
if (i && addition.hasOwnProperty(i)) {
191+
params[i] = mergeParams(params[i], addition[i]);
192+
}
193+
>>>>>>> dfaf3130e7ad0c25f7464dc46bb862c9f0bfb9ee
131194
}
132195
return params;
133196
};

querystring-stringify.js

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ function querystring_stringify (obj, sep, eq, name, escape) {
4242
if (util.isNumber(obj) || util.isString(obj)) {
4343
return escape(name) + eq + escape(obj);
4444
}
45-
4645
if (util.isArray(obj)) {
4746
var s = [];
4847
name = name+'[]';

test.js

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ sys.puts("qs: " + qs.stringify(foo));
2828

2929
sys.puts('------------------');
3030

31+
/* test 2 */
32+
foo = qs.parse("foo=bar&foo=");
33+
34+
sys.puts("obj: " + JSON.stringify(foo));
35+
sys.puts("qs: " + qs.stringify(foo));
36+
37+
sys.puts('------------------');
38+
3139
/* test 3 */
3240
foo = qs.parse("foo[1]=1&foo[2]=2&foo[3]=4");
3341

util.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,34 @@ exports.isArray = isArray;
1818
exports.isObject = isObject;
1919

2020

21-
var toString = Object.prototype.toString;
22-
2321
function is (type, obj) {
24-
return toString.call(obj) === '[object '+type+']';
22+
return Object.prototype.toString.call(obj) === '[object '+type+']';
2523
}
2624

2725
function isArray (obj) {
28-
return is("Array", obj);
26+
return is("Array", obj);
2927
}
3028

3129
function isObject (obj) {
32-
return is("Object", obj);
30+
return is("Object", obj);
3331
}
3432

3533
function isString (obj) {
36-
return is("String", obj);
34+
return is("String", obj);
3735
}
3836

3937
function isNumber (obj) {
40-
return is("Number", obj);
38+
return is("Number", obj);
4139
}
4240

4341
function isBoolean (obj) {
44-
return is("Boolean", obj);
42+
return is("Boolean", obj);
4543
}
4644

4745
function isNull (obj) {
48-
return typeof obj === "object" && !obj;
46+
return typeof obj === "object" && !obj;
4947
}
5048

5149
function isUndefined (obj) {
52-
return typeof obj === "undefined";
50+
return typeof obj === "undefined";
5351
}

0 commit comments

Comments
 (0)