Skip to content

Commit 5c8b597

Browse files
silentroachjasnell
authored andcommittedApr 26, 2016
querystring: using toString for objects on querystring.escape
This commit fixes an inconsistency in querystring.escape objects handling compared to native encodeURIComponent function. Fixes: #5309 PR-URL: #5341 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 677642e commit 5c8b597

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed
 

‎lib/querystring.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ for (var i = 0; i < 256; ++i)
9090
QueryString.escape = function(str) {
9191
// replaces encodeURIComponent
9292
// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
93-
if (typeof str !== 'string')
94-
str += '';
93+
if (typeof str !== 'string') {
94+
if (typeof str === 'object')
95+
str = String(str);
96+
else
97+
str += '';
98+
}
9599
var out = '';
96100
var lastPos = 0;
97101

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
const qs = require('querystring');
6+
7+
assert.deepEqual(qs.escape(5), '5');
8+
assert.deepEqual(qs.escape('test'), 'test');
9+
assert.deepEqual(qs.escape({}), '%5Bobject%20Object%5D');
10+
assert.deepEqual(qs.escape([5, 10]), '5%2C10');
11+
12+
// using toString for objects
13+
assert.strictEqual(
14+
qs.escape({test: 5, toString: () => 'test', valueOf: () => 10 }),
15+
'test'
16+
);
17+
18+
// toString is not callable, must throw an error
19+
assert.throws(() => qs.escape({toString: 5}));
20+
21+
// should use valueOf instead of non-callable toString
22+
assert.strictEqual(qs.escape({toString: 5, valueOf: () => 'test'}), 'test');
23+
24+
assert.throws(() => qs.escape(Symbol('test')));

0 commit comments

Comments
 (0)