Skip to content

Commit

Permalink
Merge changes from Trott/querystring
Browse files Browse the repository at this point in the history
"add escape/unescape per http://nodejs.org/api/querystring.html, fix for
Gozala#6"
  • Loading branch information
balu committed Jan 2, 2017
2 parents 17ab5d0 + 8089e01 commit 344d2fd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
4 changes: 2 additions & 2 deletions decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ module.exports = function(qs, sep, eq, options) {
vstr = '';
}

k = decodeURIComponent(kstr);
v = decodeURIComponent(vstr);
k = this.unescape(kstr);
v = this.unescape(vstr);

if (!hasOwnProperty(obj, k)) {
obj[k] = v;
Expand Down
11 changes: 6 additions & 5 deletions encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var stringifyPrimitive = function(v) {
};

module.exports = function(obj, sep, eq, name) {
var that = this;
sep = sep || '&';
eq = eq || '=';
if (obj === null) {
Expand All @@ -46,21 +47,21 @@ module.exports = function(obj, sep, eq, name) {

if (typeof obj === 'object') {
return map(objectKeys(obj), function(k) {
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
var ks = that.escape(stringifyPrimitive(k)) + eq;
if (isArray(obj[k])) {
return map(obj[k], function(v) {
return ks + encodeURIComponent(stringifyPrimitive(v));
return ks + that.escape(stringifyPrimitive(v));
}).join(sep);
} else {
return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
return ks + that.escape(stringifyPrimitive(obj[k]));
}
}).join(sep);

}

if (!name) return '';
return encodeURIComponent(stringifyPrimitive(name)) + eq +
encodeURIComponent(stringifyPrimitive(obj));
return that.escape(stringifyPrimitive(name)) + eq +
that.escape(stringifyPrimitive(obj));
};

var isArray = Array.isArray || function (xs) {
Expand Down
24 changes: 24 additions & 0 deletions escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// 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.

'use strict';

module.exports = encodeURIComponent;
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
'use strict';

exports.decode = exports.parse = require('./decode');
exports.encode = exports.stringify = require('./encode');
module.exports = (function () {
var methods = {};

methods.decode = methods.parse = require('./decode');
methods.encode = methods.stringify = require('./encode');
methods.escape = require('./escape');
methods.unescape = require('./unescape');

return methods;
}());

26 changes: 26 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,29 @@ exports['test nested in colon'] = function(assert) {

assert.deepEqual({}, qs.parse(), 'parse undefined');
};

exports['test stringify() with escape() override'] = function(assert) {
var original = qs.escape;
qs.escape = function (value) { return value; };
var f = qs.stringify({a: 'a :'});
assert.equal(f, 'a=a :' , 'stringify with escape() override"');
qs.escape = original;
};

exports['test escape()'] = function(assert) {
var f = qs.escape('a :');
assert.equal(f, 'a%20%3A', 'escape "a :"');
};

exports['test parse() with unescape() override'] = function(assert) {
var original = qs.unescape;
qs.unescape = function (value) { return 'static'; };
var f = qs.parse('a=a :');
assert.deepEqual(f, {static: 'static'}, 'unescape "a :" with override"');
qs.unescape = original;
};

exports['test unescape()'] = function(assert) {
var f = qs.unescape('a%20%3A');
assert.equal(f, 'a :', 'unescape "a :"');
};
24 changes: 24 additions & 0 deletions unescape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// 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.

'use strict';

module.exports = decodeURIComponent;

0 comments on commit 344d2fd

Please sign in to comment.