diff --git a/dist/url.js b/dist/url.js index 4fe2cf9..e9922ff 100644 --- a/dist/url.js +++ b/dist/url.js @@ -7,10 +7,21 @@ })(this, function(exports) { 'use strict'; + /** + * Thanks https://gist.github.com/bchapuis/5575512 + * + * @param string + * @returns {string} + */ + function decodeUrlParameter(string) { + return decodeURIComponent(''.concat(string).replace(/\+/g, '%20')); + } + /** * @param query * @returns {{}} */ + function _buildParams(query) { var params = {}; @@ -31,9 +42,9 @@ params[key] = []; } - params[key].push(decodeURIComponent(value)); + params[key].push(decodeUrlParameter(value)); } else { - params[key] = decodeURIComponent(value); + params[key] = decodeUrlParameter(value); } }); } @@ -126,7 +137,7 @@ var array = raw.split(']['); array.unshift(match[1]); - var nesting = _buildNesting(array, value); + var nesting = _buildNesting(array, decodeUrlParameter(value)); params = _mergeObjectsDeep(params, nesting); } diff --git a/dist/url.min.js b/dist/url.min.js index b5c6a04..c2c63a4 100644 --- a/dist/url.min.js +++ b/dist/url.min.js @@ -1 +1 @@ -(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.jcurl={}))})(this,function(a){'use strict';function b(a){var b={};return a&&a.split("&").forEach(function(a){var c=a.split("=",2),d=c[0],e=c[1]||"";"[]"===d.substr(-2)?(d=d.substr(0,d.length-2),(void 0===b[d]||!Array.isArray(b[d]))&&(b[d]=[]),b[d].push(decodeURIComponent(e))):b[d]=decodeURIComponent(e)}),b}function c(a,b){var d={};return d[a[a.length-1]]=b,1===a.length?d:c(a.slice(0,a.length-1),d)}function d(a,b){var c=function(a){return a&&a instanceof Object};return c(a)&&c(b)?(Object.keys(b).forEach(function(e){var f=a[e],g=b[e];a[e]=Array.isArray(f)&&Array.isArray(g)?f.concat(g):c(f)&&c(g)?d(Object.assign({},f),g):g}),a):b}function e(a){var b={};return a.split("&").forEach(function(a,e){var f=a.split("=",2),g=f[0],h=f[1]||"",i=g.match(/(.+?)(\[(.*)\])/i);if(i){var j=i[3]||e+"",k=j.split("][");k.unshift(i[1]);var l=c(k,h);b=d(b,l)}}),b}function f(a){var b=[],c=function(c){if(a.hasOwnProperty(c)){var d=a[c];Array.isArray(d)&&d.length?d.forEach(function(a){b.push("".concat(c,"[]=").concat(a))}):b.push("".concat(c,"=").concat(d))}};for(var d in a)c(d);return b.join("&")}function g(a,b,c){Object.keys(a).forEach(function(d){var e=b.concat([d]),f=a[d];f instanceof Object?g(f,e,c):(e.push(f),c.push(e))})}function h(a){var b=[];g(a,[],b);var c=b.map(function(a){return a.reduce(function(b,c,d){return b?d { expect(getParams('example.com?bar=1&bar=2')).toEqual({ bar: '2' }); expect(getParams('example.com?bar[]=1&bar[]=2')).toEqual({ bar: ['1', '2'] }); expect(getParams('example.com?bar=1&bar[]=2')).toEqual({ bar: ['2'] }); + expect(getParams('example.com?bar=test+test%2Ctest')).toEqual({ bar: 'test test,test' }); }); test('getParamsExtended()', () => { + expect(getParamsExtended('example.com?bar[t]=test+test%2Ctest')).toEqual({ bar: { t: 'test test,test' } }); expect(getParamsExtended('example.com?bar[t]=1&bar[j]=2')).toEqual({ bar: { t: '1', j: '2' } }); expect(getParamsExtended('example.com?bar[t]=1&bar[j]=2&bar[j]=3')).toEqual({ bar: { t: '1', j: '3' } }); expect(getParamsExtended('example.com?b[t]=1&b[j]=2&b[j][g]=3'))