@sheetxl/io
Version:
IO - Import/Export Libraries for SheetXL.
85 lines (84 loc) • 3.02 kB
JavaScript
import { Workbook as k } from "@sheetxl/sdk";
/**
* @license @sheetxl/io - IO - Import/Export Libraries for SheetXL. - v0.7.27
*
* (C) 2025-present SheetXL Inc. & Michael T. Ford
* License: The license can be found at https://www.sheetxl.com/license.
*/
const N = async (w, l) => {
const u = await w.toJSON(), b = l?.whiteSpace ?? 0;
let g;
l?.beforeWrite?.(w, u), g = b === 0 ? JSON.stringify(u) : (function(i, r = 2) {
const t = [];
function f(o) {
const e = r * o;
return t[e] === void 0 && (t[e] = " ".repeat(e)), t[e];
}
return (function o(e, a, s, O) {
if (e == null || typeof e != "object") return JSON.stringify(e);
const A = f(a), m = f(a + 1);
if (Array.isArray(e)) {
const c = e.length;
if (c === 0) return "[]";
if (s !== 0) return JSON.stringify(e);
let y = `[
`;
for (let S = 0; S < c; S++) y += m + o(e[S], a + 1, s + 1, !0), S < c - 1 && (y += `,
`);
return y += `
` + A + "]", y;
}
const B = Object.keys(e), h = B.length;
if (h === 0) return "{}";
if (!(s < 2)) return JSON.stringify(e);
let d = `{
`;
O || (s = 0);
for (let c = 0; c < h; c++) {
const y = B[c];
d += m + JSON.stringify(y) + ": " + o(e[y], a + 1, s, !1), c < h - 1 && (d += `,
`);
}
return d += `
` + A + "}", d;
})(i, 0, 0, !1);
})(u, b);
let p = new TextEncoder().encode(g).buffer;
const n = l?.compress ?? !0;
return n && (p = await (async function(i, r = "gzip") {
let t;
typeof SharedArrayBuffer < "u" && i instanceof SharedArrayBuffer ? (t = new ArrayBuffer(i.byteLength), new Uint8Array(t).set(new Uint8Array(i))) : t = i;
const f = new ReadableStream({ start(a) {
a.enqueue(t), a.close();
} }), o = new CompressionStream(r), e = f.pipeThrough(o);
return (await new Response(e).blob()).arrayBuffer();
})(p, typeof n == "string" ? n : void 0)), p;
}, z = async (w, l) => {
let u = w;
(function(n) {
if (n.byteLength < 4) return !1;
const i = new Uint8Array(n), r = { gzip: [31, 139], zip: [80, 75, 3, 4], bz2: [66, 90, 104], br: [206, 178, 207, 129] }, t = Object.keys(r);
for (let f = 0; f < t.length; f++) {
const o = t[f], e = r[o];
let a = !0;
for (let s = 0; s < e.length; s++) if (i[s] !== e[s]) {
a = !1;
break;
}
if (a) return o;
}
return !1;
})(u) && (u = await (async function(n, i = "gzip") {
let r;
typeof SharedArrayBuffer < "u" && n instanceof SharedArrayBuffer ? (r = new ArrayBuffer(n.byteLength), new Uint8Array(r).set(new Uint8Array(n))) : r = n;
const t = new Blob([r]).stream(), f = new DecompressionStream(i), o = t.pipeThrough(f);
return (await new Response(o).blob()).arrayBuffer();
})(u));
let b = new TextDecoder().decode(u);
const g = JSON.parse(b), p = { name: l?.name, ...l?.createWorkbookOptions ?? {}, json: g };
return new k(p);
};
export {
z as readBufferSXL,
N as writeBufferSXL
};