Utility Functions
A collection of general-purpose utilities. These can be imported directly form the tabris
module.
Methods
Set(target, attributes)
Creates a attributes object for the given widget type. This is meant to be used to creates rule sets for the apply
method. The benefit of using this method as opposed to passing the object directly is twofold:
1 - The IDE and/or TypeScript compiler can check that the attributes are matching the given widget type.
2 - The apply
method will only set the attributes if the selected widget matches the type given here. If there is a mismatch an error will be thrown.
Parameter | Type | Description |
---|---|---|
target | WidgetConstructor<WidgetType> |
The type of the target widget. |
attributes | Attributes<WidgetType> |
A set of attributes (properties and listeners) for the given widget type. |
Returns: Attributes<WidgetType>
asFactory(constructor)
Wraps the given widget constructor so that it can also be called like a normal function (factory) without the “new” keyword. When used like this the first parameter may not only contain all settable properties, but also listener (e.g. {onTap: ev => console.log(ev)}
and children ({children: [...]}
). A second parameter may be given to the factory, which should be a functional component. It will not be invoked, but becomes usable as a selector to obtain the instance created by this factory call.
All built-in widgets are already callable like this, so this function is only useful for user-defined widget subclasses/custom components.
In TypeScript the wrapped constructor will not have any static members declared, though they are still available at runtime.
Parameter | Type | Description |
---|---|---|
constructor | OriginalConstructor |
Returns: CallableConstructor<OriginalConstructor>
checkType(value, type, options?)
Checks that the given value is of the expected type. If the check fails a TypeError
will be thrown. The name
option may be used to change how the error message refers to the value.
If the given value is of the expected type the function will simply return the value. In TypeScript files and some JavaScript editors the IDE will recognize the return value as being of the expected type.
Some values will never pass the check regardless of given type: NaN
, Infinity
, -Infinity
and any boxed primitive. The values null
and undefined
will pass only if the nullable
option is explicitly set to true.
Parameter | Type | Description |
---|---|---|
value | any |
The value to check the type of. |
type | Constructor<T> |
The constructor function (class) of the expected type. May also be a subclass. Primitive types are represented by their respective boxed type constructors String , Number and Boolean . |
options | { |
Optional. |
Returns: T
checkType(value, type, callback)
Checks that the given value is of the expected type. If the check fails a TypeError
will be thrown. If the value is of the expected type the given callback will be called (synchronously) with the checked value as the only argument. In TypeScript files and some JavaScript editors the IDE will recognize that argument being of the expected type.
Some values will never pass the check regardless of given type: NaN
, Infinity
, -Infinity
, boxed primitives, null
and undefined
.
Parameter | Type | Description |
---|---|---|
value | any |
The value to check the type of. |
type | Constructor<T> |
The constructor function (class) of the expected type. May also be a subclass. Primitive types are represented by their respective boxed type constructors String , Number and Boolean . |
callback | (value) => any |
A callback called with the value if it is of the expected type. |
Returns: undefined
format(…data)
Formats the given value(s) in the same manner the console does.
Parameter | Type | Description |
---|---|---|
…data | any[] |
Returns: string
format(message, …data)
Formats the given value(s) in the same manner the console does. The any placeholders in the message are replaced by the additional data parameters. Valid placeholders are “%d” for decimals, “%i” for integers, “%f” for floats, “%j” for any number, “%j” for any objects (including arrays) and “%s” for strings. To print the percentage sign itself use “%%”.
Parameter | Type | Description |
---|---|---|
message | string |
The main message. |
…data | any[] |
Data to be inserted in to the main message. |
Returns: string