( xml: string | string[], )
| 11 | } |
| 12 | |
| 13 | function items( |
| 14 | xml: string | string[], |
| 15 | ): AsyncGenerator<Record<string, string>> { |
| 16 | const chunks = Array.isArray(xml) ? xml : [xml]; |
| 17 | return parseXmlRecords<Record<string, string>>( |
| 18 | ReadableStream.from(chunks), |
| 19 | (emit) => { |
| 20 | let insideItem = false; |
| 21 | let currentTag = ""; |
| 22 | let currentItem: Record<string, string> = {}; |
| 23 | return { |
| 24 | onStartElement(name) { |
| 25 | if (name === "item") { |
| 26 | insideItem = true; |
| 27 | currentItem = {}; |
| 28 | } else if (insideItem) { |
| 29 | currentTag = name; |
| 30 | } |
| 31 | }, |
| 32 | onText(text) { |
| 33 | if (insideItem && currentTag) { |
| 34 | currentItem[currentTag] = (currentItem[currentTag] ?? "") + text; |
| 35 | } |
| 36 | }, |
| 37 | onEndElement(name) { |
| 38 | if (name === "item") { |
| 39 | emit(currentItem); |
| 40 | insideItem = false; |
| 41 | currentItem = {}; |
| 42 | } |
| 43 | currentTag = ""; |
| 44 | }, |
| 45 | }; |
| 46 | }, |
| 47 | { ignoreWhitespace: true }, |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | // ============================================================================= |
| 52 | // Basic record emission |
no test coverage detected