Package Exports
- @zakkudo/query-string
- @zakkudo/query-string/QueryStringError
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@zakkudo/query-string) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
QueryString
Make working with url query-strings enjoyable.
Why use this?
- Consistancy with simplicity
- The instance acts like a plain-old-object
JSON.stringify()
will serialize it to json like it was a normal object- Casting to a string will format it to be directly usable in a query Update the properties after initialization and the serialization will reflect the updates
- Complex params are automatically serialized and deserialized from json
Install with:
yarn add @zakkudo/query-string
Throws:
QueryStringError
On issues during serialization or construction
Example (Initializing with an object)
import QueryString from '@zakkudo/query-string';
const query = new QueryString({
page: 3,
title: 'awesomeness',
complex: {'test': 'value'}
});
String(query) // '?page=3&title=awesomeness&complex=%7B%22test%22%3A%22value%22%7D&'
query.toString() // '?page=3&title=awesomeness&complex=%7B%22test%22%3A%22value%22%7D&'
const url = `http://example${query}` //Automatically serializes correctly
Example (Initializing with a URL)
import QueryString from '@zakkudo/query-string';
const query = new QueryString('http://example?page=3&title=awesomeness');
delete query.page;
String(query) // '?title=awesomeness'
query.toString() // '?title=awesomeness'
Example (Parsing an invalid query string with duplicate ?)
import QueryString from '@zakkudo/query-string';
import QueryStringError from '@zakkudo/query-string/QueryStringError';
try {
const query = new QueryString('http://invalid.com/?first=1?second=2')
} catch(e) {
if (e instanceof QueryStringError) {
console.error(e.message); // Trying to add duplicate query param when already exists
} else {
throw e;
}
}
module.exports ⏏
new module.exports(data)
Param | Type | Description |
---|---|---|
data | String | Object | QueryString |
Initial data. A url String will be parsed, and Object /QueryString instances will be copied. |
module.exports.toString() ⇒ String
Converts the object into its serialized query string representation that can be used in a url.
Kind: instance method of module.exports
Returns: String
- The serialized representation of the QueryString
. It
will be an empty string if there are no params to serialize.