diff --git a/lib/parse.js b/lib/parse.js index 04493e1d..53c30dbc 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -83,7 +83,7 @@ var parseValues = function parseQueryStringValues(str, options) { val = interpretNumericEntities(val); } - if (val && options.comma && val.indexOf(',') > -1) { + if (val && typeof val === 'string' && options.comma && val.indexOf(',') > -1) { val = val.split(','); } diff --git a/test/parse.js b/test/parse.js index 9ea38dd3..cda69284 100644 --- a/test/parse.js +++ b/test/parse.js @@ -400,6 +400,35 @@ test('parse()', function (t) { st.end(); }); + t.test('use numbers decoder and parses string with comma as array divider', + function (st) { + var decoder = function (str, defaultDecoder, charset, type) { + if (!isNaN(str)) { + return parseFloat(str); + } + + if (str && typeof str === 'string' && str.indexOf(',') > -1) { + return str.split(',').map(function (item) { + if (!isNaN(item)) { + return parseFloat(item); + } + return defaultDecoder(item, defaultDecoder, charset, type); + }); + } + + return defaultDecoder(str, defaultDecoder, charset, type); + }; + st.deepEqual(qs.parse('foo[bar]=1.2,1.5', { comma: true, decoder: decoder }), { + foo: { bar: [1.2, 1.5] } + }); + st.deepEqual(qs.parse('foo=1,2', { comma: true, decoder: decoder }), { foo: [1, 2] }); + st.deepEqual(qs.parse('foo=1.2,2.3', { comma: true, decoder: decoder }), { foo: [1.2, 2.3] }); + st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true, decoder: decoder }), { foo: null }); + + st.end(); + } + ); + t.test('parses an object in dot notation', function (st) { var input = { 'user.name': { 'pop[bob]': 3 },