diff --git a/modules/helpers/Path.js b/modules/helpers/Path.js index 1a72a86dc1..114bc200c6 100644 --- a/modules/helpers/Path.js +++ b/modules/helpers/Path.js @@ -1,8 +1,7 @@ var invariant = require('react/lib/invariant'); var merge = require('react/lib/merge'); var qs = require('querystring'); -var urlDecode = require('./urlDecode'); -var urlEncode = require('./urlEncode'); +var URL = require('./URL'); var paramMatcher = /((?::[a-z_$][a-z0-9_$]*)|\*)/ig; var queryMatcher = /\?(.+)/; @@ -43,14 +42,14 @@ var Path = { */ extractParams: function (pattern, path) { if (!isDynamicPattern(pattern)) { - if (pattern === urlDecode(path)) + if (pattern === URL.decode(path)) return {}; // No dynamic segments, but the paths match. return null; } var compiled = compilePattern(pattern); - var match = urlDecode(path).match(compiled.matcher); + var match = URL.decode(path).match(compiled.matcher); if (!match) return null; @@ -89,7 +88,7 @@ var Path = { 'Missing "' + paramName + '" parameter for path "' + pattern + '"' ); - return urlEncode(params[paramName]); + return URL.encode(params[paramName]); }); }, diff --git a/modules/helpers/URL.js b/modules/helpers/URL.js new file mode 100644 index 0000000000..18b9f8f4f9 --- /dev/null +++ b/modules/helpers/URL.js @@ -0,0 +1,22 @@ +var urlEncodedSpaceRE = /\+/g; +var encodedSpaceRE = /%20/g; + +var URL = { + + /* These functions were copied from the https://github.com/cujojs/rest source, MIT licensed */ + + decode: function (str) { + // spec says space should be encoded as '+' + str = str.replace(urlEncodedSpaceRE, ' '); + return decodeURIComponent(str); + }, + + encode: function (str) { + str = encodeURIComponent(str); + // spec says space should be encoded as '+' + return str.replace(encodedSpaceRE, '+'); + } + +}; + +module.exports = URL; diff --git a/modules/helpers/urlDecode.js b/modules/helpers/urlDecode.js deleted file mode 100644 index e86c052249..0000000000 --- a/modules/helpers/urlDecode.js +++ /dev/null @@ -1,11 +0,0 @@ -/** This function was copied from the https://github.com/cujojs/rest source, MIT licensed */ - -var urlEncodedSpaceRE = /\+/g; - -function urlDecode(str) { - // spec says space should be encoded as '+' - str = str.replace(urlEncodedSpaceRE, ' '); - return decodeURIComponent(str); -} - -module.exports = urlDecode; diff --git a/modules/helpers/urlEncode.js b/modules/helpers/urlEncode.js deleted file mode 100644 index 2a0453dd7f..0000000000 --- a/modules/helpers/urlEncode.js +++ /dev/null @@ -1,11 +0,0 @@ -/** This function was copied from the https://github.com/cujojs/rest source, MIT licensed */ - -var encodedSpaceRE = /%20/g; - -function urlEncode(str) { - str = encodeURIComponent(str); - // spec says space should be encoded as '+' - return str.replace(encodedSpaceRE, '+'); -} - -module.exports = urlEncode;