UNPKG

@byetool/json-exporter

Version:

A library that supports exporting json to .xls, .xlsx, .csv, .html, .xml, .json, .txt files in browser

76 lines (75 loc) 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function escape(value) { return String(value) .replace(/</g, '&lt;') .replace(/>/g, '&gt;') .replace(/&/g, '&amp;') .replace(/"/g, '&quot;') .replace(/'/g, '&apos;'); } function toSpaces(count) { return ' '.repeat(count); } function renderXML(data, { tag, props = {}, indent = 0, aliasHeaders = [], keyHeaders = [] }) { if (data == null) { return `<${tag} />`; } const spaces = toSpaces(indent); let shouldNewLine = true; let content = ''; if (Array.isArray(data)) { content = data .map((item) => renderXML(item, { tag: 'row', props: {}, indent: indent + 2, aliasHeaders, keyHeaders, })) .join('\n'); } else if (data !== null && typeof data === 'object') { if (keyHeaders.length > 0) { content = keyHeaders .map((key, index) => renderXML(data[key], { tag: 'col', props: { name: aliasHeaders[index] }, indent: indent + 2, })) .join('\n'); } else { content = Object.keys(data) .map((key) => renderXML(data[key], { tag: 'col', props: { name: key }, indent: indent + 2, })) .join('\n'); } } else { shouldNewLine = false; content = escape(data); } const attrs = Object.keys(props) .map((item) => ` ${item}="${props[item]}"`) .join(' '); if (shouldNewLine) { return [`${spaces}<${tag}${attrs}>`, content, `${spaces}</${tag}>`].join('\n'); } return [`${spaces}<${tag}${attrs}>`, content, `</${tag}>`].join(''); } function render(data, options) { const DTD = '<?xml version="1.0" encoding="UTF-8"?>'; const content = renderXML(data, { tag: 'rows', aliasHeaders: options.aliasHeaders || [], keyHeaders: options.keyHeaders || [], }); return [DTD, content].join('\n'); } exports.default = { render, };