All files / x createXml.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38    1x 1x 1x 1x                 1x               1x 1x     1x 1x     1x 1x     1x 1x    
import { j2xParser as J2XParser, J2xOptionsOptional } from 'fast-xml-parser'
 
const arrayTagName = 'item' as const
const attrTagName = '@__attr__@' as const
const textTagName = '@__text__@' as const
const cdataTagName = '@__cdata__@' as const
 
/**
 * 创建 XML 文本。
 *
 * @param data 数据
 * @param options 选项
 */
export function createXml(data: any, options?: J2xOptionsOptional): string {
  return new J2XParser({
    ...options,
    attrNodeName: attrTagName,
    textNodeName: textTagName,
    cdataTagName: cdataTagName,
  }).parse(data)
}
 
createXml.array = function <T = any>(value: T[]): any {
  return { [arrayTagName]: value }
}
 
createXml.attr = function <T = any>(value: T): any {
  return { [attrTagName]: value }
}
 
createXml.text = function <T = any>(value: T): any {
  return { [textTagName]: value }
}
 
createXml.cdata = function <T = any>(value: T): any {
  return { [cdataTagName]: value }
}