Skip to content
Browse files

everythign else

  • Loading branch information...
1 parent 2d90fac commit 6014c1309b8f982ba05cb0338c219f52ddd9c0fe @isaacs committed Jan 25, 2011
Showing with 367 additions and 0 deletions.
  1. +18 −0 LICENSE
  2. +10 −0 package.json
  3. +158 −0 querystring.js
  4. +181 −0 test-querystring.js
View
18 LICENSE
@@ -0,0 +1,18 @@
+Copyright 2009, 2010 Joyent, Inc. All rights reserved.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
View
10 package.json
@@ -0,0 +1,10 @@
+{"name":"querystring"
+,"version":"1.0.0"
+,"description":"The querystring module from nodejs"
+,"author":"Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)"
+,"license":{"type":"MIT"
+ ,"url":"https://github.com/isaacs/node-querystring/raw/master/LICENSE"}
+,"main":"querystring.js"
+,"repositories":[{"type":"git"
+ ,"url":"https://github.com/isaacs/node-querystring.git"}]
+,"scripts":{"test":"node test-querystring.js"}}
View
158 querystring.js
@@ -0,0 +1,158 @@
+// Query String Utilities
+
+var QueryString = exports;
+var urlDecode = process.binding('http_parser').urlDecode;
+
+
+function charCode(c) {
+ return c.charCodeAt(0);
+}
+
+
+// a safe fast alternative to decodeURIComponent
+QueryString.unescapeBuffer = function(s, decodeSpaces) {
+ var out = new Buffer(s.length);
+ var state = 'CHAR'; // states: CHAR, HEX0, HEX1
+ var n, m, hexchar;
+
+ for (var inIndex = 0, outIndex = 0; inIndex <= s.length; inIndex++) {
+ var c = s.charCodeAt(inIndex);
+ switch (state) {
+ case 'CHAR':
+ switch (c) {
+ case charCode('%'):
+ n = 0;
+ m = 0;
+ state = 'HEX0';
+ break;
+ case charCode('+'):
+ if (decodeSpaces) c = charCode(' ');
+ // pass thru
+ default:
+ out[outIndex++] = c;
+ break;
+ }
+ break;
+
+ case 'HEX0':
+ state = 'HEX1';
+ hexchar = c;
+ if (charCode('0') <= c && c <= charCode('9')) {
+ n = c - charCode('0');
+ } else if (charCode('a') <= c && c <= charCode('f')) {
+ n = c - charCode('a') + 10;
+ } else if (charCode('A') <= c && c <= charCode('F')) {
+ n = c - charCode('A') + 10;
+ } else {
+ out[outIndex++] = charCode('%');
+ out[outIndex++] = c;
+ state = 'CHAR';
+ break;
+ }
+ break;
+
+ case 'HEX1':
+ state = 'CHAR';
+ if (charCode('0') <= c && c <= charCode('9')) {
+ m = c - charCode('0');
+ } else if (charCode('a') <= c && c <= charCode('f')) {
+ m = c - charCode('a') + 10;
+ } else if (charCode('A') <= c && c <= charCode('F')) {
+ m = c - charCode('A') + 10;
+ } else {
+ out[outIndex++] = charCode('%');
+ out[outIndex++] = hexchar;
+ out[outIndex++] = c;
+ break;
+ }
+ out[outIndex++] = 16 * n + m;
+ break;
+ }
+ }
+
+ // TODO support returning arbitrary buffers.
+
+ return out.slice(0, outIndex - 1);
+};
+
+
+QueryString.unescape = function(s, decodeSpaces) {
+ return QueryString.unescapeBuffer(s, decodeSpaces).toString();
+};
+
+
+QueryString.escape = function(str) {
+ return encodeURIComponent(str);
+};
+
+var stringifyPrimitive = function(v) {
+ switch (typeof v) {
+ case 'string':
+ return v;
+
+ case 'boolean':
+ return v ? 'true' : 'false';
+
+ case 'number':
+ return isFinite(v) ? v : '';
+
+ default:
+ return '';
+ }
+};
+
+
+QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) {
+ sep = sep || '&';
+ eq = eq || '=';
+ obj = (obj === null) ? undefined : obj;
+
+ switch (typeof obj) {
+ case 'object':
+ return Object.keys(obj).map(function(k) {
+ if (Array.isArray(obj[k])) {
+ return obj[k].map(function(v) {
+ return QueryString.escape(stringifyPrimitive(k)) +
+ eq +
+ QueryString.escape(stringifyPrimitive(v));
+ }).join(sep);
+ } else {
+ return QueryString.escape(stringifyPrimitive(k)) +
+ eq +
+ QueryString.escape(stringifyPrimitive(obj[k]));
+ }
+ }).join(sep);
+
+ default:
+ if (!name) return '';
+ return QueryString.escape(stringifyPrimitive(name)) + eq +
+ QueryString.escape(stringifyPrimitive(obj));
+ }
+};
+
+// Parse a key=val string.
+QueryString.parse = QueryString.decode = function(qs, sep, eq) {
+ sep = sep || '&';
+ eq = eq || '=';
+ var obj = {};
+
+ if (typeof qs !== 'string' || qs.length === 0) {
+ return obj;
+ }
+
+ qs.split(sep).forEach(function(kvp) {
+ var x = kvp.split(eq);
+ var k = QueryString.unescape(x[0], true);
+ var v = QueryString.unescape(x.slice(1).join(eq), true);
+
+ if (!(k in obj)) {
+ obj[k] = v;
+ } else if (!Array.isArray(obj[k])) {
+ obj[k] = [obj[k], v];
+ } else {
+ obj[k].push(v);
+ }
+ });
+
+ return obj;
+};
View
181 test-querystring.js
@@ -0,0 +1,181 @@
+var assert = require('assert');
+
+// test using assert
+var qs = require('./querystring');
+
+// folding block, commented to pass gjslint
+// {{{
+// [ wonkyQS, canonicalQS, obj ]
+var qsTestCases = [
+ ['foo=918854443121279438895193',
+ 'foo=918854443121279438895193',
+ {'foo': '918854443121279438895193'}],
+ ['foo=bar', 'foo=bar', {'foo': 'bar'}],
+ ['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}],
+ ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}],
+ ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F',
+ 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F',
+ {'my weird field': 'q1!2"\'w$5&7/z8)?' }],
+ ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}],
+ ['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}],
+ ['str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
+ 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
+ { 'str': 'foo',
+ 'arr': ['1', '2', '3'],
+ 'somenull': '',
+ 'undef': ''}],
+ [' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}],
+ ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}],
+ ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }]
+];
+
+// [ wonkyQS, canonicalQS, obj ]
+var qsColonTestCases = [
+ ['foo:bar', 'foo:bar', {'foo': 'bar'}],
+ ['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}],
+ ['foo:1&bar:2;baz:quux',
+ 'foo:1%26bar%3A2;baz:quux',
+ {'foo': '1&bar:2', 'baz': 'quux'}],
+ ['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}],
+ ['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}]
+];
+
+// [wonkyObj, qs, canonicalObj]
+var extendedFunction = function() {};
+extendedFunction.prototype = {a: 'b'};
+var qsWeirdObjects = [
+ [{regexp: /./g}, 'regexp=', {'regexp': ''}],
+ [{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}],
+ [{fn: function() {}}, 'fn=', {'fn': ''}],
+ [{fn: new Function('')}, 'fn=', {'fn': ''}],
+ [{math: Math}, 'math=', {'math': ''}],
+ [{e: extendedFunction}, 'e=', {'e': ''}],
+ [{d: new Date()}, 'd=', {'d': ''}],
+ [{d: Date}, 'd=', {'d': ''}],
+ [{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}],
+ [{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],
+ [{n: null}, 'n=', {'n': ''}],
+ [{nan: NaN}, 'nan=', {'nan': ''}],
+ [{inf: Infinity}, 'inf=', {'inf': ''}]
+];
+// }}}
+
+var Script = require('vm').Script;
+var foreignObject = Script.runInContext('({"foo": ["bar", "baz"]})',
+ Script.createContext());
+
+var qsNoMungeTestCases = [
+ ['', {}],
+ ['foo=bar&foo=baz', {'foo': ['bar', 'baz']}],
+ ['foo=bar&foo=baz', foreignObject],
+ ['blah=burp', {'blah': 'burp'}],
+ ['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}],
+ ['frappucino=muffin&goat%5B%5D=scone&pond=moose',
+ {'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}],
+ ['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}]
+];
+
+assert.strictEqual('918854443121279438895193',
+ qs.parse('id=918854443121279438895193').id);
+
+// test that the canonical qs is parsed properly.
+qsTestCases.forEach(function(testCase) {
+ assert.deepEqual(testCase[2], qs.parse(testCase[0]));
+});
+
+// test that the colon test cases can do the same
+qsColonTestCases.forEach(function(testCase) {
+ assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'));
+});
+
+// test the weird objects, that they get parsed properly
+qsWeirdObjects.forEach(function(testCase) {
+ assert.deepEqual(testCase[2], qs.parse(testCase[1]));
+});
+
+qsNoMungeTestCases.forEach(function(testCase) {
+ assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false));
+});
+
+// test the nested qs-in-qs case
+(function() {
+ var f = qs.parse('a=b&q=x%3Dy%26y%3Dz');
+ f.q = qs.parse(f.q);
+ assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } });
+})();
+
+// nested in colon
+(function() {
+ var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':');
+ f.q = qs.parse(f.q, ';', ':');
+ assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } });
+})();
+
+// now test stringifying
+
+// basic
+qsTestCases.forEach(function(testCase) {
+ assert.equal(testCase[1], qs.stringify(testCase[2]));
+});
+
+qsColonTestCases.forEach(function(testCase) {
+ assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'));
+});
+
+qsWeirdObjects.forEach(function(testCase) {
+ assert.equal(testCase[1], qs.stringify(testCase[0]));
+});
+
+// nested
+var f = qs.stringify({
+ a: 'b',
+ q: qs.stringify({
+ x: 'y',
+ y: 'z'
+ })
+});
+assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz');
+
+assert.doesNotThrow(function() {
+ qs.parse(undefined);
+});
+
+// nested in colon
+var f = qs.stringify({
+ a: 'b',
+ q: qs.stringify({
+ x: 'y',
+ y: 'z'
+ }, ';', ':')
+}, ';', ':');
+assert.equal(f, 'a:b;q:x%3Ay%3By%3Az');
+
+
+assert.deepEqual({}, qs.parse());
+
+
+
+var b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' +
+ '%0d%ac%a2%2f%9d%eb%d8%a2%e6');
+// <Buffer d3 f2 55 67 1f 36 76 24 5e 98 cb 0d ac a2 2f 9d eb d8 a2 e6>
+assert.equal(0xd3, b[0]);
+assert.equal(0xf2, b[1]);
+assert.equal(0x55, b[2]);
+assert.equal(0x67, b[3]);
+assert.equal(0x1f, b[4]);
+assert.equal(0x36, b[5]);
+assert.equal(0x76, b[6]);
+assert.equal(0x24, b[7]);
+assert.equal(0x5e, b[8]);
+assert.equal(0x98, b[9]);
+assert.equal(0xcb, b[10]);
+assert.equal(0x0d, b[11]);
+assert.equal(0xac, b[12]);
+assert.equal(0xa2, b[13]);
+assert.equal(0x2f, b[14]);
+assert.equal(0x9d, b[15]);
+assert.equal(0xeb, b[16]);
+assert.equal(0xd8, b[17]);
+assert.equal(0xa2, b[18]);
+assert.equal(0xe6, b[19]);
+

0 comments on commit 6014c13

Please sign in to comment.
Something went wrong with that request. Please try again.