bower
Version:
The browser package manager
116 lines (68 loc) • 2.48 kB
Markdown
Utilities for query string manipulation.
Checks if query string contains parameter.
1. `url` (String) : URL or query string.
2. `paramName` (String) : Parameter name.
```js
var url = 'example.com/?lorem=ipsum';
contains(url, 'lorem'); // true
contains(url, 'foo'); //false
```
Parses query string and creates an object of keys => vals.
Will typecast value with [`string/typecast`](string.html
and decode string parameters using `decodeURIComponent()`.
```js
var query = '?foo=bar&lorem=123';
decode(query); // {foo: "bar", lorem: 123}
decode(query, false); // {foo: "bar", lorem: "123"}
```
Encode object into a query string.
Will encode parameters with `encodeURIComponent()`.
```js
encode({foo: "bar", lorem: 123}); // "?foo=bar&lorem=123"
```
Get query parameter value.
Will typecast value with [`string/typecast`](string.html
See: [`setParam()`](
1. `url` (String) : Url.
2. `param` (String) : Parameter name.
3. `[shouldTypecast]` (Boolean) : If it should typecast value.
```js
var url = 'example.com/?foo=bar&lorem=123&ipsum=false';
getParam(url, 'dolor'); // "amet"
getParam(url, 'lorem'); // 123
getParam(url, 'lorem', false); // "123"
```
Parses URL, extracts query string and decodes it.
It will typecast all properties of the query object unless second argument is
`false`.
Alias to: `decode(getQuery(url))`.
```js
var url = 'example.com/?lorem=ipsum&a=123';
parse(url); // {lorem: "ipsum", a: 123}
parse(url, false); // {lorem: "ipsum", a: "123"}
```
Gets full query as string with all special chars decoded.
```js
getQuery('example.com/?lorem=ipsum'); // "?lorem=ipsum"
```
Add new query string parameter to URL or update existing value.
See: [`getParam()`](
```js
setParam('?foo=bar&lorem=0', 'lorem', 'ipsum'); // '?foo=bar&lorem=ipsum'
setParam('?lorem=1', 'foo', 123); // '?lorem=1&foo=123'
```
-------------------------------------------------------------------------------
For more usage examples check specs inside `/tests` folder. Unit tests are the
best documentation you can get...