Created
July 9, 2020 14:24
-
-
Save timwright12/cf4482b891efadd2c4242d2107af00d4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Get URL params | |
* | |
* @param {String} query - The location.search query | |
* @returns {Object} - the params | |
*/ | |
export const getUrlParams = ( query ) => { | |
const vars = query.split( '&' ); | |
const queryString = {}; | |
for ( let i = 0; i < vars.length; i++ ) { | |
const pair = vars[i].split( '=' ); | |
const key = decodeURIComponent( pair[0].replace( '?', '' ) ); | |
const value = decodeURIComponent( pair[1] ); | |
if ( 'undefined' === typeof queryString[key] ) { | |
queryString[key] = decodeURIComponent( value ); | |
} else if ( 'string' === typeof queryString[key] ) { | |
const arr = [queryString[key], decodeURIComponent( value )]; | |
queryString[key] = arr; | |
} else { | |
queryString[key].push( decodeURIComponent( value ) ); | |
} | |
} | |
return queryString; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment