MCPcopy Create free account
hub / github.com/denoland/std / items

Function items

xml/parse_records_test.ts:13–49  ·  view source on GitHub ↗
(
  xml: string | string[],
)

Source from the content-addressed store, hash-verified

11}
12
13function 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

Callers 1

Calls 2

parseXmlRecordsFunction · 0.90
fromMethod · 0.45

Tested by

no test coverage detected