-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add transferableUrlParameters option to the embed API
- Loading branch information
1 parent
87b7824
commit 5531870
Showing
10 changed files
with
106 additions
and
10 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
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
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
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
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,23 @@ | ||
const urlSearchToParams = (search) => { | ||
var params = {} | ||
if (search !== '' && search !== null) { | ||
var vars = search.split('&') | ||
for (var i = 0; i < vars.length; i++) { | ||
var pair = vars[i].split('=') | ||
params[pair[0]] = decodeURIComponent(pair[1]) | ||
} | ||
} | ||
return params | ||
} | ||
|
||
export const transferUrlParametersToQueryStrings = (transferableUrlParameters, queryStrings) => { | ||
const urlSearchString = window.location.search.substr(1) | ||
const urlSearchParams = urlSearchToParams(urlSearchString) | ||
const queryStringsWithTransferedParams = { ...queryStrings } | ||
transferableUrlParameters.forEach(transferableParam => { | ||
if (!(transferableParam in queryStrings)) { | ||
queryStringsWithTransferedParams[transferableParam] = urlSearchParams[transferableParam] | ||
} | ||
}) | ||
return queryStringsWithTransferedParams | ||
} |
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,32 @@ | ||
import { transferUrlParametersToQueryStrings } from './url-parameters-transfer' | ||
|
||
describe('transferUrlParametersToQueryStrings', () => { | ||
const { location } = window | ||
|
||
beforeAll(() => { | ||
delete window.location | ||
window.location = { search: '?foo=jason&bar=rachel&utm_medium=cpc&utm_campaign=camp2008&utm_source=instagram&embed-hide-footer=false' } | ||
}) | ||
|
||
afterAll(() => { | ||
window.location = location | ||
}) | ||
|
||
it('should return ?foo=jason&bar=rachel&utm_medium=cpc&utm_campaign=camp2008&utm_source=instagram&embed-hide-footer=false', () => { | ||
expect(window.location.search).toEqual('?foo=jason&bar=rachel&utm_medium=cpc&utm_campaign=camp2008&utm_source=instagram&embed-hide-footer=false') | ||
}) | ||
|
||
it('transfer the parameters of the URL in the query strings', () => { | ||
const urlParameters = ['foo', 'bar'] | ||
const queryStrings = {} | ||
const queryStringWithTransferedUrlParameters = transferUrlParametersToQueryStrings(urlParameters, queryStrings) | ||
expect(queryStringWithTransferedUrlParameters).toEqual({ foo: 'jason', bar: 'rachel' }) | ||
}) | ||
|
||
it('does not override existing queryString for embed configuration', () => { | ||
const urlParameters = ['foo', 'bar', 'embed-hide-footer'] | ||
const queryStrings = { 'embed-hide-footer': true } | ||
const queryStringWithTransferedUrlParameters = transferUrlParametersToQueryStrings(urlParameters, queryStrings) | ||
expect(queryStringWithTransferedUrlParameters).toEqual({ foo: 'jason', bar: 'rachel', 'embed-hide-footer': true }) | ||
}) | ||
}) |