string-to-query-params
v0.1.6
Published
Parses a URI-like query string and returns an object composed of parameter/value pairs.
Downloads
5
Maintainers
Readme
string-to-query-params(string)
This is a port of the PrototypeJS method toQueryParams
.
It parses a URI-like query string and returns an object composed of parameter/value
pairs. There's an inverse of this method called toQueryString
that's available
at object-to-query-string.
string-to-query-params
is really targeted at parsing query strings (hence the
default value of "&"
for the separator argument). For this reason, it does not
consider anything that is either before a question mark (which signals the
beginning of a query string) or beyond the hash symbol ("#"
), and runs
decodeURIComponent()
on each parameter/value pair.
string-to-query-params
also aggregates the values of identical keys into an
array of values.
Note that parameters which do not have a specified value will be set to undefined.
Installation
npm install string-to-query-params
Usage
var toQueryParams = require("string-to-query-params");
toQueryParams('section=blog&id=45');
// -> {section: 'blog', id: '45'}
toQueryParams('section=blog;id=45', ';');
// -> {section: 'blog', id: '45'}
toQueryParams('http://www.example.com?section=blog&id=45#comments');
// -> {section: 'blog', id: '45'}
toQueryParams('section=blog&tag=javascript&tag=prototype&tag=doc');
// -> {section: 'blog', tag: ['javascript', 'prototype', 'doc']}
toQueryParams('tag=ruby%20on%20rails');
// -> {tag: 'ruby on rails'}
toQueryParams('id=45&raw');
// -> {id: '45', raw: undefined}
Tests
npm test
Contributing
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code. See the CONTRIBUTING file for more detailed information.