Main Function#
prompt()#
The main function exported by format-prompt. It's a template tag function that formats template literal strings.
Parameters:
strings- The template strings array from a template literalvalues- The interpolated values from the template literal
Returns: A formatted string with cleaned whitespace
Example:
Behavior#
Whitespace Handling#
The prompt function processes whitespace in the following way:
- Leading whitespace: Removed from each line
- Trailing whitespace: Removed from each line
- Multiple spaces: Collapsed to single spaces within lines
- Line breaks: Preserved as single line breaks
- Final trim: The entire result is trimmed of leading/trailing whitespace
Examples#
Basic Formatting#
Multi-line Formatting#
Preserving Structure#
Interpolation#
The function supports standard template literal interpolation:
Interpolated values are converted to strings using String():
Type Definitions#
Function Signature#
TemplateStringsArray#
This is a built-in TypeScript type representing the strings in a template literal:
Use Cases#
AI Prompt Formatting#
Email Template#
Code Generation#
Performance#
The prompt function is designed to be lightweight and efficient:
- Uses native string operations
- Minimal overhead for template literal processing
- No external dependencies for the core formatting logic
- Small bundle size (~1KB minified + gzipped)
Best Practices#
- Use for prompts: This function is optimized for formatting AI prompts and similar text
- Preserve structure: The function maintains your intended line break structure
- Interpolate variables: Use template literal interpolation for dynamic content
- Code readability: Write prompts with proper indentation in your code - the function will clean it up