urlq
v0.1.0
Published
url query library to get and modify query params
Downloads
1,077
Readme
urlq
A lightweight library to get/set url query string values.
Usage
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'
Functions
addVal(q, p, v) ⇒ 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. |
getParam(q, p) ⇒ Array
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. |
makeValidQuery(q) ⇒ String
Returns a valid formatted url query string
Kind: global function
| Param | Type | Description | | --- | --- | --- | | q | String | The query string to format. |
removeVal(q, p, v) ⇒ String
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. |
removeParam(q, p) ⇒ String
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. |
updateParam(q, p, vs) ⇒ String
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. |
updateQuery(q)
Updates the browser history with a new query string.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | q | String | The new query string. |
Test
npm test