Utils
Methods
blockProvideEvent(blockProps:action:eventType:)
Provides an event handler for a specific event type if the block supports it.
public func blockProvideEvent(
blockProps: BlockProps,
action: [NativeActionModel],
eventType: String
) -> (() -> Void)?
Parameters
- blockProps: The properties of the block, including its event handling logic.
- action: An array of NativeActionModel instances representing the actions associated with the block.
- eventType: The type of event to check and provide a handler for.
Returns
A closure representing the event handler if the event type is supported; otherwise, nil.
blockProvideSlot(blockProps:slots:slotType:)
Provides the NativeBlockSlotModel for the specified slot type if the block supports it.
public func blockProvideSlot(
blockProps: BlockProps,
slots: [String: NativeBlockSlotModel],
slotType: String
) -> NativeBlockSlotModel?
Parameters
- blockProps: The properties of the block, including its sub-blocks.
- slots: A dictionary of slot identifiers to their corresponding NativeBlockSlotModel instances.
- slotType: The type of slot to check and provide.
Returns
The NativeBlockSlotModel for the specified slot type if the block supports it; otherwise, nil.
findWindowSizeClass(_:_:_:)
Determines the appropriate value for a property based on the device's size class.
public func findWindowSizeClass(
_ vertical: UserInterfaceSizeClass?,
_ horizontal: UserInterfaceSizeClass?,
_ prop: NativeBlockPropertyModel?
) -> String?
Parameters
- vertical: The vertical size class of the device (e.g., .compact or .regular).
- horizontal: The horizontal size class of the device (e.g., .compact or .regular).
- prop: The NativeBlockPropertyModel containing values for mobile, tablet, and desktop.
Returns
The value corresponding to the device's size class (mobile, tablet, or desktop), or nil if no value is found.
blockHasSlot(blocks:slot:)
Checks if a block contains a specific slot.
public func blockHasSlot(
blocks: [String: NativeBlockModel],
slot: String
) -> Bool
Parameters
- blocks: A dictionary of block identifiers to their corresponding NativeBlockModel instances.
- slot: The slot identifier to check for in the blocks.
Returns
true if the slot is found in any block; otherwise, false.
blockHasEvent(action:eventType:)
Checks if a block supports a specific event type.
public func blockHasEvent(
action: [NativeActionModel],
eventType: String
) -> Bool
Parameters
- action: An array of NativeActionModel instances representing the actions associated with the block.
- eventType: The type of event to check for.
Returns
true if the event type is supported; otherwise, false.
blockHandleEvent(blockProps:action:eventType:)
Handles an event for a block by invoking the appropriate action handler.
public func blockHandleEvent(
blockProps: BlockProps,
action: [NativeActionModel],
eventType: String
)
Parameters
- blockProps: The properties of the block, including its event handling logic.
- action: An array of NativeActionModel instances representing the actions associated with the block.
- eventType: The type of event to handle.
blockHandleVariableValue(blockProps:value:)
Processes and evaluates a string value within the context of a block.
public func blockHandleVariableValue(blockProps: BlockProps, value: String?) -> String?
Example Usage:
// Using JSON path
let jsonValue = "data$[{index}]"
let result1 = blockHandleVariableValue(blockProps: props, value: jsonValue)
// Using localization
let localValue = "{local:greeting}"
let result2 = blockHandleVariableValue(blockProps: props, value: localValue)
// Using variable reference
let varValue = "{var:userName}"
let result3 = blockHandleVariableValue(blockProps: props, value: varValue)
Parameters
- blockProps: The properties associated with the block being rendered.
- value: The string value to be processed, which may contain variable references, localizations, or JSON paths.
Returns
The processed string value, or nil if the input value is invalid.
blockHandleVariableValue(blockProps:variable:)
Processes and evaluates a variable value within the context of a block.
public func blockHandleVariableValue(blockProps: BlockProps, variable: NativeVariableModel?) -> String?
Example Usage:
let variable = NativeVariableModel(value: "{local:welcome_message}")
let result = blockHandleVariableValue(blockProps: props, variable: variable)
print(result) // Output: "Welcome!" (if localized value is "Welcome!")
Parameters
- blockProps: The properties associated with the block being rendered.
- variable: The native variable model to be processed.
Returns
The processed string value of the variable, or nil if the variable is invalid.
actionHandleVariableValue(actionProps:value:)
Processes and evaluates a string value within the context of an action.
public func actionHandleVariableValue(actionProps: ActionProps, value: String?) -> String?
Example Usage:
let value = "data$[{index}]"
let result = actionHandleVariableValue(actionProps: props, value: value)
print(result) // Output: The value at the specified index in the data array
Parameters
- actionProps: The properties associated with the action being executed.
- value: The string value to be processed, which may contain variable references or JSON paths.
Returns
The processed string value, or nil if the input value is invalid.
actionHandleVariableValue(actionProps:variable:)
Processes and evaluates a variable value within the context of an action.
public func actionHandleVariableValue(actionProps: ActionProps, variable: NativeVariableModel?) -> String?
Example Usage:
let variable = NativeVariableModel(value: "{var:username}")
let result = actionHandleVariableValue(actionProps: props, variable: variable)
print(result) // Output: "John" (if username variable is "John")
Parameters
- actionProps: The properties associated with the action being executed.
- variable: The native variable model to be processed.
Returns
The processed string value of the variable, or nil if the variable is invalid.
Extensions on String
toCGFloat()
Converts a string representation of a number to a CGFloat.
public func toCGFloat() -> CGFloat?
Returns
A CGFloat if the string can be converted to a number; otherwise, nil.
evaluateMixConditionOperator(type:)
Evaluates a mixed expression that may contain both conditions and arithmetic operators.
public func evaluateMixConditionOperator(type: String) -> String
Example Usage:
let expression1 = "(4 / 2 != 0) && (true == true)"
let result1 = expression1.evaluateMixConditionOperator(type: "BOOLEAN")
print(result1) // Output: "true"
let expression2 = "3 + 1"
let result2 = expression2.evaluateMixConditionOperator(type: "BOOLEAN")
print(result2) // Output: "3+1"
let expression3 = "(4 / 2 != 0) && (true == true)"
let result3 = expression3.evaluateMixConditionOperator(type: "INT")
print(result3) // Output: "(4 / 2 != 0) && (true == true)"
let expression4 = "3 + 1"
let result4 = expression4.evaluateMixConditionOperator(type: "INT")
print(result4) // Output: "4.0"
let expression5 = "(4 / 2 != 0) && (true == true)"
let result5 = expression5.evaluateMixConditionOperator(type: "STRING")
print(result5) // Output: "(4 / 2 != 0) && (true == true)"
let expression6 = "3 + 1"
let result6 = expression6.evaluateMixConditionOperator(type: "STRING")
print(result6) // Output: "3+1"
Parameters
- type: The type of the result to return (BOOLEAN, INT, DOUBLE, LONG, FLOAT).
Returns
The evaluated result as a string based on the specified type.
extractKeyVariables(key:)
Extracts key variables from a string based on a specific key pattern.
public func extractKeyVariables(key: String) -> [(String, String?)]
Example Usage:
let path = "data$[{index:1}]"
let variables = path.extractKeyVariables(key: "index")
print(variables) // Output: [("index", Optional("1"))]
Parameters
- key: The key to search for within the string (e.g., "index").
Returns
An array of tuples where each tuple contains the extracted key and its optional value.
getVariableValue(key:replacement:)
Replaces a variable in the string with its value.
public func getVariableValue(key: String, replacement: String) -> String
Example Usage:
let template = "Hello, {name}!"
let result = template.getVariableValue(key: "name", replacement: "John")
print(result) // Output: Hello, John!
Parameters
- key: The variable key.
- replacement: The replacement value for the key.
Returns
A new string with the variable replaced.
extractKeyVariables()
Extracts key-value pairs from a string containing placeholders in the format {key} or {key:value}.
public func extractKeyVariables() -> [(String, String?)]
Returns
An array of tuples where the first element is the key and the second element is the value (or nil if absent).
replaceNativeVariable(_:)
Replaces native variable placeholders with corresponding values from the variables dictionary.
public func replaceNativeVariable(_ variables: [String: NativeVariableModel]?) -> String