Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

qs

Package Overview
Dependencies
Maintainers
1
Versions
113
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

qs - npm Package Compare versions

Comparing version 0.4.2 to 0.5.0

.npmignore

5

History.md
0.5.0 / 2012-05-04
==================
* Added component support
0.4.2 / 2012-02-08

@@ -3,0 +8,0 @@ ==================

14

lib/querystring.js
/*!
* querystring
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Library version.
*/
exports.version = '0.4.2';
/**
* Object#toString() ref for stringify().

@@ -195,3 +183,3 @@ */

for (var i = 0; i < arr.length; i++) {
ret.push(stringify(arr[i], prefix + '[]'));
ret.push(stringify(arr[i], prefix + '['+i+']'));
}

@@ -198,0 +186,0 @@ return ret.join('&');

{
"name": "qs",
"description": "querystring parser",
"version": "0.4.2",
"version": "0.5.0",
"keywords": ["query string", "parser", "component"],
"repository": {

@@ -11,4 +12,9 @@ "type" : "git",

"mocha": "*"
, "should": "*"
, "expect.js": "*"
},
"component": {
"scripts": {
"querystring": "querystring.js"
}
},
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",

@@ -15,0 +21,0 @@ "main": "index",

# node-querystring
query string parser for node supporting nesting, as it was removed from `0.3.x`, so this library provides the previous and commonly desired behaviour (and twice as fast). Used by [express](http://expressjs.com), [connect](http://senchalabs.github.com/connect) and others.
query string parser for node and the browser supporting nesting, as it was removed from `0.3.x`, so this library provides the previous and commonly desired behaviour (and twice as fast). Used by [express](http://expressjs.com), [connect](http://senchalabs.github.com/connect) and others.

@@ -31,2 +31,6 @@ ## Installation

browser:
$ open test/browser/index.html
## License

@@ -33,0 +37,0 @@

/**
* Module dependencies.
*/
if (require.register) {
var qs = require('querystring');
} else {
var qs = require('../')
, expect = require('expect.js');
}
var qs = require('../');
describe('qs.parse()', function(){
it('should support the basics', function(){
expect(qs.parse('0=foo')).to.eql({ '0': 'foo' });
module.exports = {
'test basics': function(){
qs.parse('0=foo').should.eql({ '0': 'foo' });
expect(qs.parse('foo=c++'))
.to.eql({ foo: 'c ' });
qs.parse('foo=c++')
.should.eql({ foo: 'c ' });
expect(qs.parse('a[>=]=23'))
.to.eql({ a: { '>=': '23' }});
qs.parse('a[>=]=23')
.should.eql({ a: { '>=': '23' }});
expect(qs.parse('a[<=>]==23'))
.to.eql({ a: { '<=>': '=23' }});
qs.parse('a[<=>]==23')
.should.eql({ a: { '<=>': '=23' }});
expect(qs.parse('a[==]=23'))
.to.eql({ a: { '==': '23' }});
qs.parse('a[==]=23')
.should.eql({ a: { '==': '23' }});
expect(qs.parse('foo'))
.to.eql({ foo: '' });
qs.parse('foo')
.should.eql({ foo: '' });
expect(qs.parse('foo=bar'))
.to.eql({ foo: 'bar' });
qs.parse('foo=bar')
.should.eql({ foo: 'bar' });
expect(qs.parse('foo%3Dbar=baz'))
.to.eql({ foo: 'bar=baz' });
qs.parse('foo%3Dbar=baz')
.should.eql({ foo: 'bar=baz' });
expect(qs.parse(' foo = bar = baz '))
.to.eql({ ' foo ': ' bar = baz ' });
qs.parse(' foo = bar = baz ')
.should.eql({ ' foo ': ' bar = baz ' });
expect(qs.parse('foo=bar=baz'))
.to.eql({ foo: 'bar=baz' });
qs.parse('foo=bar=baz')
.should.eql({ foo: 'bar=baz' });
expect(qs.parse('foo=bar&bar=baz'))
.to.eql({ foo: 'bar', bar: 'baz' });
qs.parse('foo=bar&bar=baz')
.should.eql({ foo: 'bar', bar: 'baz' });
expect(qs.parse('foo=bar&baz'))
.to.eql({ foo: 'bar', baz: '' });
qs.parse('foo=bar&baz')
.should.eql({ foo: 'bar', baz: '' });
qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World')
.should.eql({
expect(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'))
.to.eql({
cht: 'p3'

@@ -52,117 +53,94 @@ , chd: 't:60,40'

});
},
'test nesting': function(){
qs.parse('ops[>=]=25')
.should.eql({ ops: { '>=': '25' }});
})
qs.parse('user[name]=tj')
.should.eql({ user: { name: 'tj' }});
it('should support nesting', function(){
expect(qs.parse('ops[>=]=25'))
.to.eql({ ops: { '>=': '25' }});
qs.parse('user[name][first]=tj&user[name][last]=holowaychuk')
.should.eql({ user: { name: { first: 'tj', last: 'holowaychuk' }}});
},
'test escaping': function(){
qs.parse('foo=foo%20bar')
.should.eql({ foo: 'foo bar' });
},
'test arrays': function(){
qs.parse('images[]')
.should.eql({ images: [] });
expect(qs.parse('user[name]=tj'))
.to.eql({ user: { name: 'tj' }});
qs.parse('user[]=tj')
.should.eql({ user: ['tj'] });
expect(qs.parse('user[name][first]=tj&user[name][last]=holowaychuk'))
.to.eql({ user: { name: { first: 'tj', last: 'holowaychuk' }}});
})
qs.parse('user[]=tj&user[]=tobi&user[]=jane')
.should.eql({ user: ['tj', 'tobi', 'jane'] });
it('should support array notation', function(){
expect(qs.parse('images[]'))
.to.eql({ images: [] });
qs.parse('user[names][]=tj&user[names][]=tyler')
.should.eql({ user: { names: ['tj', 'tyler'] }});
expect(qs.parse('user[]=tj'))
.to.eql({ user: ['tj'] });
qs.parse('user[names][]=tj&user[names][]=tyler&user[email]=tj@vision-media.ca')
.should.eql({ user: { names: ['tj', 'tyler'], email: 'tj@vision-media.ca' }});
expect(qs.parse('user[]=tj&user[]=tobi&user[]=jane'))
.to.eql({ user: ['tj', 'tobi', 'jane'] });
qs.parse('items=a&items=b')
.should.eql({ items: ['a', 'b'] });
expect(qs.parse('user[names][]=tj&user[names][]=tyler'))
.to.eql({ user: { names: ['tj', 'tyler'] }});
qs.parse('user[names]=tj&user[names]=holowaychuk&user[names]=TJ')
.should.eql({ user: { names: ['tj', 'holowaychuk', 'TJ'] }});
expect(qs.parse('user[names][]=tj&user[names][]=tyler&user[email]=tj@vision-media.ca'))
.to.eql({ user: { names: ['tj', 'tyler'], email: 'tj@vision-media.ca' }});
qs.parse('user[name][first]=tj&user[name][first]=TJ')
.should.eql({ user: { name: { first: ['tj', 'TJ'] }}});
expect(qs.parse('items=a&items=b'))
.to.eql({ items: ['a', 'b'] });
expect(qs.parse('user[names]=tj&user[names]=holowaychuk&user[names]=TJ'))
.to.eql({ user: { names: ['tj', 'holowaychuk', 'TJ'] }});
expect(qs.parse('user[name][first]=tj&user[name][first]=TJ'))
.to.eql({ user: { name: { first: ['tj', 'TJ'] }}});
var o = qs.parse('existing[fcbaebfecc][name][last]=tj')
o.should.eql({ existing: { 'fcbaebfecc': { name: { last: 'tj' }}}})
Array.isArray(o.existing).should.be.false;
},
expect(o).to.eql({ existing: { 'fcbaebfecc': { name: { last: 'tj' }}}})
expect(Array.isArray(o.existing)).to.equal(false);
})
'test right-hand brackets': function(){
qs.parse('pets=["tobi"]')
.should.eql({ pets: '["tobi"]' });
it('should support arrays with indexes', function(){
expect(qs.parse('foo[0]=bar&foo[1]=baz')).to.eql({ foo: ['bar', 'baz'] });
expect(qs.parse('foo[1]=bar&foo[0]=baz')).to.eql({ foo: ['baz', 'bar'] });
expect(qs.parse('foo[base64]=RAWR')).to.eql({ foo: { base64: 'RAWR' }});
expect(qs.parse('foo[64base]=RAWR')).to.eql({ foo: { '64base': 'RAWR' }});
})
qs.parse('operators=[">=", "<="]')
.should.eql({ operators: '[">=", "<="]' });
it('should expand to an array when dupliate keys are present', function(){
expect(qs.parse('items=bar&items=baz&items=raz'))
.to.eql({ items: ['bar', 'baz', 'raz'] });
})
qs.parse('op[>=]=[1,2,3]')
.should.eql({ op: { '>=': '[1,2,3]' }});
it('should support right-hand side brackets', function(){
expect(qs.parse('pets=["tobi"]'))
.to.eql({ pets: '["tobi"]' });
qs.parse('op[>=]=[1,2,3]&op[=]=[[[[1]]]]')
.should.eql({ op: { '>=': '[1,2,3]', '=': '[[[[1]]]]' }});
},
'test duplicates': function(){
qs.parse('items=bar&items=baz&items=raz')
.should.eql({ items: ['bar', 'baz', 'raz'] });
},
expect(qs.parse('operators=[">=", "<="]'))
.to.eql({ operators: '[">=", "<="]' });
'test empty': function(){
qs.parse('').should.eql({});
qs.parse(undefined).should.eql({});
qs.parse(null).should.eql({});
},
expect(qs.parse('op[>=]=[1,2,3]'))
.to.eql({ op: { '>=': '[1,2,3]' }});
'test arrays with indexes': function(){
qs.parse('foo[0]=bar&foo[1]=baz').should.eql({ foo: ['bar', 'baz'] });
qs.parse('foo[1]=bar&foo[0]=baz').should.eql({ foo: ['baz', 'bar'] });
qs.parse('foo[base64]=RAWR').should.eql({ foo: { base64: 'RAWR' }});
qs.parse('foo[64base]=RAWR').should.eql({ foo: { '64base': 'RAWR' }});
},
expect(qs.parse('op[>=]=[1,2,3]&op[=]=[[[[1]]]]'))
.to.eql({ op: { '>=': '[1,2,3]', '=': '[[[[1]]]]' }});
})
'test arrays becoming objects': function(){
qs.parse('foo[0]=bar&foo[bad]=baz').should.eql({ foo: { 0: "bar", bad: "baz" }});
qs.parse('foo[bad]=baz&foo[0]=bar').should.eql({ foo: { 0: "bar", bad: "baz" }});
},
it('should support empty values', function(){
expect(qs.parse('')).to.eql({});
expect(qs.parse(undefined)).to.eql({});
expect(qs.parse(null)).to.eql({});
})
'test bleed-through of Array native properties/methods': function(){
Array.prototype.protoProperty = true;
Array.prototype.protoFunction = function () {};
qs.parse('foo=bar').should.eql({ foo: 'bar' });
},
it('should transform arrays to objects', function(){
expect(qs.parse('foo[0]=bar&foo[bad]=baz')).to.eql({ foo: { 0: "bar", bad: "baz" }});
expect(qs.parse('foo[bad]=baz&foo[0]=bar')).to.eql({ foo: { 0: "bar", bad: "baz" }});
})
'test malformed uri': function(){
qs.parse('{%:%}').should.eql({ '{%:%}': '' });
qs.parse('foo=%:%}').should.eql({ 'foo': '%:%}' });
},
it('should support malformed uri chars', function(){
expect(qs.parse('{%:%}')).to.eql({ '{%:%}': '' });
expect(qs.parse('foo=%:%}')).to.eql({ 'foo': '%:%}' });
})
'test semi-parsed': function(){
qs.parse({ 'user[name]': 'tobi' })
.should.eql({ user: { name: 'tobi' }});
it('should support semi-parsed strings', function(){
expect(qs.parse({ 'user[name]': 'tobi' }))
.to.eql({ user: { name: 'tobi' }});
qs.parse({ 'user[name]': 'tobi', 'user[email][main]': 'tobi@lb.com' })
.should.eql({ user: { name: 'tobi', email: { main: 'tobi@lb.com' } }});
}
// 'test complex': function(){
// qs.parse('users[][name][first]=tj&users[foo]=bar')
// .should.eql({
// users: [ { name: 'tj' }, { name: 'tobi' }, { foo: 'bar' }]
// });
//
// qs.parse('users[][name][first]=tj&users[][name][first]=tobi')
// .should.eql({
// users: [ { name: 'tj' }, { name: 'tobi' }]
// });
// }
};
expect(qs.parse({ 'user[name]': 'tobi', 'user[email][main]': 'tobi@lb.com' }))
.to.eql({ user: { name: 'tobi', email: { main: 'tobi@lb.com' } }});
})
})
/**
* Module dependencies.
*/
if (require.register) {
var qs = require('querystring');
} else {
var qs = require('../')
, expect = require('expect.js');
}
var qs = require('../')
, should = require('should')
, str_identities = {
'basics': [
{ str: 'foo=bar', obj: {'foo' : 'bar'}},
{ str: 'foo=%22bar%22', obj: {'foo' : '\"bar\"'}},
{ str: 'foo=', obj: {'foo': ''}},
{ str: 'foo=1&bar=2', obj: {'foo' : '1', 'bar' : '2'}},
{ str: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', obj: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
{ str: 'foo%3Dbaz=bar', obj: {'foo=baz': 'bar'}},
{ str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}}
],
'escaping': [
{ str: 'foo=foo%20bar', obj: {foo: 'foo bar'}},
{ str: 'cht=p3&chd=t%3A60%2C40&chs=250x100&chl=Hello%7CWorld', obj: {
cht: 'p3'
, chd: 't:60,40'
, chs: '250x100'
, chl: 'Hello|World'
}}
],
'nested': [
{ str: 'foo[]=bar&foo[]=quux', obj: {'foo' : ['bar', 'quux']}},
{ str: 'foo[]=bar', obj: {foo: ['bar']}},
{ str: 'foo[]=1&foo[]=2', obj: {'foo' : ['1', '2']}},
{ str: 'foo=bar&baz[]=1&baz[]=2&baz[]=3', obj: {'foo' : 'bar', 'baz' : ['1', '2', '3']}},
{ str: 'foo[]=bar&baz[]=1&baz[]=2&baz[]=3', obj: {'foo' : ['bar'], 'baz' : ['1', '2', '3']}},
{ str: 'x[y][z]=1', obj: {'x' : {'y' : {'z' : '1'}}}},
{ str: 'x[y][z][]=1', obj: {'x' : {'y' : {'z' : ['1']}}}},
{ str: 'x[y][z]=2', obj: {'x' : {'y' : {'z' : '2'}}}},
{ str: 'x[y][z][]=1&x[y][z][]=2', obj: {'x' : {'y' : {'z' : ['1', '2']}}}},
{ str: 'x[y][][z]=1', obj: {'x' : {'y' : [{'z' : '1'}]}}},
{ str: 'x[y][][z][]=1', obj: {'x' : {'y' : [{'z' : ['1']}]}}},
{ str: 'x[y][][z]=1&x[y][][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'w' : '2'}]}}},
{ str: 'x[y][][v][w]=1', obj: {'x' : {'y' : [{'v' : {'w' : '1'}}]}}},
{ str: 'x[y][][z]=1&x[y][][v][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'v' : {'w' : '2'}}]}}},
{ str: 'x[y][][z]=1&x[y][][z]=2', obj: {'x' : {'y' : [{'z' : '1'}, {'z' : '2'}]}}},
{ str: 'x[y][][z]=1&x[y][][w]=a&x[y][][z]=2&x[y][][w]=3', obj: {'x' : {'y' : [{'z' : '1', 'w' : 'a'}, {'z' : '2', 'w' : '3'}]}}},
{ str: 'user[name][first]=tj&user[name][last]=holowaychuk', obj: { user: { name: { first: 'tj', last: 'holowaychuk' }}}}
],
'errors': [
{ obj: 'foo=bar', message: 'stringify expects an object' },
{ obj: ['foo', 'bar'], message: 'stringify expects an object' }
],
'numbers': [
{ str: 'limit[]=1&limit[]=2&limit[]=3', obj: { limit: [1, 2, '3'] }},
{ str: 'limit=1', obj: { limit: 1 }}
]
};
var str_identities = {
'basics': [
{ str: 'foo=bar', obj: {'foo' : 'bar'}},
{ str: 'foo=%22bar%22', obj: {'foo' : '\"bar\"'}},
{ str: 'foo=', obj: {'foo': ''}},
{ str: 'foo=1&bar=2', obj: {'foo' : '1', 'bar' : '2'}},
{ str: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', obj: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
{ str: 'foo%3Dbaz=bar', obj: {'foo=baz': 'bar'}},
{ str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}}
],
'escaping': [
{ str: 'foo=foo%20bar', obj: {foo: 'foo bar'}},
{ str: 'cht=p3&chd=t%3A60%2C40&chs=250x100&chl=Hello%7CWorld', obj: {
cht: 'p3'
, chd: 't:60,40'
, chs: '250x100'
, chl: 'Hello|World'
}}
],
'nested': [
{ str: 'foo[0]=bar&foo[1]=quux', obj: {'foo' : ['bar', 'quux']}},
{ str: 'foo[0]=bar', obj: {foo: ['bar']}},
{ str: 'foo[0]=1&foo[1]=2', obj: {'foo' : ['1', '2']}},
{ str: 'foo=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : 'bar', 'baz' : ['1', '2', '3']}},
{ str: 'foo[0]=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : ['bar'], 'baz' : ['1', '2', '3']}},
{ str: 'x[y][z]=1', obj: {'x' : {'y' : {'z' : '1'}}}},
{ str: 'x[y][z][0]=1', obj: {'x' : {'y' : {'z' : ['1']}}}},
{ str: 'x[y][z]=2', obj: {'x' : {'y' : {'z' : '2'}}}},
{ str: 'x[y][z][0]=1&x[y][z][1]=2', obj: {'x' : {'y' : {'z' : ['1', '2']}}}},
{ str: 'x[y][0][z]=1', obj: {'x' : {'y' : [{'z' : '1'}]}}},
{ str: 'x[y][0][z][0]=1', obj: {'x' : {'y' : [{'z' : ['1']}]}}},
{ str: 'x[y][0][z]=1&x[y][0][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'w' : '2'}]}}},
{ str: 'x[y][0][v][w]=1', obj: {'x' : {'y' : [{'v' : {'w' : '1'}}]}}},
{ str: 'x[y][0][z]=1&x[y][0][v][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'v' : {'w' : '2'}}]}}},
{ str: 'x[y][0][z]=1&x[y][1][z]=2', obj: {'x' : {'y' : [{'z' : '1'}, {'z' : '2'}]}}},
{ str: 'x[y][0][z]=1&x[y][0][w]=a&x[y][1][z]=2&x[y][1][w]=3', obj: {'x' : {'y' : [{'z' : '1', 'w' : 'a'}, {'z' : '2', 'w' : '3'}]}}},
{ str: 'user[name][first]=tj&user[name][last]=holowaychuk', obj: { user: { name: { first: 'tj', last: 'holowaychuk' }}}}
],
'errors': [
{ obj: 'foo=bar', message: 'stringify expects an object' },
{ obj: ['foo', 'bar'], message: 'stringify expects an object' }
],
'numbers': [
{ str: 'limit[0]=1&limit[1]=2&limit[2]=3', obj: { limit: [1, 2, '3'] }},
{ str: 'limit=1', obj: { limit: 1 }}
]
};
// Assert error
function err(fn, msg){
var err;
try {
fn();
} catch (e) {
should.equal(e.message, msg);
return;
}
throw new Error('no exception thrown, expected "' + msg + '"');
}
function test(type) {
var str, obj;
for (var i = 0; i < str_identities[type].length; i++) {
str = str_identities[type][i].str;
obj = str_identities[type][i].obj;
qs.stringify(obj).should.eql(str);
return function(){
var str, obj;
for (var i = 0; i < str_identities[type].length; i++) {
str = str_identities[type][i].str;
obj = str_identities[type][i].obj;
expect(qs.stringify(obj)).to.eql(str);
}
}
}
module.exports = {
'test basics': function() {
test('basics');
},
'test escaping': function() {
test('escaping');
},
'test nested': function() {
test('nested');
},
'test numbers': function(){
test('numbers');
},
'test errors': function() {
var obj, message;
for (var i = 0; i < str_identities['errors'].length; i++) {
message = str_identities['errors'][i].message;
obj = str_identities['errors'][i].obj;
err(function(){ qs.stringify(obj) }, message);
}
}
};
describe('qs.stringify()', function(){
it('should support the basics', test('basics'))
it('should support escapes', test('escaping'))
it('should support nesting', test('nested'))
it('should support numbers', test('numbers'))
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc