UNPKG

@sheetxl/io

Version:

IO - Import/Export Libraries for SheetXL.

13 lines (12 loc) 2.26 kB
"use strict";/** * @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. */Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const J=require("@sheetxl/sdk");exports.fromBufferJSON=async(b,l)=>{let c=b;(function(t){if(t.byteLength<4)return!1;const i=new Uint8Array(t),r={gzip:[31,139],zip:[80,75,3,4],bz2:[66,90,104],br:[206,178,207,129]},n=Object.keys(r);for(let s=0;s<n.length;s++){const o=n[s],e=r[o];let f=!0;for(let a=0;a<e.length;a++)if(i[a]!==e[a]){f=!1;break}if(f)return o}return!1})(c)&&(c=await async function(t,i="gzip"){let r;t instanceof SharedArrayBuffer?(r=new ArrayBuffer(t.byteLength),new Uint8Array(r).set(new Uint8Array(t))):r=t;const n=new Blob([r]).stream(),s=new DecompressionStream(i),o=n.pipeThrough(s);return(await new Response(o).blob()).arrayBuffer()}(c));let g=new TextDecoder().decode(c);const p=JSON.parse(g),w={...l?.constructorOptions??{},json:p};return new J.Workbook(w)},exports.toBufferJSON=async(b,l)=>{const c=await b.toJSON(),g=l?.whiteSpace??0;let p;l?.beforeWrite?.(b,c),p=g===0?JSON.stringify(c):function(i,r=2){const n=[];function s(o){const e=r*o;return n[e]===void 0&&(n[e]=" ".repeat(e)),n[e]}return function o(e,f,a,B){if(e==null||typeof e!="object")return JSON.stringify(e);const d=s(f),A=s(f+1);if(Array.isArray(e)){const u=e.length;if(u===0)return"[]";if(a!==0)return JSON.stringify(e);let y=`[ `;for(let h=0;h<u;h++)y+=A+o(e[h],f+1,a+1,!0),h<u-1&&(y+=`, `);return y+=` `+d+"]",y}const m=Object.keys(e),O=m.length;if(O===0)return"{}";if(!(a<2))return JSON.stringify(e);let S=`{ `;B||(a=0);for(let u=0;u<O;u++){const y=m[u];S+=A+JSON.stringify(y)+": "+o(e[y],f+1,a,!1),u<O-1&&(S+=`, `)}return S+=` `+d+"}",S}(i,0,0,!1)}(c,g);let w=new TextEncoder().encode(p).buffer;const t=l?.compress??!0;return t&&(w=await async function(i,r="gzip"){let n;i instanceof SharedArrayBuffer?(n=new ArrayBuffer(i.byteLength),new Uint8Array(n).set(new Uint8Array(i))):n=i;const s=new ReadableStream({start(f){f.enqueue(n),f.close()}}),o=new CompressionStream(r),e=s.pipeThrough(o);return(await new Response(e).blob()).arrayBuffer()}(w,typeof t=="string"?t:void 0)),w};