new Utils()
This class contains some useful utility methods.
The Utils class cannot be instantiated (its constructor will throw if called), all its methods are static.
- Source:
Methods
(static) appendQueryParameter(uri, key, value) → {String}
Appends a query parameter to an existing URL, taking care of existing query parameters.
This method returns uri
as-is if key
or value
is not defined.
Parameters:
Name | Type | Description |
---|---|---|
uri |
String | The URI to modify. |
key |
String | The name of the parameter to append. |
value |
String | The value of the parameter to append. |
- Source:
Returns:
The modified URI.
- Type
- String
(static) assertRequiredParameterHasType(parameter, name, type) → {*}
Asserts that the given parameter
is present and has the expected type.
This method is intended to be called when you need to validate input parameters of functions.
Examples:
assertRequiredParameterHasType(5, 'name', 'number') => returns 5
assertRequiredParameterHasType({}, 'name', CustomClass) => throws an Error
Parameters:
Name | Type | Description |
---|---|---|
parameter |
* | The parameter to validate. |
name |
String | The name of the parameter, as given to the calling function. This is used to format the error message contained by the thrown Error. |
type |
String | Type | The expected type of the parameter. |
- Source:
Throws:
-
If the parameter is not defined or is not an object.
- Type
- Error
Returns:
The validated parameter, as-is.
- Type
- *
(static) assertRequiredParameterIsArrayWithMinimumLength(parameter, name, lengthopt) → {*}
Asserts that the given parameter
is an Array with a minimum length.
This method is intended to be called when you need to validate input parameters of functions.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
parameter |
* | The parameter to validate. |
||
name |
String | The name of the parameter, as given to the calling function. This is used to format the error message contained by the thrown Error. |
||
length |
Number |
<optional> |
0 | The minimum required length of the array. |
- Source:
Throws:
-
If the parameter is not an array of does not have the minimum length.
- Type
- Error
Returns:
The validated parameter, as-is.
- Type
- *
(static) assertRequiredParameterIsObject(parameter, name) → {*}
Asserts that the given parameter
is present and is an object.
This method is intended to be called when you need to validate input parameters of functions.
Parameters:
Name | Type | Description |
---|---|---|
parameter |
* | The parameter to validate. |
name |
String | The name of the parameter, as given to the calling function. This is used to format the error message contained by the thrown Error. |
- Source:
Throws:
-
If the parameter is not defined or is not an object.
- Type
- Error
Returns:
The validated parameter, as-is.
- Type
- *
(static) assertRequiredParameterIsPresent(parameter, name) → {*}
Asserts that the given parameter
is present (read: truthy).
This method is intended to be called when you need to validate input parameters of functions.
Parameters:
Name | Type | Description |
---|---|---|
parameter |
* | The parameter to validate. |
name |
String | The name of the parameter, as given to the calling function. This is used to format the error message contained by the thrown Error. |
- Source:
Throws:
-
If the parameter is not defined.
- Type
- Error
Returns:
The validated parameter, as-is.
- Type
- *
(static) assertValidJMAPResponse(request, data) → {*}
Asserts that the given data
is a valid JMAP response.
This method is intended to be called by instances of Client, or by any other object making JMAP requests,
when validation of the response is required.
The following checks are made by this method:
data
is defined and is an arraydata
has one or more elements, and all elements are arraysdata[0][0]
is either- the expected response string (computed with the help of the
request
parameter) - 'error'
- an unknown response
- the expected response string (computed with the help of the
data[0][1]
exists
Parameters:
Name | Type | Description |
---|---|---|
request |
String | The JMAP request to check the response for. This should be a valid JMAP request name. |
data |
* | The JMAP response to validate. |
- Source:
Throws:
-
If the received data is not a valid JMAP response.
- Type
- Error
Returns:
The data, as-is, if it is detected as a valid JMAP response.
- Type
- *
(static) capitalize(str) → {String}
Capitalizes the given String, that is, returns the same string with the first character in uppercase.
If undefined
, null
, the empty string or something else that a string is given, the argument is returned as-is.
Parameters:
Name | Type | Description |
---|---|---|
str |
String | The String to capitalize. |
- Source:
Returns:
The capitalized String.
- Type
- String
(static) fillURITemplate(uri, parameters) → {String}
Fills a URI template by substituting variables by their corresponding values.
This supports Level 1 URI templates only, as per this RFC.
Parameters:
Name | Type | Description |
---|---|---|
uri |
String | The URI template to fill. |
parameters |
Object | A hash of name/value pairs used for variables substitution. |
- Source:
Returns:
The filled URI template.
- Type
- String
(static) isDefined(parameter) → {Boolean}
Check is the parameter
is not undefined and not null.
Parameters:
Name | Type | Description |
---|---|---|
parameter |
* | The parameter to check. |
- Source:
Returns:
True if parameter
is not undefined and not null.
- Type
- Boolean
(static) nthElementOrDefault(array, index, defaultValue) → {*}
Returns the value at index in the given Array
, or the default value if the array is undefined, null,
if index is negative or there is not enough elements in the array.
Parameters:
Name | Type | Description |
---|---|---|
array |
The |
|
index |
The index of the desired value. |
|
defaultValue |
The default value to return if the element cannot be found in the array. |
- Source:
Returns:
The found value or the given default.
- Type
- *