QueryString
Query-string creator and parser.
Example:
const querystring = new QueryString();
querystring.set('name', 'Peter');
querystring.setIterable('tags', ['person', 'male']);
const encodedQuerystring = querystring.urlencode();
// encodedQuerystring === 'name=Peter&tags=person&tags=male' // order may vary
const querystring = new QueryString('name=Peter&tags=person&tags=male');
const name = querystring.get('name');
const tags = querystring.getArray('tags');
const firstTag = querystring.get('tags');
const querystring = new QueryString('name=Peter&tags=person&tags=male');
querystring.set('name', 'John');
querystring.append('tags', 'important');
// querystring.urlencode() === 'name=John&tags=person&tags=male&tags=important'
querystring.setIterable('tags', ['male']);
// querystring.urlencode() === 'name=John&tags=male'
Constructor Summary
Public Constructor | ||
public |
constructor(querystring: string) |
Method Summary
Public Methods | ||
public |
Append a value to a key. |
|
public |
clear() Remove all keys and values from the QueryString. |
|
public |
Get a value. |
|
public |
Get the values for the specified key as an array. |
|
public |
Check if the QueryString contains the given key. |
|
public |
Returns |
|
public |
Remove the specified key from the QueryString. |
|
public |
Set a value. |
|
public |
setIterable(key: string, iterable: *) Set value from an iterable. |
|
public |
urlencode(): * Get the QueryString object as a string in query-string format. |
Public Constructors
Public Methods
public append(key: string, value: string) source
Append a value to a key.
Example:
const querystring = QueryString();
querystring.append('names', 'Jane');
querystring.append('names', 'Joe');
// querystring.urlencode() === 'names=Jane&names=Joe'
public getArray(key: string, fallback: Array): Array source
Get the values for the specified key as an array.
Always returns an array, even if the value was set with QueryString#set.
public has(key: string): boolean source
Check if the QueryString contains the given key.
Params:
Name | Type | Attribute | Description |
key | string | The key to check for. |
public remove(key: string) source
Remove the specified key from the QueryString.
Params:
Name | Type | Attribute | Description |
key | string | The key to remove. |
public set(key: string, value: string) source
Set a value.
Example:
const querystring = QueryString();
querystring.set('name', 'Peter');
public setIterable(key: string, iterable: *) source
Set value from an iterable.
Params:
Name | Type | Attribute | Description |
key | string | The key to set. |
|
iterable | * | Something that can be iterated with a
|
Example:
const querystring = QueryString();
querystring.setIterable('names', ['Peter', 'Jane']);
public urlencode(): * source
Get the QueryString object as a string in query-string format.
Return:
* |
Example:
const querystring = QueryString();
querystring.set('next', '/a&b/');
querystring.set('name', 'john');
let urlEncodedQuerystring = querystring.urlencode();
// urlEncodedQuerystring === 'name=john&next=%2Fa%26b%2F' // order may vary