Home Manual Reference Source
import QueryString from 'ievv_jsbase/lib/http/QueryString'
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 a querystring from window.location.search
// window.location.search == "?name=test&age=12"
const querystring = new QueryString(window.location.search);
const name = querystring.get('name');
const age = querystring.get('age');
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

deepCopy(): *

Create a deep copy of this QueryString object.

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

merge(queryStringObjects: *)

Merge QueryString objects into with this object.

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

setSmart(key: string, value: string | number | boolean | array | Set)

Calls QueryString#set or QueryString#setIterable depending on the type of the provided value.

public

Set values from a Map.

public

Set values from an Object.

public

Set values from a querystring, like window.location.search.

public

urlencode(options: Object): *

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 deepCopy(): * source

Create a deep copy of this QueryString object.

Return:

*

The copy.

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 merge(queryStringObjects: *) source

Merge QueryString objects into with this object.

Overwrites any key/value pairs currently in this object with keys in the provided queryStringObjects in provided order, with the last one overwriting any preceding values.

Params:

NameTypeAttributeDescription
queryStringObjects *

Zero or more QueryString objects.

Example:

const querystring = new QueryString('name=oldname');
querystring.merge(
   new QueryString('name=newname1&age=33'),
   new QueryString('name=newname2&size=large'));
// querystring.get('name') == 'newname2'
// querystring.get('age') == '33'
// querystring.get('size') == 'large'

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 setSmart(key: string, value: string | number | boolean | array | Set) source

Calls QueryString#set or QueryString#setIterable depending on the type of the provided value.

Params:

NameTypeAttributeDescription
key string

The key to store the value as.

value string | number | boolean | array | Set

The value to set using QueryString#set or QueryString#setIterable depending on the type.

public setValuesFromMap(map: Map) source

Set values from a Map.

Overwrites any key/value pairs currently in this QueryString with key/value pairs in the provided map.

Uses QueryString#setSmart to set the values, so the values of the map can be both simple types and iterables like arrays and sets.

Params:

NameTypeAttributeDescription
map Map

A map.

Example:

const querystring = new QueryString();
querystring.set('name', 'oldname');
querystring.addValuesFromMap(new Map([
  ['name', 'newname'],
  ['age', 33],
  ['tags', ['tag1', 'tag2']]
]));
// querystring.get('name') == 'newname'
// querystring.get('age') == 33
// querystring.getArray('tags') == ['tag1', 'tag2']

public setValuesFromObject(object: Object) source

Set values from an Object.

Overwrites any key/value pairs currently in this QueryString with key/value pairs in the provided object.

Uses QueryString#setSmart to set the values, so the values of the map can be both simple types and iterables like arrays and sets.

Params:

NameTypeAttributeDescription
object Object

An Object.

Example:

const querystring = new QueryString();
querystring.set('name', 'oldname');
querystring.addValuesFromObject({
  name: 'newname',
  age: 33,
  tags: ['tag1', 'tag2']
});
// querystring.get('name') == 'newname'
// querystring.get('age') == 33
// querystring.getArray('tags') == ['tag1', 'tag2']

public setValuesFromQueryString(querystring: string) source

Set values from a querystring, like window.location.search.

Overwrites any key/value pairs currently in this object with keys in the provided querystring.

Params:

NameTypeAttributeDescription
querystring string

A querystring, like the one in window.location.search. Examples: "?a=10", "a=10", "a=10&s=test".

Example:

const querystring = new QueryString();
querystring.set('name', 'oldname');
querystring.addValuesFromQueryString('name=newname&age=33');
// querystring.get('name') == 'newname'
// querystring.get('age') == '33'

public urlencode(options: Object): * source

Get the QueryString object as a string in query-string format.

Params:

NameTypeAttributeDescription
options Object

Options. All are optional

options.sortKeys boolean

Sort the keys using Array.sort? false by default.

options.sortValues boolean

Sort the values using Array.sort? false by default. This only makes sense if you have keys with multiple values.

options.skipEmptyValues boolean

Skip empty values? false by default.

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
Sort keys
const querystring = QueryString();
querystring.set('name', 'john');
querystring.set('age', 33);
let urlEncodedQuerystring = querystring.urlencode({sortKeys: true});
// urlEncodedQuerystring === 'age=33&name=john'
Sort values
const querystring = QueryString();
querystring.setIterable('name', ['john', 'amy', 'xion']);
let urlEncodedQuerystring = querystring.urlencode();
// urlEncodedQuerystring === 'name=amy&name=john&name=xion'