Skip to content

Commit 6831731

Browse files
Copilothckhanh
andauthored
Add JSDoc documentation for JSR.io (#5)
* Initial plan * Add JSDoc documentation for formatString and prompt functions Co-authored-by: hckhanh <6380436+hckhanh@users.noreply.github.com> * Add changeset for JSDoc documentation update Co-authored-by: hckhanh <6380436+hckhanh@users.noreply.github.com> * Fix JSDoc documentation accuracy based on code review feedback Co-authored-by: hckhanh <6380436+hckhanh@users.noreply.github.com> * Fix missing bundle files --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: hckhanh <6380436+hckhanh@users.noreply.github.com> Co-authored-by: Khánh Hoàng <hi@khanh.id>
1 parent ae2167c commit 6831731

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

.changeset/tasty-owls-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"format-prompt": patch
3+
---
4+
5+
Add comprehensive JSDoc documentation to formatString and prompt functions for better documentation in JSR.io

.changeset/thirty-meals-feel.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/thirty-shrimps-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"format-prompt": patch
3+
---
4+
5+
Fix missing bundle files

src/index.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@ import { collapse } from 'string-collapse-white-space'
44
const startRegex = /^([\s\n\r]+)/
55
const endRegex = /([\s\n\r]+)$/
66

7+
/**
8+
* Formats a string by collapsing internal whitespace while preserving leading and trailing whitespace.
9+
*
10+
* This function processes a string to remove excessive whitespace and empty lines, making it
11+
* more compact for token-efficient prompts. It preserves the string's leading and trailing
12+
* whitespace to maintain the original boundaries, but optimizes the internal content.
13+
*
14+
* The function performs the following operations:
15+
* - Preserves leading and trailing whitespace (spaces, newlines, carriage returns)
16+
* - Trims leading/trailing whitespace from individual lines
17+
* - Limits consecutive empty lines to a maximum of 1
18+
* - Collapses consecutive whitespace characters to a maximum of 2
19+
*
20+
* @param s - The string to format
21+
* @returns The formatted string with optimized whitespace
22+
*
23+
* @example
24+
* ```ts
25+
* // Collapses multiple spaces
26+
* formatString("Hello world") // "Hello world"
27+
*
28+
* // Preserves leading/trailing whitespace
29+
* formatString(" Hello ") // " Hello "
30+
*
31+
* // Limits excessive consecutive empty lines
32+
* formatString("Line 1\n\n\n\nLine 2") // "Line 1\n\nLine 2"
33+
* ```
34+
*/
735
function formatString(s: string): string {
836
const start = startRegex.exec(s)?.[0] ?? ''
937
const end = endRegex.exec(s)?.[0] ?? ''
@@ -17,6 +45,54 @@ function formatString(s: string): string {
1745
return collWhitespace(start + formatted + end, 2)
1846
}
1947

48+
/**
49+
* A template literal tag function that formats prompts for cleaner presentation and optimal token usage.
50+
*
51+
* This function processes template literals to remove unnecessary whitespace and line breaks while
52+
* preserving the intended structure of your prompts. It's particularly useful for AI/LLM prompts
53+
* where you want readable, well-formatted code but need compact, token-efficient output.
54+
*
55+
* The function automatically:
56+
* - Collapses multiple spaces into single spaces
57+
* - Removes excessive empty lines (limiting to maximum 1 consecutive empty line)
58+
* - Trims leading whitespace from each line (removing indentation)
59+
* - Preserves intentional line breaks and list structures
60+
* - Handles template interpolations correctly
61+
* - Trims the final output
62+
*
63+
* @param strings - The template literal string parts
64+
* @param values - The interpolated values in the template literal
65+
* @returns The formatted prompt string with optimized whitespace
66+
*
67+
* @example
68+
* ```ts
69+
* import { prompt } from 'format-prompt'
70+
*
71+
* // Basic usage with multiline string
72+
* const formatted = prompt`
73+
* Hello world
74+
* This is a prompt
75+
* `
76+
* console.log(formatted) // "Hello world\nThis is a prompt"
77+
*
78+
* // With interpolation
79+
* const name = "John"
80+
* const age = 30
81+
* const result = prompt`
82+
* Name: ${name}
83+
* Age: ${age}
84+
* `
85+
* console.log(result) // "Name: John\nAge: 30"
86+
*
87+
* // Complex prompt with lists
88+
* const instructions = prompt`
89+
* You are a helpful assistant. Please:
90+
* * Be concise
91+
* * Be accurate
92+
* * Be helpful
93+
* `
94+
* ```
95+
*/
2096
export function prompt(
2197
strings: TemplateStringsArray,
2298
...values: unknown[]

0 commit comments

Comments
 (0)