Skip to content

Commit

Permalink
fix decode uri parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
worka committed Mar 11, 2020
1 parent 5e941d6 commit 47d3931
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
17 changes: 14 additions & 3 deletions dist/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand All @@ -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);
}
});
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/url.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/_buildParams.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _decodeUrlParameter from './_decodeUrlParameter';

/**
* @param query
* @returns {{}}
Expand All @@ -19,9 +21,9 @@ export default function _buildParams(query) {
params[key] = [];
}

params[key].push(decodeURIComponent(value));
params[key].push(_decodeUrlParameter(value));
} else {
params[key] = decodeURIComponent(value);
params[key] = _decodeUrlParameter(value);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/_buildParamsExtended.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _buildNesting from './_buildNesting';
import _mergeObjectsDeep from './_mergeObjectsDeep';
import _decodeUrlParameter from './_decodeUrlParameter';

/**
* @param query
Expand All @@ -22,7 +23,7 @@ export default function _buildParamsExtended(query) {
const array = raw.split('][');
array.unshift(match[1]);

const nesting = _buildNesting(array, value);
const nesting = _buildNesting(array, _decodeUrlParameter(value));

params = _mergeObjectsDeep(params, nesting);
}
Expand Down
9 changes: 9 additions & 0 deletions src/_decodeUrlParameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Thanks https://gist.github.com/bchapuis/5575512
*
* @param string
* @returns {string}
*/
export default function decodeUrlParameter(string) {
return decodeURIComponent(`${ string }`.replace(/\+/g, '%20'));
}
2 changes: 2 additions & 0 deletions src/url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ describe('GET', () => {
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'))
Expand Down

0 comments on commit 47d3931

Please sign in to comment.