A lightweight library to get/set url query string values.
const urlq = require('urlq');
const q1 = '?sections=carbs,dessert&diets=gluten-free';
urlq.getParam(q1, 'sections')
=> ['carbs', 'dessert']
urlq.getParam(q1, 'diets')
=> ['gluten-free']
urlq.getParam(q1, 'non-existing')
=> []
var q2 = urlq.addVal(q1, 'sections', 'soups');
q2;
=> '?sections=carbs,dessert,soups&diets=gluten-free'
urlq.updateQuery(q2);
urlq.getParam(window.location.search, 'sections');
=> ['carbs', 'dessert', 'soups']
var q3 = urlq.updateParam(q2, 'diets', ['vegetarian', 'pescepescetarian']);
urlq.removeParam(q2, 'sections');
q3;
=> '?diets=vegetarian,pescepescetarian'
- addVal(q, p, v) ⇒
String
Returns a new query string with values added to a query parameter.
- getParam(q, p) ⇒
Array
Returns an array of values for a query parameter.
- makeValidQuery(q) ⇒
String
Returns a valid formatted url query string
- removeVal(q, p, v) ⇒
String
Returns a new query string with a value removed from a parameter.
- removeParam(q, p) ⇒
String
Returns a new query string with a parameter removed.
- updateParam(q, p, vs) ⇒
String
Returns a new query string with updated values from a parameter. If newVals.length = 0 the parameter is removed.
- updateQuery(q)
Updates the browser history with a new query string.
Returns a new query string with values added to a query parameter.
Kind: global function
Param | Type | Description |
---|---|---|
q | String |
The query string. |
p | String |
The query parameter to update. |
v | * |
The value to add. |
Returns an array of values for a query parameter.
Kind: global function
Param | Type | Description |
---|---|---|
q | String |
The query string. |
p | String |
The query parameter. |
Returns a valid formatted url query string
Kind: global function
Param | Type | Description |
---|---|---|
q | String |
The query string to format. |
Returns a new query string with a value removed from a parameter.
Kind: global function
Param | Type | Description |
---|---|---|
q | String |
The query string to update. |
p | String |
The parameter to remove the value. |
v | String |
The value to remove from a parameter. |
Returns a new query string with a parameter removed.
Kind: global function
Param | Type | Description |
---|---|---|
q | String |
The query string to update. |
p | String |
The parameter to remove. |
Returns a new query string with updated values from a parameter. If newVals.length = 0 the parameter is removed.
Kind: global function
Param | Type | Description |
---|---|---|
q | String |
The query string to update. |
p | String |
The param to update. |
vs | Array |
The new values for the param. |
Updates the browser history with a new query string.
Kind: global function
Param | Type | Description |
---|---|---|
q | String |
The new query string. |
npm test