MCPcopy Create free account
hub / github.com/Stenway/RSV-Challenge / stringToXmlElement

Function stringToXmlElement

JavaScript-Deno/rsv-conv.js:13–31  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

11// ----------------------------------------------------------------------
12
13function stringToXmlElement(str) {
14 let needsBase64Encoding = false
15 for (let i=0; i<str.length; i++) {
16 const c = str.charCodeAt(i)
17 if (c >= 0 && c <= 0x1F && c !== 0x09 && c !== 0x0A && c !== 0x0D) {
18 needsBase64Encoding = true
19 break
20 }
21 }
22 if (needsBase64Encoding === true) {
23 const encoder = new TextEncoder()
24 if (!/\p{Surrogate}/u.test(str) === false) { throw new Error(`Invalid string value`); }
25 const bytes = encoder.encode(str)
26 const binaryString = String.fromCodePoint(...bytes)
27 return "<B>"+btoa(binaryString)+"</B>"
28 } else {
29 return "<V>"+str.replace("&", "&amp;").replace("<", "&lt;")+"</V>"
30 }
31}
32
33export function rsvToXml(rows) {
34 return `<?xml version="1.0"?>\n<Rows>`+(rows.length > 0 ? "\n" : "") +

Callers 1

rsvToXmlFunction · 0.70

Calls 1

encodeMethod · 0.80

Tested by

no test coverage detected