Home Manual Reference Source
import QueryString from 'ievv_jsbase/http/QueryString.js'
public class | source

QueryString

Query-string creator and parser.

Example:

Basics - build a querystring
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
Parse a querystring
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');
Parse and modify a querystring
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(key: string, value: string)

Append a value to a key.

public

clear()

Remove all keys and values from the QueryString.

public

get(key: string, fallback: string): *

Get a value.

public

getArray(key: string, fallback: Array): Array

Get the values for the specified key as an array.

public

has(key: string): boolean

Check if the QueryString contains the given key.

public

Returns true if the querystring is empty, otherwise false.

public

remove(key: string)

Remove the specified key from the QueryString.

public

set(key: string, value: string)

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 constructor(querystring: string) source

Params:

NameTypeAttributeDescription
querystring string

Optional input querystring to parse.

Public Methods

public append(key: string, value: string) source

Append a value to a key.

Params:

NameTypeAttributeDescription
key string

The key to append a value to.

value string

The value to append.

Example:

const querystring = QueryString();
querystring.append('names', 'Jane');
querystring.append('names', 'Joe');
// querystring.urlencode() === 'names=Jane&names=Joe'

public clear() source

Remove all keys and values from the QueryString.

public get(key: string, fallback: string): * source

Get a value.

Params:

NameTypeAttributeDescription
key string

The key to get the value for.

fallback string

An optional fallback value if the key is not in the QueryString. Defaults to undefined.

Return:

*

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.

Params:

NameTypeAttributeDescription
key string

The key to get the values for.

fallback Array

An optional fallback value if they key is not in the QueryString. Defaults to an empty array.

Return:

Array

public has(key: string): boolean source

Check if the QueryString contains the given key.

Params:

NameTypeAttributeDescription
key string

The key to check for.

Return:

boolean

public isEmpty(): boolean source

Returns true if the querystring is empty, otherwise false.

Return:

boolean

public remove(key: string) source

Remove the specified key from the QueryString.

Params:

NameTypeAttributeDescription
key string

The key to remove.

public set(key: string, value: string) source

Set a value.

Params:

NameTypeAttributeDescription
key string

The key to store the value as.

value string

The value to set.

Example:

const querystring = QueryString();
querystring.set('name', 'Peter');

public setIterable(key: string, iterable: *) source

Set value from an iterable.

Params:

NameTypeAttributeDescription
key string

The key to set.

iterable *

Something that can be iterated with a for(const value of iterable) loop. All the values in the iterable must be strings. If the iterable is empty the key will be removed from the QueryString.

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