(
iterable: AsyncIterable<T> | T[],
json: boolean,
humanReadableWriter: (item: T) => void = (item) => console.log(formatReadableJson(item)),
jsonTransform: (item: T) => object = (json) => json,
)
| 13 | } |
| 14 | |
| 15 | export async function printIterable<T extends object>( |
| 16 | iterable: AsyncIterable<T> | T[], |
| 17 | json: boolean, |
| 18 | humanReadableWriter: (item: T) => void = (item) => console.log(formatReadableJson(item)), |
| 19 | jsonTransform: (item: T) => object = (json) => json, |
| 20 | ): Promise<void> { |
| 21 | if (json) { |
| 22 | // Output streaming JSON array format |
| 23 | process.stdout.write('[\n'); |
| 24 | let isFirst = true; |
| 25 | for await (const item of iterable) { |
| 26 | if (!isFirst) { |
| 27 | process.stdout.write(',\n'); |
| 28 | } |
| 29 | process.stdout.write(JSON.stringify(jsonTransform(item))); |
| 30 | isFirst = false; |
| 31 | } |
| 32 | process.stdout.write('\n]\n'); |
| 33 | } else { |
| 34 | for await (const item of iterable) { |
| 35 | humanReadableWriter(item); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | export function formatReadableJson(json: object) { |
| 41 | // Prints the JSON in a readable format without the depth limit. |
no test coverage detected