Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add escape/unescape per http://nodejs.org/api/querystring.html #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,19 +47,19 @@ module.exports = function(obj, sep, eq, name) {

if (typeof obj === 'object') {
return Object.keys(obj).map(function(k) {
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
var ks = that.escape(stringifyPrimitive(k)) + eq;
if (Array.isArray(obj[k])) {
return obj[k].map(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));
};
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;