Skip to content

Commit

Permalink
popping IIFEs to appease rollup tree shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetJSDev committed Mar 22, 2022
1 parent 2f274dd commit 0f0b3de
Show file tree
Hide file tree
Showing 21 changed files with 410 additions and 378 deletions.
16 changes: 0 additions & 16 deletions .fossaignore

This file was deleted.

88 changes: 42 additions & 46 deletions bits/04_base64.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
var Base64 = function() {
var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
return {
encode: function(input) {
var o = "";
var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0;
for (var i = 0; i < input.length; ) {
c1 = input.charCodeAt(i++);
e1 = c1 >> 2;
c2 = input.charCodeAt(i++);
e2 = (c1 & 3) << 4 | c2 >> 4;
c3 = input.charCodeAt(i++);
e3 = (c2 & 15) << 2 | c3 >> 6;
e4 = c3 & 63;
if (isNaN(c2)) {
e3 = e4 = 64;
} else if (isNaN(c3)) {
e4 = 64;
}
o += map.charAt(e1) + map.charAt(e2) + map.charAt(e3) + map.charAt(e4);
}
return o;
},
decode: function(input) {
var o = "";
var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0;
input = input.replace(/[^\w\+\/\=]/g, "");
for (var i = 0; i < input.length; ) {
e1 = map.indexOf(input.charAt(i++));
e2 = map.indexOf(input.charAt(i++));
c1 = e1 << 2 | e2 >> 4;
o += String.fromCharCode(c1);
e3 = map.indexOf(input.charAt(i++));
c2 = (e2 & 15) << 4 | e3 >> 2;
if (e3 !== 64) {
o += String.fromCharCode(c2);
}
e4 = map.indexOf(input.charAt(i++));
c3 = (e3 & 3) << 6 | e4;
if (e4 !== 64) {
o += String.fromCharCode(c3);
}
}
return o;
var Base64_map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function Base64_encode(input) {
var o = "";
var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0;
for (var i = 0; i < input.length; ) {
c1 = input.charCodeAt(i++);
e1 = c1 >> 2;
c2 = input.charCodeAt(i++);
e2 = (c1 & 3) << 4 | c2 >> 4;
c3 = input.charCodeAt(i++);
e3 = (c2 & 15) << 2 | c3 >> 6;
e4 = c3 & 63;
if (isNaN(c2)) {
e3 = e4 = 64;
} else if (isNaN(c3)) {
e4 = 64;
}
};
}();
o += Base64_map.charAt(e1) + Base64_map.charAt(e2) + Base64_map.charAt(e3) + Base64_map.charAt(e4);
}
return o;
}
function Base64_decode(input) {
var o = "";
var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0;
input = input.replace(/[^\w\+\/\=]/g, "");
for (var i = 0; i < input.length; ) {
e1 = Base64_map.indexOf(input.charAt(i++));
e2 = Base64_map.indexOf(input.charAt(i++));
c1 = e1 << 2 | e2 >> 4;
o += String.fromCharCode(c1);
e3 = Base64_map.indexOf(input.charAt(i++));
c2 = (e2 & 15) << 4 | e3 >> 2;
if (e3 !== 64) {
o += String.fromCharCode(c2);
}
e4 = Base64_map.indexOf(input.charAt(i++));
c3 = (e3 & 3) << 6 | e4;
if (e4 !== 64) {
o += String.fromCharCode(c3);
}
}
return o;
}
9 changes: 4 additions & 5 deletions bits/18_cfb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*::
declare var Base64:any;
declare var ReadShift:any;
declare var CheckField:any;
declare var prep_blob:any;
Expand Down Expand Up @@ -552,7 +551,7 @@ function read(blob/*:RawBytes|string*/, options/*:CFBReadOpts*/) {
}
switch(type || "base64") {
case "file": /*:: if(typeof blob !== 'string') throw "Must pass a filename when type='file'"; */return read_file(blob, options);
case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64.decode(blob)), options);
case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64_decode(blob)), options);
case "binary": /*:: if(typeof blob !== 'string') throw "Must pass a binary string when type='file'"; */return parse(s2a(blob), options);
}
return parse(/*::typeof blob == 'string' ? new Buffer(blob, 'utf-8') : */blob, options);
Expand Down Expand Up @@ -875,7 +874,7 @@ function write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|string
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 "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;
Expand Down Expand Up @@ -1584,7 +1583,7 @@ function get_content_type(fi/*:CFBEntry*/, fp/*:string*/)/*:string*/ {

/* 76 character chunks TODO: intertwine encoding */
function write_base64_76(bstr/*:string*/)/*:string*/ {
var data = Base64.encode(bstr);
var data = Base64_encode(bstr);
var o = [];
for(var i = 0; i < data.length; i+= 76) o.push(data.slice(i, i+76));
return o.join("\r\n") + "\r\n";
Expand Down Expand Up @@ -1665,7 +1664,7 @@ function parse_mime(cfb/*:CFBContainer*/, data/*:Array<string>*/, root/*:string*
}
++di;
switch(cte.toLowerCase()) {
case 'base64': fdata = s2a(Base64.decode(data.slice(di).join(""))); break;
case 'base64': fdata = s2a(Base64_decode(data.slice(di).join(""))); break;
case 'quoted-printable': fdata = parse_quoted_printable(data.slice(di)); break;
default: throw new Error("Unsupported Content-Transfer-Encoding " + cte);
}
Expand Down
7 changes: 5 additions & 2 deletions bits/29_xlsenum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* [MS-OLEPS] 2.2 PropertyType */
// Note: some tree shakers cannot handle VT_VECTOR | $CONST, hence extra vars
//var VT_EMPTY = 0x0000;
//var VT_NULL = 0x0001;
var VT_I2 = 0x0002;
Expand Down Expand Up @@ -33,6 +34,8 @@ var VT_CF = 0x0047;
//var VT_CLSID = 0x0048;
//var VT_VERSIONED_STREAM = 0x0049;
var VT_VECTOR = 0x1000;
var VT_VECTOR_VARIANT = 0x100C;
var VT_VECTOR_LPSTR = 0x101E;
//var VT_ARRAY = 0x2000;

var VT_STRING = 0x0050; // 2.3.3.1.11 VtString
Expand All @@ -52,8 +55,8 @@ var DocSummaryPIDDSI = {
/*::[*/0x09/*::]*/: { n: 'HiddenCount', t: VT_I4 },
/*::[*/0x0a/*::]*/: { n: 'MultimediaClipCount', t: VT_I4 },
/*::[*/0x0b/*::]*/: { n: 'ScaleCrop', t: VT_BOOL },
/*::[*/0x0c/*::]*/: { n: 'HeadingPairs', t: VT_VECTOR | VT_VARIANT },
/*::[*/0x0d/*::]*/: { n: 'TitlesOfParts', t: VT_VECTOR | VT_LPSTR },
/*::[*/0x0c/*::]*/: { n: 'HeadingPairs', t: VT_VECTOR_VARIANT /* VT_VECTOR | VT_VARIANT */ },
/*::[*/0x0d/*::]*/: { n: 'TitlesOfParts', t: VT_VECTOR_LPSTR /* VT_VECTOR | VT_LPSTR */ },
/*::[*/0x0e/*::]*/: { n: 'Manager', t: VT_STRING },
/*::[*/0x0f/*::]*/: { n: 'Company', t: VT_STRING },
/*::[*/0x10/*::]*/: { n: 'LinksUpToDate', t: VT_BOOL },
Expand Down
8 changes: 4 additions & 4 deletions bits/40_harb.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function dbf_to_aoa(buf, opts)/*:AOA*/ {
var out/*:AOA*/ = [];
var d/*:Block*/ = (new_raw_buf(1)/*:any*/);
switch(opts.type) {
case 'base64': d = s2a(Base64.decode(buf)); break;
case 'base64': d = s2a(Base64_decode(buf)); break;
case 'binary': d = s2a(buf); break;
case 'buffer':
case 'array': d = buf; break;
Expand Down Expand Up @@ -394,7 +394,7 @@ var SYLK = /*#__PURE__*/(function() {
/* TODO: find an actual specification */
function sylk_to_aoa(d/*:RawData*/, opts)/*:[AOA, Worksheet]*/ {
switch(opts.type) {
case 'base64': return sylk_to_aoa_str(Base64.decode(d), opts);
case 'base64': return sylk_to_aoa_str(Base64_decode(d), opts);
case 'binary': return sylk_to_aoa_str(d, opts);
case 'buffer': return sylk_to_aoa_str(has_buf && Buffer.isBuffer(d) ? d.toString('binary') : a2s(d), opts);
case 'array': return sylk_to_aoa_str(cc2str(d), opts);
Expand Down Expand Up @@ -597,7 +597,7 @@ var SYLK = /*#__PURE__*/(function() {
var DIF = /*#__PURE__*/(function() {
function dif_to_aoa(d/*:RawData*/, opts)/*:AOA*/ {
switch(opts.type) {
case 'base64': return dif_to_aoa_str(Base64.decode(d), opts);
case 'base64': return dif_to_aoa_str(Base64_decode(d), opts);
case 'binary': return dif_to_aoa_str(d, opts);
case 'buffer': return dif_to_aoa_str(has_buf && Buffer.isBuffer(d) ? d.toString('binary') : a2s(d), opts);
case 'array': return dif_to_aoa_str(cc2str(d), opts);
Expand Down Expand Up @@ -963,7 +963,7 @@ var PRN = /*#__PURE__*/(function() {
function prn_to_sheet(d/*:RawData*/, opts)/*:Worksheet*/ {
var str = "", bytes = opts.type == 'string' ? [0,0,0,0] : firstbyte(d, opts);
switch(opts.type) {
case 'base64': str = Base64.decode(d); break;
case 'base64': str = Base64_decode(d); break;
case 'binary': str = d; break;
case 'buffer':
if(opts.codepage == 65001) str = d.toString('utf8'); // TODO: test if buf
Expand Down
2 changes: 1 addition & 1 deletion bits/41_lotus.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var WK_ = /*#__PURE__*/(function() {

function lotus_to_workbook(d/*:RawData*/, opts) {
switch(opts.type) {
case 'base64': return lotus_to_workbook_buf(s2a(Base64.decode(d)), opts);
case 'base64': return lotus_to_workbook_buf(s2a(Base64_decode(d)), opts);
case 'binary': return lotus_to_workbook_buf(s2a(d), opts);
case 'buffer':
case 'array': return lotus_to_workbook_buf(d, opts);
Expand Down
2 changes: 1 addition & 1 deletion bits/45_rtf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var RTF = /*#__PURE__*/(function() {
function rtf_to_sheet(d/*:RawData*/, opts)/*:Worksheet*/ {
switch(opts.type) {
case 'base64': return rtf_to_sheet_str(Base64.decode(d), opts);
case 'base64': return rtf_to_sheet_str(Base64_decode(d), opts);
case 'binary': return rtf_to_sheet_str(d, opts);
case 'buffer': return rtf_to_sheet_str(has_buf && Buffer.isBuffer(d) ? d.toString('binary') : a2s(d), opts);
case 'array': return rtf_to_sheet_str(cc2str(d), opts);
Expand Down
2 changes: 1 addition & 1 deletion bits/75_xlml.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ function parse_xlml_xml(d, _opts)/*:Workbook*/ {
function parse_xlml(data/*:RawBytes|string*/, opts)/*:Workbook*/ {
fix_read_opts(opts=opts||{});
switch(opts.type||"base64") {
case "base64": return parse_xlml_xml(Base64.decode(data), opts);
case "base64": return parse_xlml_xml(Base64_decode(data), opts);
case "binary": case "buffer": case "file": return parse_xlml_xml(data, opts);
case "array": return parse_xlml_xml(a2s(data), opts);
}
Expand Down
2 changes: 1 addition & 1 deletion bits/76_xls.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ if(cfb.FullPaths) {
WB = CFB.find(cfb, '/Workbook') || CFB.find(cfb, '/Book');
} else {
switch(options.type) {
case 'base64': cfb = s2a(Base64.decode(cfb)); break;
case 'base64': cfb = s2a(Base64_decode(cfb)); break;
case 'binary': cfb = s2a(cfb); break;
case 'buffer': break;
case 'array': if(!Array.isArray(cfb)) cfb = Array.prototype.slice.call(cfb); break;
Expand Down
12 changes: 12 additions & 0 deletions bits/83_numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ function parse_shallow(buf) {
}
return out;
}
function write_shallow(proto) {
var out = [];
proto.forEach(function(field, idx) {
field.forEach(function(item) {
out.push(write_varint49(idx * 8 + item.type));
if (item.type == 2)
out.push(write_varint49(item.data.length));
out.push(item.data);
});
});
return u8concat(out);
}
function mappa(data, cb) {
if (!data)
return [];
Expand Down
6 changes: 3 additions & 3 deletions bits/87_read.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function firstbyte(f/*:RawData*/,o/*:?TypeOpts*/)/*:Array<number>*/ {
var x = "";
switch((o||{}).type || "base64") {
case 'buffer': return [f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]];
case 'base64': x = Base64.decode(f.slice(0,12)); break;
case 'base64': x = Base64_decode(f.slice(0,12)); break;
case 'binary': x = f; break;
case 'array': return [f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]];
default: throw new Error("Unrecognized type " + (o && o.type || "undefined"));
Expand Down Expand Up @@ -36,7 +36,7 @@ function read_plaintext(data/*:string*/, o/*:ParseOpts*/)/*:Workbook*/ {
function read_plaintext_raw(data/*:RawData*/, o/*:ParseOpts*/)/*:Workbook*/ {
var str = "", bytes = firstbyte(data, o);
switch(o.type) {
case 'base64': str = Base64.decode(data); break;
case 'base64': str = Base64_decode(data); break;
case 'binary': str = data; break;
case 'buffer': str = data.toString('binary'); break;
case 'array': str = cc2str(data); break;
Expand All @@ -49,7 +49,7 @@ function read_plaintext_raw(data/*:RawData*/, o/*:ParseOpts*/)/*:Workbook*/ {

function read_utf16(data/*:RawData*/, o/*:ParseOpts*/)/*:Workbook*/ {
var d = data;
if(o.type == 'base64') d = Base64.decode(d);
if(o.type == 'base64') d = Base64_decode(d);
d = $cptable.utils.decode(1200, d.slice(2), 'str');
o.type = "binary";
return read_plaintext(d, o);
Expand Down
6 changes: 3 additions & 3 deletions bits/88_write.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function write_string_type(out/*:string*/, opts/*:WriteOpts*/, bom/*:?string*/)/
if(!bom) bom = "";
var o = bom + out;
switch(opts.type) {
case "base64": return Base64.encode(utf8write(o));
case "base64": return Base64_encode(utf8write(o));
case "binary": return utf8write(o);
case "string": return out;
case "file": return write_dl(opts.file, o, 'utf8');
Expand All @@ -72,7 +72,7 @@ function write_string_type(out/*:string*/, opts/*:WriteOpts*/, bom/*:?string*/)/

function write_stxt_type(out/*:string*/, opts/*:WriteOpts*/)/*:any*/ {
switch(opts.type) {
case "base64": return Base64.encode(out);
case "base64": return Base64_encode(out);
case "binary": return out;
case "string": return out; /* override in sheet_to_txt */
case "file": return write_dl(opts.file, out, 'binary');
Expand All @@ -93,7 +93,7 @@ function write_binary_type(out, opts/*:WriteOpts*/)/*:any*/ {
var bstr = "";
// $FlowIgnore
for(var i = 0; i < out.length; ++i) bstr += String.fromCharCode(out[i]);
return opts.type == 'base64' ? Base64.encode(bstr) : opts.type == 'string' ? utf8read(bstr) : bstr;
return opts.type == 'base64' ? Base64_encode(bstr) : opts.type == 'string' ? utf8read(bstr) : bstr;
case "file": return write_dl(opts.file, out);
case "buffer": return out;
default: throw new Error("Unrecognized type " + opts.type);
Expand Down
9 changes: 4 additions & 5 deletions misc/18_esmcfb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*::
declare var Base64:any;
declare var ReadShift:any;
declare var CheckField:any;
declare var prep_blob:any;
Expand Down Expand Up @@ -552,7 +551,7 @@ function read(blob/*:RawBytes|string*/, options/*:CFBReadOpts*/) {
}
switch(type || "base64") {
case "file": /*:: if(typeof blob !== 'string') throw "Must pass a filename when type='file'"; */return read_file(blob, options);
case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64.decode(blob)), options);
case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64_decode(blob)), options);
case "binary": /*:: if(typeof blob !== 'string') throw "Must pass a binary string when type='file'"; */return parse(s2a(blob), options);
}
return parse(/*::typeof blob == 'string' ? new Buffer(blob, 'utf-8') : */blob, options);
Expand Down Expand Up @@ -875,7 +874,7 @@ function write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|string
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 "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;
Expand Down Expand Up @@ -1584,7 +1583,7 @@ function get_content_type(fi/*:CFBEntry*/, fp/*:string*/)/*:string*/ {

/* 76 character chunks TODO: intertwine encoding */
function write_base64_76(bstr/*:string*/)/*:string*/ {
var data = Base64.encode(bstr);
var data = Base64_encode(bstr);
var o = [];
for(var i = 0; i < data.length; i+= 76) o.push(data.slice(i, i+76));
return o.join("\r\n") + "\r\n";
Expand Down Expand Up @@ -1665,7 +1664,7 @@ function parse_mime(cfb/*:CFBContainer*/, data/*:Array<string>*/, root/*:string*
}
++di;
switch(cte.toLowerCase()) {
case 'base64': fdata = s2a(Base64.decode(data.slice(di).join(""))); break;
case 'base64': fdata = s2a(Base64_decode(data.slice(di).join(""))); break;
case 'quoted-printable': fdata = parse_quoted_printable(data.slice(di)); break;
default: throw new Error("Unsupported Content-Transfer-Encoding " + cte);
}
Expand Down
Loading

0 comments on commit 0f0b3de

Please sign in to comment.