Function adjusted

  • Formats a multi-line template literal by adjusting indentation and preserving line breaks.

    This function ensures consistent formatting of multi-line strings, which is particularly useful for defining code blocks or text outputs. It uses Platform newlines (e.g. \r\n on Windows).

    The indentation of dynamically inserted placeholders (${...}) will be adjusted to match the indentation level of the placeholder itself. This feature is especially useful for code generation scenarios, where nested structures or varying indentation levels are needed.

    const text = adjusted`
    ${cmds}
    if (sayAgain) {
    ${cmds}
    }
    `;

    // Output (without `|`):
    // |console.log('Hello,');
    // |console.log('World!');
    // |if (sayAgain) {
    // | console.log('Hello,');
    // | console.log('World!');
    // |}

    Parameters

    • staticParts: TemplateStringsArray

      The static (literal) parts of the template string.

    • ...substitutions: unknown[]

      The dynamic parts of the template string, which will be injected into the corresponding placeholders in staticParts.

    Returns string

    A formatted string with fixed indentation and Unix-style line breaks (\n), without carriage return characters (\r).