A lightweight lib to work with query strings (1kb uglified)
You can install via npm or via Bower, query-object is also provided as AMD if you use requireJS:
npm install query-object
bower install query-object
Defines the usage of HTML5HistoryAPI for modifier methods. DEFAULT: false
queryObject.useHistory = true;
High-convenience test method to set current context since we can't reload using old location.search during tests. Returns the new context.
fakeContext = {};
fakeContext.location = {};
queryObject.setContext(fakeContext);
// returns {}
Clears the current query string
queryObject.clear();
// returns undefined
Returns the current query string as a key/value object. If an array was provided then an object with the matched keys will be returned, otherwise a string or undefined will be returned.
// ?foo=foo&bar=bar&baz
queryObject.get();
// returns {foo: 'foo', bar: 'bar', baz: undefined}
queryObject.get('foo');
// returns {foo: 'foo'}
queryObject.get(['foo', 'bar']);
// returns {foo: 'foo', bar: 'bar'}
Sets the query string. Returns the new query string if an object was provided, otherwise returns undefined.
queryObject.set({foo: 'foo'});
// returns 'foo=foo';
Adds the provided object to the current query string. Returns the new query string if an object was provided, otherwise returns undefined.
// ?foo=foo
queryObject.add({bar: 'bar'});
// returns 'foo=foo&bar=bar'
Removes the current property or propeties of the current query string. Returns the new query string or undefined if the param isn't provided as expected.
// ?foo=foo&bar=bar&baz
queryObject.remove('baz');
// retuns 'foo=foo&bar=bar'
queryObject.remove(['foo', 'bar']);
// returns ''
Checks for a property on the query string. Returns a boolean based on the existance of the property. If no property is provided, or the property isn't a string, undefined is returned.
// ?foo
queryObject.has('foo');
// returns true
queryObject.has('bar');
// returns false
</tr>
<tr>
<td align="center">9+</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
<td align="center">✓</td>
</tr>