Skip to content

Commit 5a1ed33

Browse files
committed
[fixed] Path.decode/encode with query string
Fixes #490
1 parent 86d3611 commit 5a1ed33

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

modules/utils/Path.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ var invariant = require('react/lib/invariant');
22
var merge = require('qs/lib/utils').merge;
33
var qs = require('qs');
44

5-
function decodePathSegment(string) {
6-
return decodeURIComponent(string.replace(/\+/g, ' '));
7-
}
8-
9-
function encodePathSegment(string) {
10-
return encodeURIComponent(string).replace(/%20/g, '+');
11-
}
12-
135
var paramCompileMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$]*)|[*.()\[\]\\+|{}^$]/g;
146
var paramInjectMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$?]*[?]?)|[*]/g;
157
var paramInjectTrailingSlashMatcher = /\/\/\?|\/\?/g;
@@ -46,15 +38,15 @@ var Path = {
4638
/**
4739
* Safely decodes special characters in the given URL path.
4840
*/
49-
decode: function decodePath(path) {
50-
return String(path).split('/').map(decodePathSegment).join('/');
41+
decode: function (path) {
42+
return decodeURI(path.replace(/\+/g, ' '));
5143
},
5244

5345
/**
5446
* Safely encodes special characters in the given URL path.
5547
*/
56-
encode: function encodePath(path) {
57-
return String(path).split('/').map(encodePathSegment).join('/');
48+
encode: function (path) {
49+
return encodeURI(path).replace(/%20/g, '+');
5850
},
5951

6052
/**

modules/utils/__tests__/Path-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
var expect = require('expect');
22
var Path = require('../Path');
33

4+
describe('Path.decode', function () {
5+
it('properly decodes a path with a query string', function () {
6+
expect(Path.decode('/my/short+path?a=b&c=d')).toEqual('/my/short path?a=b&c=d');
7+
});
8+
});
9+
10+
describe('Path.encode', function () {
11+
it('properly encodes a path with a query string', function () {
12+
expect(Path.encode('/my/short path?a=b&c=d')).toEqual('/my/short+path?a=b&c=d');
13+
});
14+
});
15+
416
describe('Path.extractParamNames', function () {
517
describe('when a pattern contains no dynamic segments', function () {
618
it('returns an empty array', function () {

0 commit comments

Comments
 (0)