sheetxl
Version:
SheetXL - Command line tool
89 lines (88 loc) • 3.34 kB
JavaScript
/**
* @license sheetxl - SheetXL - Command line tool - v0.6.0
*
* (C) 2025-present SheetXL Inc. & Michael T. Ford
* License: The license can be found at https://www.sheetxl.com/license.
*/
import { y as e } from "../cli.js";
import "node:util";
import "node:path";
import "node:fs";
import "node:os";
import "open";
import "node:repl";
import "chalk";
import "fs";
import "path";
import "commander";
const r = async (e2, r2) => {
const t2 = await e2.toJSON(), n = r2?.whiteSpace ?? 0;
let o;
r2?.beforeWrite?.(e2, t2), o = 0 === n ? JSON.stringify(t2) : function(e3, r3 = 2) {
const t3 = [];
function n2(e4) {
const n3 = r3 * e4;
return void 0 === t3[n3] && (t3[n3] = " ".repeat(n3)), t3[n3];
}
return function e4(r4, t4, o2, i2) {
if (null == r4 || "object" != typeof r4) return JSON.stringify(r4);
const f2 = n2(t4), a = n2(t4 + 1);
if (Array.isArray(r4)) {
const n3 = r4.length;
if (0 === n3) return "[]";
if (0 !== o2) return JSON.stringify(r4);
let i3 = "[\n";
for (let f3 = 0; f3 < n3; f3++) i3 += a + e4(r4[f3], t4 + 1, o2 + 1, true), f3 < n3 - 1 && (i3 += ",\n");
return i3 += "\n" + f2 + "]", i3;
}
const s = Object.keys(r4), c = s.length;
if (0 === c) return "{}";
if (!(o2 < 2)) return JSON.stringify(r4);
let u = "{\n";
i2 || (o2 = 0);
for (let n3 = 0; n3 < c; n3++) {
const i3 = s[n3];
u += a + JSON.stringify(i3) + ": " + e4(r4[i3], t4 + 1, o2, false), n3 < c - 1 && (u += ",\n");
}
return u += "\n" + f2 + "}", u;
}(e3, 0, 0, false);
}(t2, n);
let i = new TextEncoder().encode(o).buffer;
const f = r2?.compress ?? true;
return f && (i = await async function(e3, r3 = "gzip") {
let t3;
"undefined" != typeof SharedArrayBuffer && e3 instanceof SharedArrayBuffer ? (t3 = new ArrayBuffer(e3.byteLength), new Uint8Array(t3).set(new Uint8Array(e3))) : t3 = e3;
const n2 = new ReadableStream({ start(e4) {
e4.enqueue(t3), e4.close();
} }), o2 = new CompressionStream(r3), i2 = n2.pipeThrough(o2);
return (await new Response(i2).blob()).arrayBuffer();
}(i, "string" == typeof f ? f : void 0)), i;
}, t = async (r2, t2) => {
let n = r2;
(function(e2) {
if (e2.byteLength < 4) return false;
const r3 = new Uint8Array(e2), t3 = { gzip: [31, 139], zip: [80, 75, 3, 4], bz2: [66, 90, 104], br: [206, 178, 207, 129] }, n2 = Object.keys(t3);
for (let e3 = 0; e3 < n2.length; e3++) {
const o2 = n2[e3], i2 = t3[o2];
let f2 = true;
for (let e4 = 0; e4 < i2.length; e4++) if (r3[e4] !== i2[e4]) {
f2 = false;
break;
}
if (f2) return o2;
}
return false;
})(n) && (n = await async function(e2, r3 = "gzip") {
let t3;
"undefined" != typeof SharedArrayBuffer && e2 instanceof SharedArrayBuffer ? (t3 = new ArrayBuffer(e2.byteLength), new Uint8Array(t3).set(new Uint8Array(e2))) : t3 = e2;
const n2 = new Blob([t3]).stream(), o2 = new DecompressionStream(r3), i2 = n2.pipeThrough(o2);
return (await new Response(i2).blob()).arrayBuffer();
}(n));
let o = new TextDecoder().decode(n);
const i = JSON.parse(o), f = { ...t2?.createWorkbookOptions ?? {}, json: i };
return new e(f);
};
export {
t as fromBufferJSON,
r as toBufferJSON
};