query-string-handler

3.1.1 • Public • Published

query-string-handler

This turns the query string into a private object. Use the API to read and manipulate the query string.

API

.update()

// only call this if an outside script updates the query string
queryStringHandler.update();

.setDefaults(obj)

@param obj {Object}

// When values equal the default value, they are ignored in the "toString"
queryStringHandler.setDefaults({ name: 'John Smith' });

.setTypes(obj)

@param obj {Object}

// Setting the type converts the query string value to that type
queryStringHandler.setTypes({ num: 'float' });

.getValue(name)

@param name {String}
@return {String}

// get a value from the query string
var val = queryStringHandler.getValue('name');

.toString([encoded[, params]])

@param encoded {Boolean}
@param params {Object}
@return {String}

// get the query string in string format
// optionally encode the ampersands with the first parameter
// optionally temporary extend the query string with the second parameter
// if all parameters are blank, it will return location.pathname
var url = queryStringHandler.toString(true, { beep: 'bop' });
console.log(url); // '?beep=bop'

.push(changes[, title])

@param changes {Object}
@param title {String}

// push a new state with changes to the query string
// optionally change the browser title
// emits an event with type 'pop'
queryStringHandler.push({ foo: 'bar' });

.replace(changes[, title])

@param changes {Object}
@param title {String}

// replace the state with changes to the query string
// optionally change the browser title
// emits an event with type 'replace'
queryStringHandler.replace({ foo: 'bar' });

.addListener(callback)

@param callback {Function}

// add a callback function to the stack
// all callbacks will be called on any event
// the first parameter in the callback will be the event type
queryStringHandler.addListener(function (type) {
    console.log(type);
});
history.back(); // 'pop'

.clone([obj])

@param obj {Object}
@return {Object}

// clone the private query string object
// optionally pass in an object to extend the query string
var clone = queryStringHandler.clone({ beep: 'boop' });

.clear(method = 'push'[, title])

@param method {String} replace|push
@param title {String}

// clone the private query string object
// optionally pass in an object to extend the query string
queryStringHandler.clear();

Install

npm install query-string-handler

Tests

Tests are written in qUnit. Open /example/index.html in a browser.

ES5 methods to polyfill

  • Object.keys
  • Array.forEach
  • Array.map
  • Array.filter

License

MIT © The C2 Group

Package Sidebar

Install

npm i query-string-handler

Weekly Downloads

0

Version

3.1.1

License

MIT

Unpacked Size

49.7 kB

Total Files

16

Last publish

Collaborators

  • thec2group