URL Parameter Utility
This module provides utility functions for managing url parameters.
- add/replace url parameter
- remove url parameter
- parse/stringify object representing url
Only string operations are supported, changes to window.history
are left to the consumer.
npm install --save-dev starparam
import starparam from 'starparam';
Get the value of a url parameter.
starparam.get('http://localhost?foo=bar', 'foo') // returns 'bar'
Generate a url string with a parameter added/updated.
starparam.set('http://localhost?foo=bar', 'baz', 'zoo'); // returns http://localhost?foo=bar&baz=zoo
Generate a url string with a parameter removed.
starparam.remove('http://localhost?foo=bar', 'foo'); // returns http://localhost
Parse the url, breaking it up into a simple representative object.
var oUrl = starparam.parse('http://localhost?foo=bar#one');
oUrl.prefix // 'http://localhost'
oUrl.params // array of simple param objects
oUrl.params[0].name // 'foo'
oUrl.params[0].value // 'bar'
oUrl.hash // 'one'
Convert from a starparam url object into a url string (the inverse of parse
).
starparam.stringify(starparam.parse('http://localhost?foo=bar')) // returns string: 'http://localhost?foo=bar'