Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
worka committed Feb 24, 2020
1 parent 265fd0f commit 8eefc2c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
10 changes: 7 additions & 3 deletions dist/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
if (key.substr(-2) === '[]') {
key = key.substr(0, key.length - 2);

if (params[key] === undefined) {
if (
params[key] === undefined ||
!Array.isArray(params[key])
) {
params[key] = [];
}

Expand Down Expand Up @@ -98,9 +101,10 @@

function addParams(url, newParams) {
if (newParams instanceof Object) {
var uri = url.split('?', 2)[0]; // concatenating current params with new params
var uri = url.split('?', 2)[0];
var currentParams = getParams(url);

var params = _concat(getParams(url), newParams);
var params = _concat(currentParams, newParams);

url = ''.concat(uri, '?').concat(_buildQuery(params));
}
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: 3 additions & 3 deletions src/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function _buildParams(query) {
if (key.substr(-2) === '[]') {
key = key.substr(0, key.length - 2);

if (params[key] === undefined) {
if (params[key] === undefined || !Array.isArray(params[key])) {
params[key] = [];
}

Expand Down Expand Up @@ -79,8 +79,8 @@ function getParams(url = window.location.href) {
function addParams(url, newParams) {
if (newParams instanceof Object) {
const uri = url.split('?', 2)[0];
// concatenating current params with new params
const params = _concat(getParams(url), newParams);
const currentParams = getParams(url);
const params = _concat(currentParams, newParams);

url = `${uri}?${_buildQuery(params)}`;
}
Expand Down
3 changes: 3 additions & 0 deletions src/url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ test('Get params from URL', () => {
expect(JcURL.getParams('example.com?bar=1&foo')).toEqual({ bar: '1', foo: '' });
expect(JcURL.getParams('example.com?bar=1&bar=2')).toEqual({ bar: '2' });
expect(JcURL.getParams('example.com?bar[]=1&bar[]=2')).toEqual({ bar: ['1', '2'] });
expect(JcURL.getParams('example.com?bar=1&bar[]=2')).toEqual({ bar: ['2'] });
// @todo to realize
// expect(JcURL.getParams('example.com?bar[t]=1&bar[j]=2')).toEqual({ bar: { t: '1', j: '2' } });
// expect(JcURL.getParams('example.com?bar[t]=1&bar[j]=2&bar[j]=3')).toEqual({ bar: { t: '1', j: '3' } });
// expect(JcURL.getParams('example.com?bar[t]=1&bar[j]=2&bar[j][g]=3')).toEqual({ bar: { t: '1', j: { g: 3 } } });
});

test('Add params to URL', () => {
Expand All @@ -16,6 +18,7 @@ test('Add params to URL', () => {
expect(JcURL.addParams('example.com?bar=1&foo', { bar: 2 })).toBe('example.com?bar=2&foo=');
expect(JcURL.addParams('example.com?bar=1&foo', { bar: 2, foo: 2 })).toBe('example.com?bar=2&foo=2');
expect(JcURL.addParams('example.com?bar=1', { bar: [2, 3] })).toBe('example.com?bar[]=2&bar[]=3');
expect(JcURL.addParams('example.com?bar=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=2&bar[]=3&bar[]=4');
expect(JcURL.addParams('example.com?bar=1&bar=2', { bar: [3, 4] })).toBe('example.com?bar[]=3&bar[]=4');
expect(JcURL.addParams('example.com?bar[]=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=1&bar[]=2&bar[]=3&bar[]=4');
// @todo to realize
Expand Down

0 comments on commit 8eefc2c

Please sign in to comment.