@byetool/json-exporter
Version:
A library that supports exporting json to .xls, .xlsx, .csv, .html, .xml, .json, .txt files in browser
157 lines (156 loc) • 5.3 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toText = exports.toJSON = exports.toXML = exports.toHTML = exports.toCSV = exports.toXLSX = exports.toXLS = void 0;
const xlsx_1 = require("xlsx");
const file_saver_1 = require("file-saver");
const xml_1 = require("./xml");
function normalizeHeaders(headers) {
const aliasHeaders = [];
const keyHeaders = [];
if (Array.isArray(headers)) {
for (const header of headers) {
if (typeof header === 'string') {
aliasHeaders.push(header);
keyHeaders.push(header);
}
else {
aliasHeaders.push(header.alias);
keyHeaders.push(header.name);
}
}
}
return { aliasHeaders, keyHeaders };
}
function aoo2aoa(data, headers) {
const { aliasHeaders, keyHeaders } = normalizeHeaders(headers);
const aoa = [aliasHeaders];
for (const item of data) {
const row = [];
for (const key of keyHeaders) {
row.push(item[key]);
}
aoa.push(row);
}
return aoa;
}
function transformData(data, headers) {
const { aliasHeaders, keyHeaders } = normalizeHeaders(headers);
const aoo = [];
for (const item of data) {
const row = {};
for (const [index, key] of keyHeaders.entries()) {
row[aliasHeaders[index]] = item[key];
}
aoo.push(row);
}
return aoo;
}
function toJSONLine(data) {
let text = '';
for (const item of data) {
text += `${JSON.stringify(item)}\n`;
}
return text;
}
function exportSheet(ws, filename, type) {
const wb = xlsx_1.utils.book_new();
xlsx_1.utils.book_append_sheet(wb, ws);
(0, xlsx_1.writeFile)(wb, `${filename}.${type}`, {
type: 'binary',
bookType: type,
});
}
function toXLS(data, filename, options = { headers: [] }) {
const { headers } = options, xlsxOptions = __rest(options, ["headers"]);
let ws = null;
if (headers && headers.length > 0) {
ws = xlsx_1.utils.aoa_to_sheet(aoo2aoa(data, headers), xlsxOptions);
}
else {
ws = xlsx_1.utils.json_to_sheet(data, xlsxOptions);
}
exportSheet(ws, filename, 'xls');
}
exports.toXLS = toXLS;
function toXLSX(data, filename, options = { headers: [] }) {
const { headers } = options, xlsxOptions = __rest(options, ["headers"]);
let ws = null;
if (headers && headers.length > 0) {
ws = xlsx_1.utils.aoa_to_sheet(aoo2aoa(data, headers), xlsxOptions);
}
else {
ws = xlsx_1.utils.json_to_sheet(data, xlsxOptions);
}
exportSheet(ws, filename, 'xlsx');
}
exports.toXLSX = toXLSX;
function toCSV(data, filename, options = { headers: [] }) {
const { headers } = options, xlsxOptions = __rest(options, ["headers"]);
let ws = null;
if (headers && headers.length > 0) {
ws = xlsx_1.utils.aoa_to_sheet(aoo2aoa(data, headers), xlsxOptions);
}
else {
ws = xlsx_1.utils.json_to_sheet(data, xlsxOptions);
}
exportSheet(ws, filename, 'csv');
}
exports.toCSV = toCSV;
function toHTML(data, filename, options = { headers: [] }) {
const { headers } = options, xlsxOptions = __rest(options, ["headers"]);
let ws = null;
if (headers && headers.length > 0) {
ws = xlsx_1.utils.aoa_to_sheet(aoo2aoa(data, headers), xlsxOptions);
}
else {
ws = xlsx_1.utils.json_to_sheet(data, xlsxOptions);
}
exportSheet(ws, filename, 'html');
}
exports.toHTML = toHTML;
function toXML(data, filename, options = { headers: [] }) {
const { headers } = options;
const { aliasHeaders, keyHeaders } = normalizeHeaders(headers);
const content = xml_1.default.render(data, { aliasHeaders, keyHeaders });
(0, file_saver_1.saveAs)(new Blob([content], { type: 'application/xml;charset=utf-8' }), `${filename}.xml`);
}
exports.toXML = toXML;
function toJSON(data, filename, options = { headers: [] }) {
const { headers } = options;
if (headers && headers.length > 0) {
data = transformData(data, headers);
}
const prettyJson = JSON.stringify(data, null, ' ');
(0, file_saver_1.saveAs)(new Blob([prettyJson], { type: 'application/json;charset=utf-8' }), `${filename}.json`);
}
exports.toJSON = toJSON;
function toText(data, filename, options = { headers: [] }) {
const { headers } = options;
if (headers && headers.length > 0) {
data = transformData(data, headers);
}
const jsonLine = toJSONLine(data);
(0, file_saver_1.saveAs)(new Blob([jsonLine], { type: 'text/plain;charset=utf-8' }), `${filename}.txt`);
}
exports.toText = toText;
const Exporter = {
toXLS,
toXLSX,
toCSV,
toHTML,
toXML,
toJSON,
toText,
};
exports.default = Exporter;
;