js-cfb/bits/77_writeutils.js
SheetJS 0e33eb6e13 unify changes with main sheetjs project
- Fixed issue #14 h/t @wisekaa03
- CFB write truncate file names exceeding 32 characters

Co-authored-by: Stanislav Vyaliy <wisekaa03@gmail.com>
2022-03-31 04:57:58 -04:00

26 lines
1.0 KiB
JavaScript

function write_file(cfb/*:CFBContainer*/, filename/*:string*/, options/*:CFBWriteOpts*/)/*:void*/ {
get_fs();
var o = _write(cfb, options);
/*:: if(typeof Buffer == 'undefined' || !Buffer.isBuffer(o) || !(o instanceof Buffer)) throw new Error("unreachable"); */
fs.writeFileSync(filename, o);
}
function a2s(o/*:RawBytes*/)/*:string*/ {
var out = new Array(o.length);
for(var i = 0; i < o.length; ++i) out[i] = String.fromCharCode(o[i]);
return out.join("");
}
function write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|string*/ {
var o = _write(cfb, options);
switch(options && options.type || "buffer") {
case "file": get_fs(); fs.writeFileSync(options.filename, (o/*:any*/)); return o;
case "binary": return typeof o == "string" ? o : a2s(o);
case "base64": return Base64_encode(typeof o == "string" ? o : a2s(o));
case "buffer": if(has_buf) return Buffer.isBuffer(o) ? o : Buffer_from(o);
/* falls through */
case "array": return typeof o == "string" ? s2a(o) : o;
}
return o;
}