0.1.0 • Published 7 years ago

urlq v0.1.0

Weekly downloads
235
License
MIT
Repository
github
Last release
7 years ago

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

ParamTypeDescription
qStringThe query string.
pStringThe 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

ParamTypeDescription
qStringThe query string.
pStringThe query parameter.

makeValidQuery(q) ⇒ String

Returns a valid formatted url query string

Kind: global function

ParamTypeDescription
qStringThe query string to format.

removeVal(q, p, v) ⇒ String

Returns a new query string with a value removed from a parameter.

Kind: global function

ParamTypeDescription
qStringThe query string to update.
pStringThe parameter to remove the value.
vStringThe value to remove from a parameter.

removeParam(q, p) ⇒ String

Returns a new query string with a parameter removed.

Kind: global function

ParamTypeDescription
qStringThe query string to update.
pStringThe 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

ParamTypeDescription
qStringThe query string to update.
pStringThe param to update.
vsArrayThe new values for the param.

updateQuery(q)

Updates the browser history with a new query string.

Kind: global function

ParamTypeDescription
qStringThe new query string.

Test

npm test