Skip to content

Instantly share code, notes, and snippets.

@timwright12
Created July 9, 2020 14:24
Show Gist options
  • Save timwright12/cf4482b891efadd2c4242d2107af00d4 to your computer and use it in GitHub Desktop.
Save timwright12/cf4482b891efadd2c4242d2107af00d4 to your computer and use it in GitHub Desktop.
/**
* 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