UNPKG

@sheetxl/io

Version:

IO - Import/Export Libraries for SheetXL.

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