• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmcelhaney / counterfact / 9310610020

30 May 2024 11:42PM CUT coverage: 87.241%. Remained the same
9310610020

Pull #902

github

web-flow
Update dependency eslint to v9
Pull Request #902: Update dependency eslint to v9

984 of 1094 branches covered (89.95%)

Branch coverage included in aggregate %.

3228 of 3734 relevant lines covered (86.45%)

44.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.43
/src/server/json-to-xml.ts
1
interface XmlHints {
2✔
2
  attribute?: boolean;
2✔
3
  name?: string;
2✔
4
  namespace?: string;
2✔
5
  prefix?: string;
2✔
6
  wrapped?: boolean;
2✔
7
}
2✔
8

2✔
9
interface Schema {
2✔
10
  [key: string]: unknown;
2✔
11
  items?: Schema;
2✔
12
  properties?: {
2✔
13
    [key: string]: Schema;
2✔
14
  };
2✔
15
  xml?: XmlHints;
2✔
16
}
2✔
17

2✔
18
function xmlEscape(xmlString: string): string {
2✔
19
  // eslint-disable-next-line unicorn/prefer-string-replace-all
2✔
20
  return xmlString.replace(/["&'<>]/gu, (character: string) => {
2✔
21
    switch (character) {
×
22
      case "<": {
×
23
        return "&lt;";
×
24
      }
×
25
      case ">": {
×
26
        return "&gt;";
×
27
      }
×
28
      case "&": {
×
29
        return "&amp;";
×
30
      }
×
31
      case "'": {
×
32
        return "&apos;";
×
33
      }
×
34
      case '"': {
×
35
        return "&quot;";
×
36
      }
×
37
      default: {
×
38
        return character;
×
39
      }
×
40
    }
×
41
  });
×
42
}
2✔
43

2✔
44
function objectToXml(
12✔
45
  json: object,
12✔
46
  schema: Schema | undefined,
12✔
47
  name: string,
12✔
48
): string {
12✔
49
  const xml: string[] = [];
12✔
50

12✔
51
  const attributes: string[] = [];
12✔
52

12✔
53
  Object.entries(json).forEach(([key, value]) => {
12✔
54
    const properties = schema?.properties?.[key];
20✔
55
    // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
20✔
56
    if (properties?.attribute) {
20✔
57
      attributes.push(` ${key}="${xmlEscape(String(value))}"`);
2✔
58
    } else {
20✔
59
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
18✔
60
      xml.push(jsonToXml(value, properties, key));
18✔
61
    }
18✔
62
  });
20✔
63

12✔
64
  return `<${name}${attributes.join("")}>${String(xml.join(""))}</${name}>`;
12✔
65
}
12✔
66

2✔
67
export function jsonToXml(
2✔
68
  json: unknown,
30✔
69
  schema: Schema | undefined,
30✔
70
  keyName = "root",
30✔
71
): string {
30✔
72
  const name = schema?.xml?.name ?? keyName;
30✔
73

30✔
74
  if (Array.isArray(json)) {
30!
75
    const items = json
×
76
      .map((item) => jsonToXml(item, schema?.items, name))
×
77
      .join("");
×
78

×
79
    // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
×
80
    if (schema?.xml?.wrapped) {
×
81
      return `<${name}>${items}</${name}>`;
×
82
    }
×
83

×
84
    return items;
×
85
  }
×
86

30✔
87
  if (typeof json === "object" && json !== null) {
30✔
88
    return objectToXml(json, schema, name);
12✔
89
  }
12✔
90

18✔
91
  return `<${name}>${String(json)}</${name}>`;
18✔
92
}
18✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc