QueryParameters()

A class that represents and can manipulate a parsed query string as a subclass of Array.

The values in the Array are objects.

The values in the Array are objects with 'key', 'escapedKey' 'value' and 'escapedValue'. The 'key' is decoded (from %-encoding) and lowercased. The 'escapedKey' is as it originally occurred in the query string. It's not decoded or lowercased. The 'escapedValue' is as it originally occurred in the query string. It's not decoded. The 'value' is decoded (from %-encoding).

If the query parameter is 'key=value', all fields are set. If the query parameter is 'key=' then 'value' and 'escapedValue' will be the empty string. If the query parameter is just 'key' then 'value' and 'escapedValue' will be null. If the query parameter is empty (double-&) then 'key' and 'escapedKey' will be '', 'value' and 'escapedValue' will be null.

It's important that we PRESERVE the order of the parameters because some projects rely on it. The 'querystring' module assigns values to the resulting object in the order that it encounters them in the input string. Since ES2015 there are specific rules defining the order of properties, and apart from one particular case the order is chronological, which is what we want. The order of properties in an object depends on the type of the included properties and their values. Unfortunately, integer indices always come first, and the rules state that "an integer index is a string that, if converted to a 53-bit non-negative integer and back is the same value". This means that any parameter whose key is an integer index will appear out of order in the result. Thus we can't use the 'querystring' module.

Constructor

new QueryParameters()

Members

keys

An Array of all the keys (as lowercased, unescaped strings)

parameters

Access the array of parameters owned by this QueryParameters object.

Methods

(static) from() → {QueryParameters}

Construct a new QueryParameters from the output of any filtering or mapping done on another QueryParameters instance's 'parameters' array.

Returns:
Type
QueryParameters

appendParameter(key, value)

Add a query parameter to the array (at the end)

Parameters:
Name Type Default Description
key String

Parameter name (key)

value String null

Parameter value (may be omitted or set to null to store only a key).

deleteByKey(key) → {Boolean}

Delete any query parameters with the given key. Key names are matched in a case-insensitive way.

Parameters:
Name Type Description
key String
Returns:

true if a parameter was deleted, false if not.

Type
Boolean

toString() → {string}

Return a querystring built from the parameters in this instance, using the escapedKey and escapedValue properties of each parameter.

Any strings that this class escapes will be correctly encoded (using %20 for space). There is an issue with input querystrings that are supposed to be encoded correctly, but which contain space characters (in either the key or the value). We don't want to convert them on input because that might cause the querystring as represented by this class to differ from the actual input. Instead, we define the behaviour of this function such that it will rebuild the querystring exactly as input EXCEPT that any spaces will be +-encoded.

Returns:
Type
string