(content: string, {
maxChars,
maxKeys
}: {
maxChars: number;
maxKeys: number;
})
| 264 | return `${k}: ${v}`; |
| 265 | } |
| 266 | function parseJsonEntries(content: string, { |
| 267 | maxChars, |
| 268 | maxKeys |
| 269 | }: { |
| 270 | maxChars: number; |
| 271 | maxKeys: number; |
| 272 | }): [string, unknown][] | null { |
| 273 | const trimmed = content.trim(); |
| 274 | if (trimmed.length === 0 || trimmed.length > maxChars || trimmed[0] !== '{') { |
| 275 | return null; |
| 276 | } |
| 277 | let parsed: unknown; |
| 278 | try { |
| 279 | parsed = jsonParse(trimmed); |
| 280 | } catch { |
| 281 | return null; |
| 282 | } |
| 283 | if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) { |
| 284 | return null; |
| 285 | } |
| 286 | const entries = Object.entries(parsed); |
| 287 | if (entries.length === 0 || entries.length > maxKeys) { |
| 288 | return null; |
| 289 | } |
| 290 | return entries; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * If content parses as a JSON object where every value is a scalar or a |
no test coverage detected