Skip to main content

API Reference

Complete API documentation for format-prompt
2 min read

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 literal
  • values - 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:

  1. Leading whitespace: Removed from each line
  2. Trailing whitespace: Removed from each line
  3. Multiple spaces: Collapsed to single spaces within lines
  4. Line breaks: Preserved as single line breaks
  5. 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#

  1. Use for prompts: This function is optimized for formatting AI prompts and similar text
  2. Preserve structure: The function maintains your intended line break structure
  3. Interpolate variables: Use template literal interpolation for dynamic content
  4. Code readability: Write prompts with proper indentation in your code - the function will clean it up

Common Patterns#

Conditional Content#

List Generation#

Nested Templates#