Query String Manipulator
Effortlessly manipulate query string parameters into your desired URL. You pass url and a set of actions to be done to QSM and you get you URL string back.
Install
QSM is written for ES modules
npm install query-string-manipulator
Usage
Lets assume that you already have it imported
;
Set parameters
Lets say that you want to add page number to a search result.
;// https://www.google.cz/search?q=hello+world&num=20
It also works if the page number is already set
;// https://www.google.cz/search?q=hello+world&num=40
It also works when passing a parameter as an array.
;// https://www.google.cz/search?q=hello+world&num=20&num=40&num=60
Remove parameters
Say that you now want to go back to first page
;// https://www.google.cz/search?q=hello+world
Or go to the empty search page
;// https://www.google.cz/search
Toggle parameters
Say that you have a button on your page that enables filter and disables it when you click it again.
;// https://www.google.cz/search?q=hello+world&num=20&tbm=isch ;// https://www.google.cz/search?q=hello+world&num=20
Constants
If you like "symbols", you can go like this:
;
Support methods
But wait, there is more!
Getting URL params
Method getUrlParams
returns list of all parameters in form of array of objects. It cannot be returned in form of key-pair values because there can be multiple same name query params.
/* returns[ { key: 'select', value: 'users' }, { key: 'getId', value: '10', }]*/
Resolve URL params
Method resolveUrlParams
returns parameters after changed by user specified actions.
const urlParams = key: 'select' value: 'users' key: 'getId' value: '10' ;const paramActions = remove: 'getId' set: select: 'userGroups' ; /* returns[ { key: 'select', value: 'userGroups' }]*/
Putting params together
Method constructUrlParams
returns query string part of the URL from parameters.
// returns "select=users&getId=10"