Steps Advanced
Utility Functions
Learn about the built-in utility functions available in code steps for validation, parsing, file handling, and formatting
Assert Function
The assert function validates conditions and throws errors when assertions fail. It’s used for runtime validation and error handling.
Signature
Parameters
expected
: Boolean condition to evaluateerrorMessage
: Error message to display if assertion fails
Return Value
- Returns:
void
- Throws:
Error
with provided message if assertion fails
Usage Examples
CODE_STEP
Best Practices
- Use descriptive error messages
- Check critical conditions early
- Chain multiple assertions for complex validation
Common Patterns
CODE_STEP
Parse Function
Parses text that may contain JSON into JavaScript objects. Extracts and parses JSON-like content from text, returning the original input if parsing fails.
Signature
Parameters
text
: Text to parse (can be any type)- If text is not a string, returns the original input unchanged
- If text is a string, attempts to extract JSON-like content (objects/arrays) before parsing
Return Value
- Returns: Parsed JavaScript object if valid JSON is found and parsed successfully
- Returns: Original input if input is not a string
- Returns: Original input if no valid JSON content is found
- Throws: JSON.parse errors if invalid JSON syntax is encountered
Usage Examples
CODE_STEP
Best Practices
- Always validate the parsed result matches expected type/structure
- Handle parsing errors with try/catch if needed
- Be aware that embedded JSON will be extracted and parsed
Common Patterns
CODE_STEP
File Operations
For information about file operations, see the Accessing File Contents and Images documentation.
Number Formatting
Formats numbers using locale-specific formatting rules. Supports various number formats and styles.
formatNumber
Signature
Parameters
number
: Number to formatoptions
: Optional Intl.NumberFormatOptions object
Return Value
- Returns: Formatted number string
Usage Examples
CODE_STEP
Best Practices
- Specify precision requirements
- Consider locale implications
- Handle negative numbers appropriately
- Use consistent formatting options
formatCurrency
Signature
Description
Formats numbers as currency values. Defaults to USD if no currency specified.
Parameters
number
: Number to format as currencyoptions
: Optional Intl.NumberFormatOptions object
Return Value
- Returns: Formatted currency string
Usage Examples
CODE_STEP
Best Practices
- Specify currency code when not USD
- Consider currency-specific conventions
- Handle zero and negative amounts properly
- Use consistent currency formatting
Was this page helpful?