-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from yapplabs/improvement/url-generation
appendToQueryParam
- Loading branch information
Showing
3 changed files
with
149 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { isArray } from '@ember/array'; | ||
import { isEmpty } from '@ember/utils'; | ||
|
||
/** | ||
* @function appendToQueryParam | ||
* @export named | ||
* @param {string} url | ||
*/ | ||
export function appendToQueryParam(urlString, param, newValue) { | ||
let url = new URL(urlString); | ||
if (isEmpty(newValue)) { | ||
return url.href; | ||
} | ||
|
||
let existingParamValue = url.searchParams.get(param); | ||
let existingValues = existingParamValue ? existingParamValue.split(',') : []; | ||
newValue = isArray(newValue) ? newValue : newValue.split(','); | ||
// Merge the two arrays, filtering out empty values | ||
existingValues = existingValues.concat(newValue).filter(Boolean); | ||
// Remove duplicate content | ||
existingValues = [...new Set(existingValues)] | ||
url.searchParams.set(param, existingValues.join(',')); | ||
|
||
return url.href; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { module, test } from 'qunit'; | ||
import { appendToQueryParam } from 'ember-data-utils/utils/url'; | ||
|
||
module('Unit | Utility | url', function() { | ||
|
||
test('appendToQueryParam()', function(assert) { | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com', | ||
'include', | ||
'profile' | ||
), | ||
'https://www.test.com/?include=profile', | ||
'it adds a new query param' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com?include=person', | ||
'include', | ||
'profile' | ||
), | ||
'https://www.test.com/?include=person%2Cprofile', | ||
'it adds to an existing param' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com?query=person', | ||
'include', | ||
'profile' | ||
), | ||
'https://www.test.com/?query=person&include=profile', | ||
'it adds a new query param while leaving existing params alone' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com', | ||
'include', | ||
['profile', 'pizza'] | ||
), | ||
'https://www.test.com/?include=profile%2Cpizza', | ||
'new params can be an array' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com?include=party', | ||
'include', | ||
['profile', 'pizza'] | ||
), | ||
'https://www.test.com/?include=party%2Cprofile%2Cpizza', | ||
'array params are concatted with existing param values' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com?include=party', | ||
'include', | ||
'profile,pizza' | ||
), | ||
'https://www.test.com/?include=party%2Cprofile%2Cpizza', | ||
'params that are strings are added' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com?include=party', | ||
'include', | ||
null | ||
), | ||
'https://www.test.com/?include=party', | ||
'null params are ignored' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com?include=party', | ||
'include', | ||
[] | ||
), | ||
'https://www.test.com/?include=party', | ||
'empty arrays are ignored' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com?include=party', | ||
'include', | ||
['party'] | ||
), | ||
'https://www.test.com/?include=party', | ||
'param values are not duplicated' | ||
); | ||
assert.equal( | ||
appendToQueryParam( | ||
'https://www.test.com?include=party', | ||
'include', | ||
'party,pizza' | ||
), | ||
'https://www.test.com/?include=party%2Cpizza', | ||
'param values are not duplicated even if a string' | ||
); | ||
}); | ||
}); |