Skip to content

Commit

Permalink
[fixed] Path.decode/encode with query string
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Nov 22, 2014
1 parent 86d3611 commit 5a1ed33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
16 changes: 4 additions & 12 deletions modules/utils/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ var invariant = require('react/lib/invariant');
var merge = require('qs/lib/utils').merge;
var qs = require('qs');

function decodePathSegment(string) {
return decodeURIComponent(string.replace(/\+/g, ' '));
}

function encodePathSegment(string) {
return encodeURIComponent(string).replace(/%20/g, '+');
}

var paramCompileMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$]*)|[*.()\[\]\\+|{}^$]/g;
var paramInjectMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$?]*[?]?)|[*]/g;
var paramInjectTrailingSlashMatcher = /\/\/\?|\/\?/g;
Expand Down Expand Up @@ -46,15 +38,15 @@ var Path = {
/**
* Safely decodes special characters in the given URL path.
*/
decode: function decodePath(path) {
return String(path).split('/').map(decodePathSegment).join('/');
decode: function (path) {
return decodeURI(path.replace(/\+/g, ' '));
},

/**
* Safely encodes special characters in the given URL path.
*/
encode: function encodePath(path) {
return String(path).split('/').map(encodePathSegment).join('/');
encode: function (path) {
return encodeURI(path).replace(/%20/g, '+');
},

/**
Expand Down
12 changes: 12 additions & 0 deletions modules/utils/__tests__/Path-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
var expect = require('expect');
var Path = require('../Path');

describe('Path.decode', function () {
it('properly decodes a path with a query string', function () {
expect(Path.decode('/my/short+path?a=b&c=d')).toEqual('/my/short path?a=b&c=d');
});
});

describe('Path.encode', function () {
it('properly encodes a path with a query string', function () {
expect(Path.encode('/my/short path?a=b&c=d')).toEqual('/my/short+path?a=b&c=d');
});
});

describe('Path.extractParamNames', function () {
describe('when a pattern contains no dynamic segments', function () {
it('returns an empty array', function () {
Expand Down

0 comments on commit 5a1ed33

Please sign in to comment.