@@ -58,9 +58,38 @@ export function fixJsonControlCharacters(jsonString: string): string {
5858
5959/**
6060 * Extracts a field value from partial JSON, handling incomplete strings
61- * Pattern matches escaped characters: (?:[^"\\]|\\.)*
62- * - [^"\\] matches any char except quote or backslash
63- * - \\. matches backslash followed by any char (handles \", \n, etc)
61+ *
62+ * Uses regex to find string field values in streaming JSON, even when the closing
63+ * quote hasn't arrived yet. Handles escaped characters properly.
64+ *
65+ * **Pattern explanation:** `(?:[^"\\]|\\.)*`
66+ * - `[^"\\]` matches any char except quote or backslash
67+ * - `\\.` matches backslash followed by any char (handles \", \n, etc)
68+ * - `(?:...)` non-capturing group
69+ * - `*` matches zero or more times
70+ *
71+ * **Examples:**
72+ * ```typescript
73+ * // Complete field
74+ * extractField('{"name": "John Doe"}', 'name')
75+ * // → "John Doe"
76+ *
77+ * // Incomplete field (no closing quote yet)
78+ * extractField('{"name": "John Do', 'name')
79+ * // → "John Do"
80+ *
81+ * // Escaped characters
82+ * extractField('{"description": "Line 1\\nLine 2"}', 'description')
83+ * // → "Line 1\nLine 2" (unescaped)
84+ *
85+ * // Escaped quotes in value
86+ * extractField('{"text": "He said \\"Hello\\""}', 'text')
87+ * // → "He said \"Hello\""
88+ *
89+ * // Field not found
90+ * extractField('{"name": "John"}', 'age')
91+ * // → undefined
92+ * ```
6493 */
6594export function extractField (
6695 jsonStr : string ,
@@ -87,7 +116,40 @@ export function extractField(
87116
88117/**
89118 * Extracts array items from partial JSON
90- * Handles both complete and incomplete array strings
119+ *
120+ * Handles both complete and incomplete array strings, finding all complete items
121+ * and optionally the incomplete last item (string without closing quote).
122+ *
123+ * Uses two regex patterns:
124+ * 1. Global pattern to find all complete items: `/"((?:[^"\\]|\\.)*)"/g`
125+ * 2. Anchored pattern to find incomplete last item: `/"((?:[^"\\]|\\.)*?)$/`
126+ *
127+ * **Examples:**
128+ * ```typescript
129+ * // Complete array
130+ * extractArrayField('{"phones": ["555-1234", "555-5678"]}', 'phones')
131+ * // → ["555-1234", "555-5678"]
132+ *
133+ * // Incomplete array (no closing bracket)
134+ * extractArrayField('{"phones": ["555-1234", "555-5678"', 'phones')
135+ * // → ["555-1234", "555-5678"]
136+ *
137+ * // Incomplete last item (no closing quote)
138+ * extractArrayField('{"phones": ["555-1234", "555-56', 'phones')
139+ * // → ["555-1234", "555-56"]
140+ *
141+ * // Escaped characters in items
142+ * extractArrayField('{"lines": ["Line 1\\nLine 2", "Line 3"]}', 'lines')
143+ * // → ["Line 1\nLine 2", "Line 3"]
144+ *
145+ * // Empty array
146+ * extractArrayField('{"phones": []}', 'phones')
147+ * // → []
148+ *
149+ * // Array not found
150+ * extractArrayField('{"name": "John"}', 'phones')
151+ * // → []
152+ * ```
91153 */
92154export function extractArrayField (
93155 jsonStr : string ,
0 commit comments