format-prompt 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ //#region src/index.d.ts
2
+ /**
3
+ * A template literal tag function that formats prompts for cleaner presentation and optimal token usage.
4
+ *
5
+ * This function processes template literals to remove unnecessary whitespace and line breaks while
6
+ * preserving the intended structure of your prompts. It's particularly useful for AI/LLM prompts
7
+ * where you want readable, well-formatted code but need compact, token-efficient output.
8
+ *
9
+ * The function automatically:
10
+ * - Collapses multiple spaces into single spaces
11
+ * - Removes excessive empty lines (limiting to maximum 1 consecutive empty line)
12
+ * - Trims leading whitespace from each line (removing indentation)
13
+ * - Preserves intentional line breaks and list structures
14
+ * - Handles template interpolations correctly
15
+ * - Trims the final output
16
+ *
17
+ * @param strings - The template literal string parts
18
+ * @param values - The interpolated values in the template literal
19
+ * @returns The formatted prompt string with optimized whitespace
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { prompt } from 'format-prompt'
24
+ *
25
+ * // Basic usage with multiline string
26
+ * const formatted = prompt`
27
+ * Hello world
28
+ * This is a prompt
29
+ * `
30
+ * console.log(formatted) // "Hello world\nThis is a prompt"
31
+ *
32
+ * // With interpolation
33
+ * const name = "John"
34
+ * const age = 30
35
+ * const result = prompt`
36
+ * Name: ${name}
37
+ * Age: ${age}
38
+ * `
39
+ * console.log(result) // "Name: John\nAge: 30"
40
+ *
41
+ * // Complex prompt with lists
42
+ * const instructions = prompt`
43
+ * You are a helpful assistant. Please:
44
+ * * Be concise
45
+ * * Be accurate
46
+ * * Be helpful
47
+ * `
48
+ * ```
49
+ */
50
+ declare function prompt(strings: TemplateStringsArray, ...values: unknown[]): string;
51
+ //#endregion
52
+ export { prompt };
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ import { collWhitespace } from "string-collapse-leading-whitespace";
2
+ import { collapse } from "string-collapse-white-space";
3
+
4
+ //#region src/index.ts
5
+ const startRegex = /^([\s\n\r]+)/;
6
+ const endRegex = /([\s\n\r]+)$/;
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
+ */
35
+ function formatString(s) {
36
+ const start = startRegex.exec(s)?.[0] ?? "";
37
+ const end = endRegex.exec(s)?.[0] ?? "";
38
+ const formatted = collapse(s, {
39
+ trimnbsp: true,
40
+ trimLines: true,
41
+ removeEmptyLines: true,
42
+ limitConsecutiveEmptyLinesTo: 1
43
+ }).result;
44
+ return collWhitespace(start + formatted + end, 2);
45
+ }
46
+ /**
47
+ * A template literal tag function that formats prompts for cleaner presentation and optimal token usage.
48
+ *
49
+ * This function processes template literals to remove unnecessary whitespace and line breaks while
50
+ * preserving the intended structure of your prompts. It's particularly useful for AI/LLM prompts
51
+ * where you want readable, well-formatted code but need compact, token-efficient output.
52
+ *
53
+ * The function automatically:
54
+ * - Collapses multiple spaces into single spaces
55
+ * - Removes excessive empty lines (limiting to maximum 1 consecutive empty line)
56
+ * - Trims leading whitespace from each line (removing indentation)
57
+ * - Preserves intentional line breaks and list structures
58
+ * - Handles template interpolations correctly
59
+ * - Trims the final output
60
+ *
61
+ * @param strings - The template literal string parts
62
+ * @param values - The interpolated values in the template literal
63
+ * @returns The formatted prompt string with optimized whitespace
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * import { prompt } from 'format-prompt'
68
+ *
69
+ * // Basic usage with multiline string
70
+ * const formatted = prompt`
71
+ * Hello world
72
+ * This is a prompt
73
+ * `
74
+ * console.log(formatted) // "Hello world\nThis is a prompt"
75
+ *
76
+ * // With interpolation
77
+ * const name = "John"
78
+ * const age = 30
79
+ * const result = prompt`
80
+ * Name: ${name}
81
+ * Age: ${age}
82
+ * `
83
+ * console.log(result) // "Name: John\nAge: 30"
84
+ *
85
+ * // Complex prompt with lists
86
+ * const instructions = prompt`
87
+ * You are a helpful assistant. Please:
88
+ * * Be concise
89
+ * * Be accurate
90
+ * * Be helpful
91
+ * `
92
+ * ```
93
+ */
94
+ function prompt(strings, ...values) {
95
+ let promptString = "";
96
+ for (let i = 0; i < strings.length; i++) {
97
+ promptString += formatString(strings[i]);
98
+ if (i < values.length) promptString += String(values[i]);
99
+ }
100
+ return promptString.trim();
101
+ }
102
+
103
+ //#endregion
104
+ export { prompt };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "format-prompt",
3
3
  "description": "A utility to format prompts for a cleaner presentation and optimal token usage",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "author": {
6
6
  "name": "Khánh Hoàng",
7
7
  "email": "hi@khanh.id",
8
8
  "url": "https://www.khanh.id"
9
9
  },
10
- "homepage": "https://github.com/hckhanh/format-prompt",
10
+ "homepage": "https://prompt.khanh.id",
11
11
  "repository": {
12
12
  "type": "git",
13
13
  "url": "git+https://github.com/hckhanh/format-prompt.git"