Skip to content

Commit

Permalink
[Fix] properly stringify a query object
Browse files Browse the repository at this point in the history
Fixes #65
  • Loading branch information
ljharb committed Sep 8, 2023
1 parent 855337b commit 4cec245
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "defunctzombie",
"dependencies": {
"punycode": "^1.4.1",
"qs": "^6.11.0"
"qs": "^6.11.2"
},
"main": "./url.js",
"keywords": [
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

var test = require('mocha').test;
var assert = require('assert');
var nodeURL = require('url');

var url = require('../url');

Expand Down Expand Up @@ -2027,6 +2028,15 @@ relativeTests.forEach(function (relativeTest) {
});
});

test('format with querystring', function () {
var obj = { protocol: 'https:', host: 'google.com', pathname: 'test', query: { message: ['value1', 'value2'] } };

var actual = url.format(obj);
var expected = nodeURL.format(obj);

assert.equal(actual, expected, 'format(' + actual + ') == ' + expected + '\nactual:' + actual);
});

/*
* format: [to, from, result]
* the test: ['.//g', 'f:/a', 'f://g'] is a fundamental problem
Expand Down
6 changes: 5 additions & 1 deletion url.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,11 @@ Url.prototype.format = function () {
}

if (this.query && typeof this.query === 'object' && Object.keys(this.query).length) {
query = querystring.stringify(this.query);
query = querystring.stringify(this.query, {
arrayFormat: 'repeat',
encodeValuesOnly: true,
addQueryPrefix: false
});
}

var search = this.search || (query && ('?' + query)) || '';
Expand Down

0 comments on commit 4cec245

Please sign in to comment.