leandrogrillo / query-object

Easy url query string manipulation via Objects (using JavaScript)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

query-object

Build Status

A lightweight lib to work with query strings (1kb uglified)

Install

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

API

useHistory

Defines the usage of HTML5HistoryAPI for modifier methods. DEFAULT: false

queryObject.useHistory = true;

setContext(object)

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 {}

clear()

Clears the current query string

queryObject.clear();
// returns undefined

get(string|array)

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'}

set(object)

Sets the query string. Returns the new query string if an object was provided, otherwise returns undefined.

queryObject.set({foo: 'foo'});
// returns 'foo=foo';

add(object)

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'

remove(string|array)

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 ''

has(string)

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

Browser Support

</tr>
<tr>
  <td align="center">9+</td>
  <td align="center">✓</td>
  <td align="center">✓</td>
  <td align="center">✓</td>
  <td align="center">✓</td>
</tr>

License

MIT License

About

Easy url query string manipulation via Objects (using JavaScript)

License:MIT License


Languages

Language:JavaScript 100.0%